customConfig.test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { cjsConfigFile, defaultConfigFile } from '../src/constants'
  4. import inTempDirectory from '../jest/runInTempDirectory'
  5. import { run, html, css, javascript } from './util/run'
  6. test('it uses the values from the custom config file', () => {
  7. let config = require(path.resolve(`${__dirname}/fixtures/custom-config.js`))
  8. return run('@tailwind utilities', config).then((result) => {
  9. expect(result.css).toMatchFormattedCss(css`
  10. @media (min-width: 400px) {
  11. .mobile\:font-bold {
  12. font-weight: 700;
  13. }
  14. }
  15. `)
  16. })
  17. })
  18. test('custom config can be passed as an object', () => {
  19. let config = {
  20. content: [{ raw: html`<div class="mobile:font-bold"></div>` }],
  21. theme: {
  22. screens: {
  23. mobile: '400px',
  24. },
  25. },
  26. }
  27. return run('@tailwind utilities', config).then((result) => {
  28. expect(result.css).toMatchFormattedCss(css`
  29. @media (min-width: 400px) {
  30. .mobile\:font-bold {
  31. font-weight: 700;
  32. }
  33. }
  34. `)
  35. })
  36. })
  37. test('custom config path can be passed using `config` property in an object', () => {
  38. let config = {
  39. config: path.resolve(`${__dirname}/fixtures/custom-config.js`),
  40. }
  41. return run('@tailwind utilities', config).then((result) => {
  42. expect(result.css).toMatchFormattedCss(css`
  43. @media (min-width: 400px) {
  44. .mobile\:font-bold {
  45. font-weight: 700;
  46. }
  47. }
  48. `)
  49. })
  50. })
  51. test('custom config can be passed under the `config` property', () => {
  52. let config = {
  53. config: {
  54. content: [{ raw: html`<div class="mobile:font-bold"></div>` }],
  55. theme: {
  56. screens: {
  57. mobile: '400px',
  58. },
  59. },
  60. },
  61. }
  62. return run('@tailwind utilities', config).then((result) => {
  63. expect(result.css).toMatchFormattedCss(css`
  64. @media (min-width: 400px) {
  65. .mobile\:font-bold {
  66. font-weight: 700;
  67. }
  68. }
  69. `)
  70. })
  71. })
  72. test('tailwind.config.cjs is picked up by default', () => {
  73. return inTempDirectory(() => {
  74. fs.writeFileSync(
  75. path.resolve(cjsConfigFile),
  76. javascript`module.exports = {
  77. content: [{ raw: '<div class="mobile:font-bold"></div>' }],
  78. theme: {
  79. screens: {
  80. mobile: '400px',
  81. },
  82. },
  83. }`
  84. )
  85. return run('@tailwind utilities').then((result) => {
  86. expect(result.css).toMatchFormattedCss(css`
  87. @media (min-width: 400px) {
  88. .mobile\:font-bold {
  89. font-weight: 700;
  90. }
  91. }
  92. `)
  93. })
  94. })
  95. })
  96. test('tailwind.config.js is picked up by default', () => {
  97. return inTempDirectory(() => {
  98. fs.writeFileSync(
  99. path.resolve(defaultConfigFile),
  100. javascript`module.exports = {
  101. content: [{ raw: '<div class="mobile:font-bold"></div>' }],
  102. theme: {
  103. screens: {
  104. mobile: '400px',
  105. },
  106. },
  107. }`
  108. )
  109. return run('@tailwind utilities').then((result) => {
  110. expect(result.css).toMatchFormattedCss(css`
  111. @media (min-width: 400px) {
  112. .mobile\:font-bold {
  113. font-weight: 700;
  114. }
  115. }
  116. `)
  117. })
  118. })
  119. })
  120. test('tailwind.config.cjs is picked up by default when passing an empty object', () => {
  121. return inTempDirectory(() => {
  122. fs.writeFileSync(
  123. path.resolve(cjsConfigFile),
  124. javascript`module.exports = {
  125. content: [{ raw: '<div class="mobile:font-bold"></div>' }],
  126. theme: {
  127. screens: {
  128. mobile: '400px',
  129. },
  130. },
  131. }`
  132. )
  133. return run('@tailwind utilities', {}).then((result) => {
  134. expect(result.css).toMatchFormattedCss(css`
  135. @media (min-width: 400px) {
  136. .mobile\:font-bold {
  137. font-weight: 700;
  138. }
  139. }
  140. `)
  141. })
  142. })
  143. })
  144. test('tailwind.config.js is picked up by default when passing an empty object', () => {
  145. return inTempDirectory(() => {
  146. fs.writeFileSync(
  147. path.resolve(defaultConfigFile),
  148. javascript`module.exports = {
  149. content: [{ raw: '<div class="mobile:font-bold"></div>' }],
  150. theme: {
  151. screens: {
  152. mobile: '400px',
  153. },
  154. },
  155. }`
  156. )
  157. return run('@tailwind utilities', {}).then((result) => {
  158. expect(result.css).toMatchFormattedCss(css`
  159. @media (min-width: 400px) {
  160. .mobile\:font-bold {
  161. font-weight: 700;
  162. }
  163. }
  164. `)
  165. })
  166. })
  167. })
  168. test('the default config can be overridden using the presets key', () => {
  169. let config = {
  170. content: [{ raw: html`<div class="min-h-primary min-h-secondary min-h-0"></div>` }],
  171. presets: [
  172. {
  173. theme: {
  174. extend: { minHeight: { secondary: '24px' } },
  175. },
  176. },
  177. ],
  178. theme: {
  179. extend: { minHeight: { primary: '48px' } },
  180. },
  181. }
  182. return run('@tailwind utilities', config).then((result) => {
  183. expect(result.css).toMatchFormattedCss(css`
  184. .min-h-primary {
  185. min-height: 48px;
  186. }
  187. .min-h-secondary {
  188. min-height: 24px;
  189. }
  190. .min-h-0 {
  191. min-height: 0px;
  192. }
  193. `)
  194. })
  195. })
  196. test('presets can be functions', () => {
  197. let config = {
  198. content: [{ raw: html`<div class="min-h-primary min-h-secondary min-h-0"></div>` }],
  199. presets: [
  200. () => ({
  201. theme: {
  202. extend: { minHeight: { secondary: '24px' } },
  203. },
  204. }),
  205. ],
  206. theme: {
  207. extend: { minHeight: { primary: '48px' } },
  208. },
  209. }
  210. return run('@tailwind utilities', config).then((result) => {
  211. expect(result.css).toMatchFormattedCss(css`
  212. .min-h-primary {
  213. min-height: 48px;
  214. }
  215. .min-h-secondary {
  216. min-height: 24px;
  217. }
  218. .min-h-0 {
  219. min-height: 0px;
  220. }
  221. `)
  222. })
  223. })
  224. test('the default config can be removed by using an empty presets key in a preset', () => {
  225. let config = {
  226. content: [{ raw: html`<div class="min-h-primary min-h-secondary min-h-0"></div>` }],
  227. presets: [
  228. {
  229. presets: [],
  230. theme: {
  231. extend: { minHeight: { secondary: '24px' } },
  232. },
  233. },
  234. ],
  235. theme: {
  236. extend: { minHeight: { primary: '48px' } },
  237. },
  238. }
  239. return run('@tailwind utilities', config).then((result) => {
  240. expect(result.css).toMatchFormattedCss(css`
  241. .min-h-primary {
  242. min-height: 48px;
  243. }
  244. .min-h-secondary {
  245. min-height: 24px;
  246. }
  247. `)
  248. })
  249. })
  250. test('presets can have their own presets', () => {
  251. let config = {
  252. content: [{ raw: html`<div class="bg-red bg-transparent bg-black bg-white"></div>` }],
  253. presets: [
  254. {
  255. presets: [],
  256. theme: {
  257. colors: { red: '#dd0000' },
  258. },
  259. },
  260. {
  261. presets: [
  262. {
  263. presets: [],
  264. theme: {
  265. colors: {
  266. transparent: 'transparent',
  267. red: '#ff0000',
  268. },
  269. },
  270. },
  271. ],
  272. theme: {
  273. extend: {
  274. colors: {
  275. black: 'black',
  276. red: '#ee0000',
  277. },
  278. backgroundColor: (theme) => theme('colors'),
  279. },
  280. },
  281. corePlugins: ['backgroundColor'],
  282. },
  283. ],
  284. theme: {
  285. extend: { colors: { white: 'white' } },
  286. },
  287. }
  288. return run('@tailwind utilities', config).then((result) => {
  289. expect(result.css).toMatchFormattedCss(css`
  290. .bg-red {
  291. background-color: #ee0000;
  292. }
  293. .bg-transparent {
  294. background-color: transparent;
  295. }
  296. .bg-black {
  297. background-color: black;
  298. }
  299. .bg-white {
  300. background-color: white;
  301. }
  302. `)
  303. })
  304. })
  305. test('function presets can be mixed with object presets', () => {
  306. let config = {
  307. content: [{ raw: html`<div class="bg-red bg-transparent bg-black bg-white"></div>` }],
  308. presets: [
  309. () => ({
  310. presets: [],
  311. theme: {
  312. colors: { red: '#dd0000' },
  313. },
  314. }),
  315. {
  316. presets: [
  317. () => ({
  318. presets: [],
  319. theme: {
  320. colors: {
  321. transparent: 'transparent',
  322. red: '#ff0000',
  323. },
  324. },
  325. }),
  326. ],
  327. theme: {
  328. extend: {
  329. colors: {
  330. black: 'black',
  331. red: '#ee0000',
  332. },
  333. backgroundColor: (theme) => theme('colors'),
  334. },
  335. },
  336. corePlugins: ['backgroundColor'],
  337. },
  338. ],
  339. theme: {
  340. extend: { colors: { white: 'white' } },
  341. },
  342. }
  343. return run('@tailwind utilities', config).then((result) => {
  344. expect(result.css).toMatchFormattedCss(css`
  345. .bg-red {
  346. background-color: #ee0000;
  347. }
  348. .bg-transparent {
  349. background-color: transparent;
  350. }
  351. .bg-black {
  352. background-color: black;
  353. }
  354. .bg-white {
  355. background-color: white;
  356. }
  357. `)
  358. })
  359. })