layer-at-rules.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import { run, html, css, defaults } from './util/run'
  2. test('custom user-land utilities', () => {
  3. let config = {
  4. content: [
  5. {
  6. raw: html`<div
  7. class="focus:hover:align-chocolate align-banana hover:align-banana uppercase"
  8. ></div>`,
  9. },
  10. ],
  11. corePlugins: { preflight: false },
  12. theme: {},
  13. plugins: [],
  14. }
  15. let input = css`
  16. @layer utilities {
  17. .align-banana {
  18. text-align: banana;
  19. }
  20. }
  21. @tailwind base;
  22. @tailwind components;
  23. @tailwind utilities;
  24. @layer utilities {
  25. .align-chocolate {
  26. text-align: chocolate;
  27. }
  28. }
  29. `
  30. return run(input, config).then((result) => {
  31. expect(result.css).toMatchFormattedCss(css`
  32. ${defaults}
  33. .uppercase {
  34. text-transform: uppercase;
  35. }
  36. .align-banana {
  37. text-align: banana;
  38. }
  39. .hover\:align-banana:hover {
  40. text-align: banana;
  41. }
  42. .focus\:hover\:align-chocolate:hover:focus {
  43. text-align: chocolate;
  44. }
  45. `)
  46. })
  47. })
  48. test('comments can be used inside layers without crashing', () => {
  49. let config = {
  50. content: [
  51. {
  52. raw: html`<div class="important-utility important-component"></div>`,
  53. },
  54. ],
  55. corePlugins: { preflight: false },
  56. theme: {},
  57. plugins: [],
  58. }
  59. let input = css`
  60. @tailwind base;
  61. @tailwind components;
  62. @tailwind utilities;
  63. @layer base {
  64. /* Important base */
  65. div {
  66. background-color: #bada55;
  67. }
  68. }
  69. @layer utilities {
  70. /* Important utility */
  71. .important-utility {
  72. text-align: banana;
  73. }
  74. }
  75. @layer components {
  76. /* Important component */
  77. .important-component {
  78. text-align: banana;
  79. }
  80. }
  81. `
  82. return run(input, config).then((result) => {
  83. expect(result.css).toMatchFormattedCss(css`
  84. /* Important base */
  85. div {
  86. background-color: #bada55;
  87. }
  88. ${defaults}
  89. /* Important component */
  90. .important-component {
  91. text-align: banana;
  92. }
  93. /* Important utility */
  94. .important-utility {
  95. text-align: banana;
  96. }
  97. `)
  98. })
  99. })
  100. test('comments can be used inside layers (with important) without crashing', () => {
  101. let config = {
  102. important: true,
  103. content: [
  104. {
  105. raw: html`<div class="important-utility important-component"></div>`,
  106. },
  107. ],
  108. corePlugins: { preflight: false },
  109. theme: {},
  110. plugins: [],
  111. }
  112. let input = css`
  113. @tailwind base;
  114. @tailwind components;
  115. @tailwind utilities;
  116. @layer base {
  117. /* Important base */
  118. div {
  119. background-color: #bada55;
  120. }
  121. }
  122. @layer utilities {
  123. /* Important utility */
  124. .important-utility {
  125. text-align: banana;
  126. }
  127. }
  128. @layer components {
  129. /* Important component */
  130. .important-component {
  131. text-align: banana;
  132. }
  133. }
  134. `
  135. return run(input, config).then((result) => {
  136. expect(result.css).toMatchFormattedCss(css`
  137. /* Important base */
  138. div {
  139. background-color: #bada55;
  140. }
  141. ${defaults}
  142. /* Important component */
  143. .important-component {
  144. text-align: banana;
  145. }
  146. /* Important utility */
  147. .important-utility {
  148. text-align: banana !important;
  149. }
  150. `)
  151. })
  152. })
  153. test('layers are grouped and inserted at the matching @tailwind rule', () => {
  154. let config = {
  155. content: [
  156. { raw: html`<div class="input btn card float-squirrel align-banana align-sandwich"></div>` },
  157. ],
  158. plugins: [
  159. function ({ addBase, addComponents, addUtilities }) {
  160. addBase({ body: { margin: 0 } })
  161. addComponents({
  162. '.input': { background: 'white' },
  163. })
  164. addUtilities({
  165. '.float-squirrel': { float: 'squirrel' },
  166. })
  167. },
  168. ],
  169. corePlugins: { preflight: false },
  170. }
  171. let input = css`
  172. @layer vanilla {
  173. strong {
  174. font-weight: medium;
  175. }
  176. }
  177. @tailwind base;
  178. @tailwind components;
  179. @tailwind utilities;
  180. @layer components {
  181. .btn {
  182. background: blue;
  183. }
  184. }
  185. @layer utilities {
  186. .align-banana {
  187. text-align: banana;
  188. }
  189. }
  190. @layer base {
  191. h1 {
  192. font-weight: bold;
  193. }
  194. }
  195. @layer components {
  196. .card {
  197. border-radius: 12px;
  198. }
  199. }
  200. @layer base {
  201. p {
  202. font-weight: normal;
  203. }
  204. }
  205. @layer utilities {
  206. .align-sandwich {
  207. text-align: sandwich;
  208. }
  209. }
  210. @layer chocolate {
  211. a {
  212. text-decoration: underline;
  213. }
  214. }
  215. `
  216. expect.assertions(2)
  217. return run(input, config).then((result) => {
  218. expect(result.warnings().length).toBe(0)
  219. expect(result.css).toMatchFormattedCss(css`
  220. @layer vanilla {
  221. strong {
  222. font-weight: medium;
  223. }
  224. }
  225. body {
  226. margin: 0;
  227. }
  228. h1 {
  229. font-weight: bold;
  230. }
  231. p {
  232. font-weight: normal;
  233. }
  234. ${defaults}
  235. .input {
  236. background: white;
  237. }
  238. .btn {
  239. background: blue;
  240. }
  241. .card {
  242. border-radius: 12px;
  243. }
  244. .float-squirrel {
  245. float: squirrel;
  246. }
  247. .align-banana {
  248. text-align: banana;
  249. }
  250. .align-sandwich {
  251. text-align: sandwich;
  252. }
  253. @layer chocolate {
  254. a {
  255. text-decoration: underline;
  256. }
  257. }
  258. `)
  259. })
  260. })
  261. it('should keep `@supports` rules inside `@layer`s', () => {
  262. let config = {
  263. content: [{ raw: html`<div class="test"></div>` }],
  264. plugins: [],
  265. }
  266. let input = css`
  267. @tailwind utilities;
  268. @layer utilities {
  269. .test {
  270. --tw-test: 1;
  271. }
  272. @supports (backdrop-filter: blur(1px)) {
  273. .test {
  274. --tw-test: 0.9;
  275. }
  276. }
  277. }
  278. `
  279. return run(input, config).then((result) => {
  280. return expect(result.css).toMatchFormattedCss(css`
  281. .test {
  282. --tw-test: 1;
  283. }
  284. @supports (backdrop-filter: blur(1px)) {
  285. .test {
  286. --tw-test: 0.9;
  287. }
  288. }
  289. `)
  290. })
  291. })