layer-at-rules.test.js 5.8 KB

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