|
| 1 | +import { __unstable__loadDesignSystem } from '@tailwindcss/node' |
| 2 | +import dedent from 'dedent' |
| 3 | +import { expect, test } from 'vitest' |
| 4 | +import { variantOrder } from './variant-order' |
| 5 | + |
| 6 | +let css = dedent |
| 7 | + |
| 8 | +test.each([ |
| 9 | + // Does nothing unless there are at least two variants |
| 10 | + ['flex', 'flex'], |
| 11 | + ['hover:flex', 'hover:flex'], |
| 12 | + ['[color:red]', '[color:red]'], |
| 13 | + ['[&:focus]:[color:red]', '[&:focus]:[color:red]'], |
| 14 | + |
| 15 | + // Reorders simple variants that include combinators |
| 16 | + ['*:first:flex', 'first:*:flex'], |
| 17 | + |
| 18 | + // Does not reorder variants without combinators |
| 19 | + ['data-[invalid]:data-[hover]:flex', 'data-[invalid]:data-[hover]:flex'], |
| 20 | + |
| 21 | + // Does not reorder some known combinations where the order does not matter |
| 22 | + ['hover:focus:flex', 'hover:focus:flex'], |
| 23 | + ['focus:hover:flex', 'focus:hover:flex'], |
| 24 | + ['[&:hover]:[&:focus]:flex', '[&:hover]:[&:focus]:flex'], |
| 25 | + ['[&:focus]:[&:hover]:flex', '[&:focus]:[&:hover]:flex'], |
| 26 | + ['data-[a]:data-[b]:flex', 'data-[a]:data-[b]:flex'], |
| 27 | + |
| 28 | + // Handles pseudo-elements that cannot have anything after them |
| 29 | + // c.f. https://github.com/tailwindlabs/tailwindcss/pull/13478/files#diff-7779a0eebf6b980dd3abd63b39729b3023cf9a31c91594f5a25ea020b066e1c0 |
| 30 | + ['dark:before:flex', 'dark:before:flex'], |
| 31 | + ['before:dark:flex', 'dark:before:flex'], |
| 32 | + |
| 33 | + // Puts some pseudo-elements that must appear at the end of the selector at |
| 34 | + // the end of the candidate |
| 35 | + ['dark:*:before:after:flex', 'dark:*:before:after:flex'], |
| 36 | + ['dark:before:after:*:flex', 'dark:*:before:after:flex'], |
| 37 | + |
| 38 | + // Some pseudo-elements are treated as regular variants |
| 39 | + ['dark:*:hover:file:focus:underline', 'dark:focus:file:hover:*:underline'], |
| 40 | + |
| 41 | + // Keeps at-rule-variants and the dark variant in the beginning and keeps their |
| 42 | + // order |
| 43 | + ['sm:dark:hover:flex', 'sm:dark:hover:flex'], |
| 44 | + ['[@media(print)]:group-hover:flex', '[@media(print)]:group-hover:flex'], |
| 45 | + ['sm:max-xl:data-[a]:data-[b]:dark:hover:flex', 'sm:max-xl:dark:data-[a]:data-[b]:hover:flex'], |
| 46 | + [ |
| 47 | + 'sm:data-[root]:*:data-[a]:even:*:data-[b]:even:before:underline', |
| 48 | + 'sm:even:data-[b]:*:even:data-[a]:*:data-[root]:before:underline', |
| 49 | + ], |
| 50 | + ['hover:[@supports(display:grid)]:flex', '[@supports(display:grid)]:hover:flex'], |
| 51 | +])('%s => %s', async (candidate, result) => { |
| 52 | + let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { |
| 53 | + base: __dirname, |
| 54 | + }) |
| 55 | + |
| 56 | + expect(variantOrder(designSystem, {}, candidate)).toEqual(result) |
| 57 | +}) |
| 58 | + |
| 59 | +test('it works with custom variants', async () => { |
| 60 | + let designSystem = await __unstable__loadDesignSystem( |
| 61 | + css` |
| 62 | + @import 'tailwindcss'; |
| 63 | + @variant atrule { |
| 64 | + @media (print) { |
| 65 | + @slot; |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + @variant combinator { |
| 70 | + > * { |
| 71 | + @slot; |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + @variant pseudo { |
| 76 | + &::before { |
| 77 | + @slot; |
| 78 | + } |
| 79 | + } |
| 80 | + `, |
| 81 | + { |
| 82 | + base: __dirname, |
| 83 | + }, |
| 84 | + ) |
| 85 | + |
| 86 | + expect(variantOrder(designSystem, {}, 'combinator:pseudo:atrule:underline')).toEqual( |
| 87 | + 'atrule:combinator:pseudo:underline', |
| 88 | + ) |
| 89 | +}) |
0 commit comments