resolveConfig.test.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  1. import resolveConfig from '../src/util/resolveConfig'
  2. import corePluginList from '../src/corePluginList'
  3. test('prefix key overrides default prefix', () => {
  4. const userConfig = {
  5. prefix: 'tw-',
  6. }
  7. const defaultConfig = {
  8. prefix: '',
  9. important: false,
  10. separator: ':',
  11. content: [],
  12. theme: {
  13. screens: {
  14. mobile: '400px',
  15. },
  16. },
  17. }
  18. const result = resolveConfig([userConfig, defaultConfig])
  19. expect(result).toMatchObject({
  20. prefix: 'tw-',
  21. important: false,
  22. separator: ':',
  23. theme: {
  24. screens: {
  25. mobile: '400px',
  26. },
  27. },
  28. })
  29. })
  30. test('important key overrides default important', () => {
  31. const userConfig = {
  32. important: true,
  33. }
  34. const defaultConfig = {
  35. prefix: '',
  36. important: false,
  37. separator: ':',
  38. content: [],
  39. theme: {
  40. screens: {
  41. mobile: '400px',
  42. },
  43. },
  44. }
  45. const result = resolveConfig([userConfig, defaultConfig])
  46. expect(result).toMatchObject({
  47. prefix: '',
  48. important: true,
  49. separator: ':',
  50. theme: {
  51. screens: {
  52. mobile: '400px',
  53. },
  54. },
  55. })
  56. })
  57. test('important (selector) key overrides default important', () => {
  58. const userConfig = {
  59. important: '#app',
  60. }
  61. const defaultConfig = {
  62. prefix: '',
  63. important: false,
  64. separator: ':',
  65. content: [],
  66. theme: {
  67. screens: {
  68. mobile: '400px',
  69. },
  70. },
  71. }
  72. const result = resolveConfig([userConfig, defaultConfig])
  73. expect(result).toMatchObject({
  74. prefix: '',
  75. important: '#app',
  76. separator: ':',
  77. theme: {
  78. screens: {
  79. mobile: '400px',
  80. },
  81. },
  82. })
  83. })
  84. test('separator key overrides default separator', () => {
  85. const userConfig = {
  86. separator: '__',
  87. }
  88. const defaultConfig = {
  89. prefix: '',
  90. important: false,
  91. separator: ':',
  92. content: [],
  93. theme: {
  94. screens: {
  95. mobile: '400px',
  96. },
  97. },
  98. }
  99. const result = resolveConfig([userConfig, defaultConfig])
  100. expect(result).toMatchObject({
  101. prefix: '',
  102. important: false,
  103. separator: '__',
  104. theme: {
  105. screens: {
  106. mobile: '400px',
  107. },
  108. },
  109. })
  110. })
  111. test('theme key is merged instead of replaced', () => {
  112. const userConfig = {
  113. theme: {
  114. screens: {
  115. mobile: '400px',
  116. },
  117. },
  118. }
  119. const defaultConfig = {
  120. prefix: '-',
  121. important: false,
  122. separator: ':',
  123. content: [],
  124. theme: {
  125. colors: {
  126. 'grey-darker': '#606f7b',
  127. 'grey-dark': '#8795a1',
  128. grey: '#b8c2cc',
  129. 'grey-light': '#dae1e7',
  130. 'grey-lighter': '#f1f5f8',
  131. },
  132. fonts: {
  133. sans: ['system-ui', 'Roboto', 'sans-serif'],
  134. serif: ['Constantia', 'Lucida Bright', 'Georgia', 'serif'],
  135. },
  136. screens: {
  137. sm: '500px',
  138. md: '750px',
  139. lg: '1000px',
  140. },
  141. },
  142. }
  143. const result = resolveConfig([userConfig, defaultConfig])
  144. expect(result).toMatchObject({
  145. prefix: '-',
  146. important: false,
  147. separator: ':',
  148. theme: {
  149. colors: {
  150. 'grey-darker': '#606f7b',
  151. 'grey-dark': '#8795a1',
  152. grey: '#b8c2cc',
  153. 'grey-light': '#dae1e7',
  154. 'grey-lighter': '#f1f5f8',
  155. },
  156. fonts: {
  157. sans: ['system-ui', 'Roboto', 'sans-serif'],
  158. serif: ['Constantia', 'Lucida Bright', 'Georgia', 'serif'],
  159. },
  160. screens: {
  161. mobile: '400px',
  162. },
  163. },
  164. })
  165. })
  166. test('theme key is deeply merged instead of replaced', () => {
  167. const userConfig = {
  168. theme: {
  169. extend: {
  170. colors: {
  171. grey: {
  172. darker: '#606f7b',
  173. dark: '#8795a1',
  174. },
  175. },
  176. },
  177. },
  178. }
  179. const defaultConfig = {
  180. prefix: '-',
  181. important: false,
  182. separator: ':',
  183. content: [],
  184. theme: {
  185. colors: {
  186. grey: {
  187. grey: '#b8c2cc',
  188. light: '#dae1e7',
  189. lighter: '#f1f5f8',
  190. },
  191. },
  192. },
  193. }
  194. const result = resolveConfig([userConfig, defaultConfig])
  195. expect(result).toMatchObject({
  196. prefix: '-',
  197. important: false,
  198. separator: ':',
  199. theme: {
  200. colors: {
  201. grey: {
  202. darker: '#606f7b',
  203. dark: '#8795a1',
  204. grey: '#b8c2cc',
  205. light: '#dae1e7',
  206. lighter: '#f1f5f8',
  207. },
  208. },
  209. },
  210. })
  211. })
  212. test('missing top level keys are pulled from the default config', () => {
  213. const userConfig = {}
  214. const defaultConfig = {
  215. prefix: '-',
  216. important: false,
  217. separator: ':',
  218. content: [],
  219. theme: {
  220. colors: { green: '#00ff00' },
  221. screens: {
  222. mobile: '400px',
  223. },
  224. },
  225. }
  226. const result = resolveConfig([userConfig, defaultConfig])
  227. expect(result).toMatchObject({
  228. prefix: '-',
  229. important: false,
  230. separator: ':',
  231. theme: {
  232. colors: { green: '#00ff00' },
  233. screens: {
  234. mobile: '400px',
  235. },
  236. },
  237. })
  238. })
  239. test('functions in the default theme section are lazily evaluated', () => {
  240. const userConfig = {
  241. theme: {
  242. colors: {
  243. red: 'red',
  244. green: 'green',
  245. blue: 'blue',
  246. },
  247. },
  248. }
  249. const defaultConfig = {
  250. prefix: '-',
  251. important: false,
  252. separator: ':',
  253. content: [],
  254. theme: {
  255. colors: {
  256. cyan: 'cyan',
  257. magenta: 'magenta',
  258. yellow: 'yellow',
  259. },
  260. backgroundColors: (theme) => theme('colors'),
  261. textColors: (theme) => theme('colors'),
  262. },
  263. }
  264. const result = resolveConfig([userConfig, defaultConfig])
  265. expect(result).toMatchObject({
  266. prefix: '-',
  267. important: false,
  268. separator: ':',
  269. theme: {
  270. colors: {
  271. red: 'red',
  272. green: 'green',
  273. blue: 'blue',
  274. },
  275. backgroundColors: {
  276. red: 'red',
  277. green: 'green',
  278. blue: 'blue',
  279. },
  280. textColors: {
  281. red: 'red',
  282. green: 'green',
  283. blue: 'blue',
  284. },
  285. },
  286. })
  287. })
  288. test('functions in the user theme section are lazily evaluated', () => {
  289. const userConfig = {
  290. theme: {
  291. colors: {
  292. red: 'red',
  293. green: 'green',
  294. blue: 'blue',
  295. },
  296. backgroundColors: (theme) => ({
  297. ...theme('colors'),
  298. customBackground: '#bada55',
  299. }),
  300. textColors: (theme) => ({
  301. ...theme('colors'),
  302. customText: '#facade',
  303. }),
  304. },
  305. }
  306. const defaultConfig = {
  307. prefix: '-',
  308. important: false,
  309. separator: ':',
  310. content: [],
  311. theme: {
  312. colors: {
  313. cyan: 'cyan',
  314. magenta: 'magenta',
  315. yellow: 'yellow',
  316. },
  317. backgroundColors: ({ colors }) => colors,
  318. textColors: ({ colors }) => colors,
  319. },
  320. }
  321. const result = resolveConfig([userConfig, defaultConfig])
  322. expect(result).toMatchObject({
  323. prefix: '-',
  324. important: false,
  325. separator: ':',
  326. theme: {
  327. colors: {
  328. red: 'red',
  329. green: 'green',
  330. blue: 'blue',
  331. },
  332. backgroundColors: {
  333. red: 'red',
  334. green: 'green',
  335. blue: 'blue',
  336. customBackground: '#bada55',
  337. },
  338. textColors: {
  339. red: 'red',
  340. green: 'green',
  341. blue: 'blue',
  342. customText: '#facade',
  343. },
  344. },
  345. })
  346. })
  347. test('theme values in the extend section extend the existing theme', () => {
  348. const userConfig = {
  349. theme: {
  350. extend: {
  351. opacity: {
  352. 25: '25',
  353. 75: '.75',
  354. },
  355. backgroundColors: {
  356. customBackground: '#bada55',
  357. },
  358. },
  359. },
  360. }
  361. const defaultConfig = {
  362. prefix: '-',
  363. important: false,
  364. separator: ':',
  365. content: [],
  366. theme: {
  367. colors: {
  368. cyan: 'cyan',
  369. magenta: 'magenta',
  370. yellow: 'yellow',
  371. },
  372. opacity: {
  373. 0: '0',
  374. 50: '.5',
  375. 100: '1',
  376. },
  377. backgroundColors: (theme) => theme('colors'),
  378. },
  379. }
  380. const result = resolveConfig([userConfig, defaultConfig])
  381. expect(result).toMatchObject({
  382. prefix: '-',
  383. important: false,
  384. separator: ':',
  385. theme: {
  386. colors: {
  387. cyan: 'cyan',
  388. magenta: 'magenta',
  389. yellow: 'yellow',
  390. },
  391. opacity: {
  392. 0: '0',
  393. 50: '.5',
  394. 100: '1',
  395. 25: '25',
  396. 75: '.75',
  397. },
  398. backgroundColors: {
  399. cyan: 'cyan',
  400. magenta: 'magenta',
  401. yellow: 'yellow',
  402. customBackground: '#bada55',
  403. },
  404. },
  405. })
  406. })
  407. test('theme values in the extend section extend the user theme', () => {
  408. const userConfig = {
  409. theme: {
  410. opacity: {
  411. 0: '0',
  412. 20: '.2',
  413. 40: '.4',
  414. },
  415. height: (theme) => theme('width'),
  416. extend: {
  417. opacity: {
  418. 60: '.6',
  419. 80: '.8',
  420. 100: '1',
  421. },
  422. height: {
  423. customHeight: '500vh',
  424. },
  425. },
  426. },
  427. }
  428. const defaultConfig = {
  429. prefix: '-',
  430. important: false,
  431. separator: ':',
  432. content: [],
  433. theme: {
  434. opacity: {
  435. 0: '0',
  436. 50: '.5',
  437. 100: '1',
  438. },
  439. height: {
  440. 0: 0,
  441. full: '100%',
  442. },
  443. width: {
  444. 0: 0,
  445. 1: '.25rem',
  446. 2: '.5rem',
  447. 3: '.75rem',
  448. 4: '1rem',
  449. },
  450. },
  451. }
  452. const result = resolveConfig([userConfig, defaultConfig])
  453. expect(result).toMatchObject({
  454. prefix: '-',
  455. important: false,
  456. separator: ':',
  457. theme: {
  458. opacity: {
  459. 0: '0',
  460. 20: '.2',
  461. 40: '.4',
  462. 60: '.6',
  463. 80: '.8',
  464. 100: '1',
  465. },
  466. height: {
  467. 0: 0,
  468. 1: '.25rem',
  469. 2: '.5rem',
  470. 3: '.75rem',
  471. 4: '1rem',
  472. customHeight: '500vh',
  473. },
  474. width: {
  475. 0: 0,
  476. 1: '.25rem',
  477. 2: '.5rem',
  478. 3: '.75rem',
  479. 4: '1rem',
  480. },
  481. },
  482. })
  483. })
  484. test('theme values in the extend section can extend values that are depended on lazily', () => {
  485. const userConfig = {
  486. theme: {
  487. extend: {
  488. colors: {
  489. red: 'red',
  490. green: 'green',
  491. blue: 'blue',
  492. },
  493. backgroundColors: {
  494. customBackground: '#bada55',
  495. },
  496. },
  497. },
  498. }
  499. const defaultConfig = {
  500. prefix: '-',
  501. important: false,
  502. separator: ':',
  503. content: [],
  504. theme: {
  505. colors: {
  506. cyan: 'cyan',
  507. magenta: 'magenta',
  508. yellow: 'yellow',
  509. },
  510. backgroundColors: (theme) => theme('colors'),
  511. },
  512. }
  513. const result = resolveConfig([userConfig, defaultConfig])
  514. expect(result).toMatchObject({
  515. prefix: '-',
  516. important: false,
  517. separator: ':',
  518. theme: {
  519. colors: {
  520. cyan: 'cyan',
  521. magenta: 'magenta',
  522. yellow: 'yellow',
  523. red: 'red',
  524. green: 'green',
  525. blue: 'blue',
  526. },
  527. backgroundColors: {
  528. cyan: 'cyan',
  529. magenta: 'magenta',
  530. yellow: 'yellow',
  531. red: 'red',
  532. green: 'green',
  533. blue: 'blue',
  534. customBackground: '#bada55',
  535. },
  536. },
  537. })
  538. })
  539. test('theme values in the extend section are not deeply merged when they are simple arrays', () => {
  540. const userConfig = {
  541. theme: {
  542. extend: {
  543. fonts: {
  544. sans: ['Comic Sans'],
  545. serif: ['Papyrus', { fontFeatureSettings: '"cv11"' }],
  546. mono: [['Lobster', 'Papyrus'], { fontFeatureSettings: '"cv11"' }],
  547. },
  548. },
  549. },
  550. }
  551. const defaultConfig = {
  552. prefix: '-',
  553. important: false,
  554. separator: ':',
  555. content: [],
  556. theme: {
  557. fonts: {
  558. sans: ['system-ui', 'Helvetica Neue', 'sans-serif'],
  559. serif: ['Constantia', 'Georgia', 'serif'],
  560. mono: ['Menlo', 'Courier New', 'monospace'],
  561. },
  562. },
  563. }
  564. const result = resolveConfig([userConfig, defaultConfig])
  565. expect(result).toMatchObject({
  566. prefix: '-',
  567. important: false,
  568. separator: ':',
  569. theme: {
  570. fonts: {
  571. sans: ['Comic Sans'],
  572. serif: ['Papyrus', { fontFeatureSettings: '"cv11"' }],
  573. mono: [['Lobster', 'Papyrus'], { fontFeatureSettings: '"cv11"' }],
  574. },
  575. },
  576. })
  577. })
  578. test('theme values in the extend section are deeply merged, when they are arrays of objects', () => {
  579. const userConfig = {
  580. theme: {
  581. extend: {
  582. typography: {
  583. ArrayArray: {
  584. css: [{ a: { backgroundColor: 'red' } }, { a: { color: 'green' } }],
  585. },
  586. ObjectArray: {
  587. css: { a: { backgroundColor: 'red' } },
  588. },
  589. ArrayObject: {
  590. css: [{ a: { backgroundColor: 'red' } }, { a: { color: 'green' } }],
  591. },
  592. },
  593. },
  594. },
  595. }
  596. const defaultConfig = {
  597. prefix: '-',
  598. important: false,
  599. separator: ':',
  600. content: [],
  601. theme: {
  602. typography: {
  603. ArrayArray: {
  604. css: [{ a: { underline: 'none' } }],
  605. },
  606. ObjectArray: {
  607. css: [{ a: { underline: 'none' } }],
  608. },
  609. ArrayObject: {
  610. css: { a: { underline: 'none' } },
  611. },
  612. },
  613. },
  614. }
  615. const result = resolveConfig([userConfig, defaultConfig])
  616. expect(result).toMatchObject({
  617. prefix: '-',
  618. important: false,
  619. separator: ':',
  620. theme: {
  621. typography: {
  622. ArrayArray: {
  623. css: [
  624. { a: { underline: 'none' } },
  625. { a: { backgroundColor: 'red' } },
  626. { a: { color: 'green' } },
  627. ],
  628. },
  629. ObjectArray: {
  630. css: [{ a: { underline: 'none' } }, { a: { backgroundColor: 'red' } }],
  631. },
  632. ArrayObject: {
  633. css: [
  634. { a: { underline: 'none' } },
  635. { a: { backgroundColor: 'red' } },
  636. { a: { color: 'green' } },
  637. ],
  638. },
  639. },
  640. },
  641. })
  642. })
  643. test('the theme function can use a default value if the key is missing', () => {
  644. const userConfig = {
  645. theme: {
  646. colors: {
  647. red: 'red',
  648. green: 'green',
  649. blue: 'blue',
  650. },
  651. },
  652. }
  653. const defaultConfig = {
  654. prefix: '-',
  655. important: false,
  656. separator: ':',
  657. content: [],
  658. theme: {
  659. colors: {
  660. cyan: 'cyan',
  661. magenta: 'magenta',
  662. yellow: 'yellow',
  663. },
  664. borderColor: (theme) => ({
  665. default: theme('colors.gray', 'currentColor'),
  666. ...theme('colors'),
  667. }),
  668. },
  669. }
  670. const result = resolveConfig([userConfig, defaultConfig])
  671. expect(result).toMatchObject({
  672. prefix: '-',
  673. important: false,
  674. separator: ':',
  675. theme: {
  676. colors: {
  677. red: 'red',
  678. green: 'green',
  679. blue: 'blue',
  680. },
  681. borderColor: {
  682. default: 'currentColor',
  683. red: 'red',
  684. green: 'green',
  685. blue: 'blue',
  686. },
  687. },
  688. })
  689. })
  690. test('the theme function can resolve function values', () => {
  691. const userConfig = {
  692. theme: {
  693. textColor: (theme) => ({
  694. lime: 'lime',
  695. ...theme('colors'),
  696. }),
  697. backgroundColor: (theme) => ({
  698. orange: 'orange',
  699. ...theme('textColor'),
  700. }),
  701. borderColor: (theme) => theme('backgroundColor'),
  702. },
  703. }
  704. const defaultConfig = {
  705. prefix: '-',
  706. important: false,
  707. separator: ':',
  708. content: [],
  709. theme: {
  710. colors: {
  711. red: 'red',
  712. green: 'green',
  713. blue: 'blue',
  714. },
  715. },
  716. }
  717. const result = resolveConfig([userConfig, defaultConfig])
  718. expect(result).toMatchObject({
  719. prefix: '-',
  720. important: false,
  721. separator: ':',
  722. theme: {
  723. colors: {
  724. red: 'red',
  725. green: 'green',
  726. blue: 'blue',
  727. },
  728. textColor: {
  729. lime: 'lime',
  730. red: 'red',
  731. green: 'green',
  732. blue: 'blue',
  733. },
  734. backgroundColor: {
  735. lime: 'lime',
  736. orange: 'orange',
  737. red: 'red',
  738. green: 'green',
  739. blue: 'blue',
  740. },
  741. borderColor: {
  742. lime: 'lime',
  743. orange: 'orange',
  744. red: 'red',
  745. green: 'green',
  746. blue: 'blue',
  747. },
  748. },
  749. })
  750. })
  751. test('the theme function can resolve deep function values', () => {
  752. const userConfig = {
  753. theme: {
  754. minWidth: (theme) => ({
  755. '1/3': theme('width.1/3'),
  756. }),
  757. },
  758. }
  759. const defaultConfig = {
  760. prefix: '-',
  761. important: false,
  762. separator: ':',
  763. content: [],
  764. theme: {
  765. spacing: {
  766. 0: '0',
  767. },
  768. width: (theme) => ({
  769. ...theme('spacing'),
  770. '1/3': '33.33333%',
  771. }),
  772. },
  773. }
  774. const result = resolveConfig([userConfig, defaultConfig])
  775. expect(result).toMatchObject({
  776. prefix: '-',
  777. important: false,
  778. separator: ':',
  779. theme: {
  780. spacing: {
  781. 0: '0',
  782. },
  783. width: {
  784. 0: '0',
  785. '1/3': '33.33333%',
  786. },
  787. minWidth: {
  788. '1/3': '33.33333%',
  789. },
  790. },
  791. })
  792. })
  793. test('theme values in the extend section are lazily evaluated', () => {
  794. const userConfig = {
  795. theme: {
  796. colors: {
  797. red: 'red',
  798. green: 'green',
  799. blue: 'blue',
  800. },
  801. extend: {
  802. colors: {
  803. orange: 'orange',
  804. },
  805. borderColor: (theme) => ({
  806. foo: theme('colors.orange'),
  807. bar: theme('colors.red'),
  808. }),
  809. },
  810. },
  811. }
  812. const defaultConfig = {
  813. prefix: '-',
  814. important: false,
  815. separator: ':',
  816. content: [],
  817. theme: {
  818. colors: {
  819. cyan: 'cyan',
  820. magenta: 'magenta',
  821. yellow: 'yellow',
  822. },
  823. borderColor: (theme) => ({
  824. default: theme('colors.yellow', 'currentColor'),
  825. ...theme('colors'),
  826. }),
  827. },
  828. }
  829. const result = resolveConfig([userConfig, defaultConfig])
  830. expect(result).toMatchObject({
  831. prefix: '-',
  832. important: false,
  833. separator: ':',
  834. theme: {
  835. colors: {
  836. orange: 'orange',
  837. red: 'red',
  838. green: 'green',
  839. blue: 'blue',
  840. },
  841. borderColor: {
  842. default: 'currentColor',
  843. foo: 'orange',
  844. bar: 'red',
  845. orange: 'orange',
  846. red: 'red',
  847. green: 'green',
  848. blue: 'blue',
  849. },
  850. },
  851. })
  852. })
  853. test('lazily evaluated values have access to the config utils', () => {
  854. const userConfig = {
  855. theme: {
  856. inset: (theme) => theme('margin'),
  857. shift: (theme, { negative }) => ({
  858. ...theme('spacing'),
  859. ...negative(theme('spacing')),
  860. }),
  861. extend: {
  862. nudge: (theme, { negative }) => ({
  863. ...theme('spacing'),
  864. ...negative(theme('spacing')),
  865. }),
  866. },
  867. },
  868. }
  869. const defaultConfig = {
  870. prefix: '-',
  871. important: false,
  872. separator: ':',
  873. content: [],
  874. theme: {
  875. spacing: {
  876. 1: '1px',
  877. 2: '2px',
  878. 3: '3px',
  879. 4: '4px',
  880. },
  881. margin: (theme, { negative }) => ({
  882. ...theme('spacing'),
  883. ...negative(theme('spacing')),
  884. }),
  885. },
  886. }
  887. const result = resolveConfig([userConfig, defaultConfig])
  888. expect(result).toMatchObject({
  889. prefix: '-',
  890. important: false,
  891. separator: ':',
  892. theme: {
  893. spacing: {
  894. 1: '1px',
  895. 2: '2px',
  896. 3: '3px',
  897. 4: '4px',
  898. },
  899. inset: {
  900. '-1': '-1px',
  901. '-2': '-2px',
  902. '-3': '-3px',
  903. '-4': '-4px',
  904. 1: '1px',
  905. 2: '2px',
  906. 3: '3px',
  907. 4: '4px',
  908. },
  909. margin: {
  910. '-1': '-1px',
  911. '-2': '-2px',
  912. '-3': '-3px',
  913. '-4': '-4px',
  914. 1: '1px',
  915. 2: '2px',
  916. 3: '3px',
  917. 4: '4px',
  918. },
  919. shift: {
  920. '-1': '-1px',
  921. '-2': '-2px',
  922. '-3': '-3px',
  923. '-4': '-4px',
  924. 1: '1px',
  925. 2: '2px',
  926. 3: '3px',
  927. 4: '4px',
  928. },
  929. nudge: {
  930. '-1': '-1px',
  931. '-2': '-2px',
  932. '-3': '-3px',
  933. '-4': '-4px',
  934. 1: '1px',
  935. 2: '2px',
  936. 3: '3px',
  937. 4: '4px',
  938. },
  939. },
  940. })
  941. })
  942. test('the original theme is not mutated', () => {
  943. const userConfig = {
  944. theme: {
  945. extend: {
  946. colors: {
  947. orange: 'orange',
  948. },
  949. },
  950. },
  951. }
  952. const defaultConfig = {
  953. prefix: '-',
  954. important: false,
  955. separator: ':',
  956. content: [],
  957. theme: {
  958. colors: {
  959. cyan: 'cyan',
  960. magenta: 'magenta',
  961. yellow: 'yellow',
  962. },
  963. },
  964. }
  965. resolveConfig([userConfig, defaultConfig])
  966. expect(userConfig).toEqual({
  967. theme: {
  968. extend: {
  969. colors: {
  970. orange: 'orange',
  971. },
  972. },
  973. },
  974. })
  975. })
  976. test('custom properties are multiplied by -1 for negative values', () => {
  977. const userConfig = {
  978. theme: {
  979. spacing: {
  980. 0: 0,
  981. 1: '1px',
  982. 2: '2px',
  983. 3: '3px',
  984. 4: '4px',
  985. auto: 'auto',
  986. foo: 'var(--foo)',
  987. bar: 'var(--bar, 500px)',
  988. baz: 'calc(50% - 10px)',
  989. qux: '10poops',
  990. },
  991. margin: (theme, { negative }) => ({
  992. ...theme('spacing'),
  993. ...negative(theme('spacing')),
  994. }),
  995. },
  996. }
  997. const defaultConfig = {
  998. prefix: '-',
  999. important: false,
  1000. separator: ':',
  1001. content: [],
  1002. theme: {},
  1003. }
  1004. const result = resolveConfig([userConfig, defaultConfig])
  1005. expect(result.theme.spacing).toEqual({
  1006. 0: 0,
  1007. 1: '1px',
  1008. 2: '2px',
  1009. 3: '3px',
  1010. 4: '4px',
  1011. auto: 'auto',
  1012. foo: 'var(--foo)',
  1013. bar: 'var(--bar, 500px)',
  1014. baz: 'calc(50% - 10px)',
  1015. qux: '10poops',
  1016. })
  1017. expect(result.theme.margin).toEqual({
  1018. 0: 0,
  1019. 1: '1px',
  1020. 2: '2px',
  1021. 3: '3px',
  1022. 4: '4px',
  1023. auto: 'auto',
  1024. foo: 'var(--foo)',
  1025. bar: 'var(--bar, 500px)',
  1026. baz: 'calc(50% - 10px)',
  1027. qux: '10poops',
  1028. '-0': '0',
  1029. '-1': '-1px',
  1030. '-2': '-2px',
  1031. '-3': '-3px',
  1032. '-4': '-4px',
  1033. '-foo': 'calc(var(--foo) * -1)',
  1034. '-bar': 'calc(var(--bar, 500px) * -1)',
  1035. '-baz': 'calc(calc(50% - 10px) * -1)',
  1036. '-qux': '-10poops',
  1037. })
  1038. })
  1039. test('more than two config objects can be resolved', () => {
  1040. const firstConfig = {
  1041. theme: {
  1042. extend: {
  1043. fontFamily: () => ({
  1044. code: ['Menlo', 'monospace'],
  1045. }),
  1046. colors: {
  1047. red: 'red',
  1048. },
  1049. backgroundColor: {
  1050. customBackgroundOne: '#bada55',
  1051. },
  1052. textDecorationColor: {
  1053. orange: 'orange',
  1054. },
  1055. },
  1056. },
  1057. }
  1058. const secondConfig = {
  1059. prefix: '-',
  1060. important: false,
  1061. separator: ':',
  1062. theme: {
  1063. extend: {
  1064. fontFamily: {
  1065. quote: ['Helvetica', 'serif'],
  1066. },
  1067. colors: {
  1068. green: 'green',
  1069. },
  1070. backgroundColor: {
  1071. customBackgroundTwo: '#facade',
  1072. },
  1073. textDecorationColor: (theme) => theme('colors'),
  1074. },
  1075. },
  1076. }
  1077. const thirdConfig = {
  1078. prefix: '-',
  1079. important: false,
  1080. separator: ':',
  1081. theme: {
  1082. extend: {
  1083. fontFamily: {
  1084. hero: ['Futura', 'sans-serif'],
  1085. },
  1086. colors: {
  1087. pink: 'pink',
  1088. },
  1089. backgroundColor: () => ({
  1090. customBackgroundThree: '#c0ffee',
  1091. }),
  1092. textDecorationColor: {
  1093. lime: 'lime',
  1094. },
  1095. },
  1096. },
  1097. }
  1098. const defaultConfig = {
  1099. prefix: '-',
  1100. important: false,
  1101. separator: ':',
  1102. content: [],
  1103. theme: {
  1104. fontFamily: {
  1105. body: ['Arial', 'sans-serif'],
  1106. display: ['Georgia', 'serif'],
  1107. },
  1108. colors: {
  1109. blue: 'blue',
  1110. },
  1111. backgroundColor: (theme) => theme('colors'),
  1112. },
  1113. }
  1114. const result = resolveConfig([firstConfig, secondConfig, thirdConfig, defaultConfig])
  1115. expect(result).toMatchObject({
  1116. prefix: '-',
  1117. important: false,
  1118. separator: ':',
  1119. theme: {
  1120. fontFamily: {
  1121. body: ['Arial', 'sans-serif'],
  1122. display: ['Georgia', 'serif'],
  1123. code: ['Menlo', 'monospace'],
  1124. quote: ['Helvetica', 'serif'],
  1125. hero: ['Futura', 'sans-serif'],
  1126. },
  1127. colors: {
  1128. red: 'red',
  1129. green: 'green',
  1130. blue: 'blue',
  1131. pink: 'pink',
  1132. },
  1133. backgroundColor: {
  1134. red: 'red',
  1135. green: 'green',
  1136. blue: 'blue',
  1137. pink: 'pink',
  1138. customBackgroundOne: '#bada55',
  1139. customBackgroundTwo: '#facade',
  1140. customBackgroundThree: '#c0ffee',
  1141. },
  1142. textDecorationColor: {
  1143. red: 'red',
  1144. green: 'green',
  1145. blue: 'blue',
  1146. pink: 'pink',
  1147. orange: 'orange',
  1148. lime: 'lime',
  1149. },
  1150. },
  1151. })
  1152. })
  1153. test('plugin config modifications are applied', () => {
  1154. const userConfig = {
  1155. plugins: [
  1156. {
  1157. config: {
  1158. prefix: 'tw-',
  1159. },
  1160. },
  1161. ],
  1162. }
  1163. const defaultConfig = {
  1164. prefix: '',
  1165. important: false,
  1166. separator: ':',
  1167. content: [],
  1168. theme: {
  1169. screens: {
  1170. mobile: '400px',
  1171. },
  1172. },
  1173. }
  1174. const result = resolveConfig([userConfig, defaultConfig])
  1175. expect(result).toMatchObject({
  1176. prefix: 'tw-',
  1177. important: false,
  1178. separator: ':',
  1179. theme: {
  1180. screens: {
  1181. mobile: '400px',
  1182. },
  1183. },
  1184. plugins: userConfig.plugins,
  1185. })
  1186. })
  1187. test('user config takes precedence over plugin config modifications', () => {
  1188. const userConfig = {
  1189. prefix: 'user-',
  1190. plugins: [
  1191. {
  1192. config: {
  1193. prefix: 'tw-',
  1194. },
  1195. },
  1196. ],
  1197. }
  1198. const defaultConfig = {
  1199. prefix: '',
  1200. important: false,
  1201. separator: ':',
  1202. content: [],
  1203. theme: {
  1204. screens: {
  1205. mobile: '400px',
  1206. },
  1207. },
  1208. }
  1209. const result = resolveConfig([userConfig, defaultConfig])
  1210. expect(result).toMatchObject({
  1211. prefix: 'user-',
  1212. important: false,
  1213. separator: ':',
  1214. theme: {
  1215. screens: {
  1216. mobile: '400px',
  1217. },
  1218. },
  1219. plugins: userConfig.plugins,
  1220. })
  1221. })
  1222. test('plugin config can register plugins that also have config', () => {
  1223. const userConfig = {
  1224. plugins: [
  1225. {
  1226. config: {
  1227. prefix: 'tw-',
  1228. plugins: [
  1229. {
  1230. config: {
  1231. important: true,
  1232. },
  1233. },
  1234. {
  1235. config: {
  1236. separator: '__',
  1237. },
  1238. },
  1239. ],
  1240. },
  1241. handler() {},
  1242. },
  1243. ],
  1244. }
  1245. const defaultConfig = {
  1246. prefix: '',
  1247. important: false,
  1248. separator: ':',
  1249. content: [],
  1250. theme: {
  1251. screens: {
  1252. mobile: '400px',
  1253. },
  1254. },
  1255. }
  1256. const result = resolveConfig([userConfig, defaultConfig])
  1257. expect(result).toMatchObject({
  1258. prefix: 'tw-',
  1259. important: true,
  1260. separator: '__',
  1261. theme: {
  1262. screens: {
  1263. mobile: '400px',
  1264. },
  1265. },
  1266. plugins: userConfig.plugins,
  1267. })
  1268. })
  1269. test('plugin configs take precedence over plugin configs registered by that plugin', () => {
  1270. const userConfig = {
  1271. plugins: [
  1272. {
  1273. config: {
  1274. prefix: 'outer-',
  1275. plugins: [
  1276. {
  1277. config: {
  1278. prefix: 'inner-',
  1279. },
  1280. },
  1281. ],
  1282. },
  1283. handler() {},
  1284. },
  1285. ],
  1286. }
  1287. const defaultConfig = {
  1288. prefix: '',
  1289. important: false,
  1290. separator: ':',
  1291. content: [],
  1292. theme: {
  1293. screens: {
  1294. mobile: '400px',
  1295. },
  1296. },
  1297. }
  1298. const result = resolveConfig([userConfig, defaultConfig])
  1299. expect(result).toMatchObject({
  1300. prefix: 'outer-',
  1301. important: false,
  1302. separator: ':',
  1303. theme: {
  1304. screens: {
  1305. mobile: '400px',
  1306. },
  1307. },
  1308. plugins: userConfig.plugins,
  1309. })
  1310. })
  1311. test('plugin theme extensions are added even if user overrides top-level theme config', () => {
  1312. const userConfig = {
  1313. theme: {
  1314. width: {
  1315. '1px': '1px',
  1316. },
  1317. },
  1318. plugins: [
  1319. {
  1320. config: {
  1321. theme: {
  1322. extend: {
  1323. width: {
  1324. '2px': '2px',
  1325. '3px': '3px',
  1326. },
  1327. },
  1328. },
  1329. },
  1330. handler() {},
  1331. },
  1332. ],
  1333. }
  1334. const defaultConfig = {
  1335. prefix: '',
  1336. important: false,
  1337. separator: ':',
  1338. content: [],
  1339. theme: {
  1340. width: {
  1341. sm: '1rem',
  1342. md: '2rem',
  1343. lg: '3rem',
  1344. },
  1345. screens: {
  1346. mobile: '400px',
  1347. },
  1348. },
  1349. }
  1350. const result = resolveConfig([userConfig, defaultConfig])
  1351. expect(result).toMatchObject({
  1352. prefix: '',
  1353. important: false,
  1354. separator: ':',
  1355. theme: {
  1356. width: {
  1357. '1px': '1px',
  1358. '2px': '2px',
  1359. '3px': '3px',
  1360. },
  1361. screens: {
  1362. mobile: '400px',
  1363. },
  1364. },
  1365. plugins: userConfig.plugins,
  1366. })
  1367. })
  1368. test('user theme extensions take precedence over plugin theme extensions with the same key', () => {
  1369. const userConfig = {
  1370. theme: {
  1371. extend: {
  1372. width: {
  1373. xl: '6rem',
  1374. },
  1375. },
  1376. },
  1377. plugins: [
  1378. {
  1379. config: {
  1380. theme: {
  1381. extend: {
  1382. width: {
  1383. xl: '4rem',
  1384. },
  1385. },
  1386. },
  1387. },
  1388. handler() {},
  1389. },
  1390. ],
  1391. }
  1392. const defaultConfig = {
  1393. prefix: '',
  1394. important: false,
  1395. separator: ':',
  1396. content: [],
  1397. theme: {
  1398. width: {
  1399. sm: '1rem',
  1400. md: '2rem',
  1401. lg: '3rem',
  1402. },
  1403. screens: {
  1404. mobile: '400px',
  1405. },
  1406. },
  1407. }
  1408. const result = resolveConfig([userConfig, defaultConfig])
  1409. expect(result).toMatchObject({
  1410. prefix: '',
  1411. important: false,
  1412. separator: ':',
  1413. theme: {
  1414. width: {
  1415. sm: '1rem',
  1416. md: '2rem',
  1417. lg: '3rem',
  1418. xl: '6rem',
  1419. },
  1420. screens: {
  1421. mobile: '400px',
  1422. },
  1423. },
  1424. plugins: userConfig.plugins,
  1425. })
  1426. })
  1427. test('extensions are applied in the right order', () => {
  1428. const userConfig = {
  1429. theme: {
  1430. extend: {
  1431. colors: {
  1432. grey: {
  1433. light: '#eee',
  1434. },
  1435. },
  1436. },
  1437. },
  1438. }
  1439. const otherConfig = {
  1440. theme: {
  1441. extend: {
  1442. colors: {
  1443. grey: {
  1444. light: '#ddd',
  1445. darker: '#111',
  1446. },
  1447. },
  1448. },
  1449. },
  1450. }
  1451. const anotherConfig = {
  1452. theme: {
  1453. extend: {
  1454. colors: {
  1455. grey: {
  1456. darker: '#222',
  1457. },
  1458. },
  1459. },
  1460. },
  1461. }
  1462. const defaultConfig = {
  1463. content: [],
  1464. theme: {
  1465. colors: {
  1466. grey: {
  1467. light: '#ccc',
  1468. dark: '#333',
  1469. },
  1470. },
  1471. },
  1472. }
  1473. const result = resolveConfig([userConfig, otherConfig, anotherConfig, defaultConfig])
  1474. expect(result).toMatchObject({
  1475. theme: {
  1476. colors: {
  1477. grey: {
  1478. light: '#eee',
  1479. dark: '#333',
  1480. darker: '#111',
  1481. },
  1482. },
  1483. },
  1484. })
  1485. })
  1486. test('core plugin configuration builds on the default list when starting with an empty object', () => {
  1487. const userConfig = {
  1488. corePlugins: { display: false },
  1489. }
  1490. const defaultConfig = {
  1491. prefix: '',
  1492. important: false,
  1493. separator: ':',
  1494. content: [],
  1495. theme: {},
  1496. corePlugins: {},
  1497. }
  1498. const result = resolveConfig([userConfig, defaultConfig])
  1499. expect(result).toMatchObject({
  1500. prefix: '',
  1501. important: false,
  1502. separator: ':',
  1503. theme: {},
  1504. corePlugins: corePluginList.filter((c) => c !== 'display'),
  1505. })
  1506. })
  1507. test('core plugins that are disabled by default can be enabled', () => {
  1508. const userConfig = {
  1509. corePlugins: { display: true },
  1510. }
  1511. const defaultConfig = {
  1512. presets: [],
  1513. prefix: '',
  1514. important: false,
  1515. separator: ':',
  1516. content: [],
  1517. theme: {},
  1518. corePlugins: { display: false },
  1519. }
  1520. const result = resolveConfig([userConfig, defaultConfig])
  1521. expect(result.corePlugins).toContain('display')
  1522. })
  1523. test('core plugin configurations stack', () => {
  1524. const userConfig = {
  1525. corePlugins: { display: false },
  1526. }
  1527. const otherConfig = {
  1528. corePlugins: ({ corePlugins }) => {
  1529. return [...corePlugins, 'margin']
  1530. },
  1531. }
  1532. const defaultConfig = {
  1533. prefix: '',
  1534. important: false,
  1535. separator: ':',
  1536. content: [],
  1537. theme: {},
  1538. corePlugins: ['float', 'display', 'padding'],
  1539. }
  1540. const result = resolveConfig([userConfig, otherConfig, defaultConfig])
  1541. expect(result).toMatchObject({
  1542. prefix: '',
  1543. important: false,
  1544. separator: ':',
  1545. theme: {},
  1546. corePlugins: ['float', 'padding', 'margin'],
  1547. })
  1548. })
  1549. test('plugins are merged', () => {
  1550. let p1 = { config: { order: '1' } }
  1551. let p2 = { config: { order: '2' } }
  1552. let p3 = { config: { order: '3' } }
  1553. const userConfig = {
  1554. plugins: [p3],
  1555. }
  1556. const otherConfig = {
  1557. plugins: [p2],
  1558. }
  1559. const defaultConfig = {
  1560. plugins: [p1],
  1561. prefix: '',
  1562. important: false,
  1563. separator: ':',
  1564. content: [],
  1565. theme: {},
  1566. }
  1567. const result = resolveConfig([userConfig, otherConfig, defaultConfig])
  1568. expect(result).toMatchObject({
  1569. prefix: '',
  1570. important: false,
  1571. separator: ':',
  1572. theme: {},
  1573. plugins: [p1, p2, p3],
  1574. })
  1575. })
  1576. test('all helpers can be destructured from the first function argument', () => {
  1577. const userConfig = {
  1578. theme: {
  1579. example: ({ theme, colors, negative, breakpoints }) => ({
  1580. weight: theme('fontWeight.bold'),
  1581. black: colors.black,
  1582. white: colors.white,
  1583. ...negative(theme('spacing')),
  1584. ...breakpoints(theme('screens')),
  1585. }),
  1586. },
  1587. }
  1588. const defaultConfig = {
  1589. prefix: '-',
  1590. important: false,
  1591. separator: ':',
  1592. content: [],
  1593. theme: {
  1594. screens: {
  1595. sm: '640px',
  1596. md: '768px',
  1597. },
  1598. fontWeight: {
  1599. bold: 700,
  1600. },
  1601. spacing: {
  1602. 0: '0px',
  1603. 1: '1px',
  1604. 2: '2px',
  1605. 3: '3px',
  1606. 4: '4px',
  1607. },
  1608. },
  1609. }
  1610. const result = resolveConfig([userConfig, defaultConfig])
  1611. expect(result).toMatchObject({
  1612. prefix: '-',
  1613. important: false,
  1614. separator: ':',
  1615. theme: {
  1616. example: {
  1617. weight: 700,
  1618. black: '#000',
  1619. white: '#fff',
  1620. '-1': '-1px',
  1621. '-2': '-2px',
  1622. '-3': '-3px',
  1623. '-4': '-4px',
  1624. 'screen-sm': '640px',
  1625. 'screen-md': '768px',
  1626. },
  1627. },
  1628. })
  1629. })
  1630. test('does not duplicate extended configs every time resolveConfig is called', () => {
  1631. let shared = {
  1632. foo: { bar: { baz: [{ color: 'red' }] } },
  1633. }
  1634. const createConfig = (color) =>
  1635. resolveConfig([
  1636. {
  1637. theme: {
  1638. foo: shared.foo,
  1639. extend: {
  1640. foo: { bar: { baz: { color } } },
  1641. },
  1642. },
  1643. },
  1644. ])
  1645. createConfig('orange')
  1646. createConfig('yellow')
  1647. createConfig('green')
  1648. const result = createConfig('blue')
  1649. expect(shared.foo.bar.baz).toMatchObject([{ color: 'red' }])
  1650. expect(result.theme.foo.bar.baz).toMatchObject([{ color: 'red' }, { color: 'blue' }])
  1651. })