|
| 1 | +import { describe, test } from 'vitest' |
| 2 | + |
| 3 | +import { emitModule } from '../src/emit' |
| 4 | + |
| 5 | +import { css, expectMerges, generateFixture, importEmittedModule } from './fixture-utils' |
| 6 | + |
| 7 | +// These independent CSS longhands provide a small, explicit oracle: a later class can replace an earlier one only when it sets all of the earlier class's properties. Do not use production coverage helpers to derive the expected results. |
| 8 | +const properties = ['width', 'height', 'color'] |
| 9 | +const utilities = Array.from({ length: 7 }, (_, index) => index + 1).flatMap((mask) => |
| 10 | + ['a', 'b'].map((value) => ({ name: `effect-${mask}-${value}`, mask, value })), |
| 11 | +) |
| 12 | +const rows = utilities.flatMap((first) => utilities.flatMap((second) => { |
| 13 | + const third = utilities[(first.mask + second.mask) % utilities.length]! |
| 14 | + return [[first, second], [first, second, third]].map((list): [string, string] => [ |
| 15 | + list.map(({ name }) => name).join(' '), |
| 16 | + list.filter((item, index) => !list.slice(index + 1).some((later) => |
| 17 | + (later.mask & item.mask) === item.mask, |
| 18 | + )).map(({ name }) => name).join(' '), |
| 19 | + ]) |
| 20 | +})) |
| 21 | + |
| 22 | +describe.each(['compact', 'exact'] as const)('%s effect combinations', (encoding) => { |
| 23 | + test.each(['', 'tw'])('keeps coverage and lookup decisions across representations (prefix: %s)', async (prefix) => { |
| 24 | + const stylesheet = css` |
| 25 | + @import 'tailwindcss' ${prefix ? 'prefix(tw)' : ''}; |
| 26 | + @custom-variant children (& > *); |
| 27 | + @theme { --color-x-13: red; } |
| 28 | + ${utilities.map(({ name, mask, value }) => `@utility ${name} { |
| 29 | + ${properties.flatMap((property, index) => mask & (1 << index) |
| 30 | + ? [`${property}: ${property === 'color' ? (value === 'a' ? 'red' : 'blue') : (value === 'a' ? '1px' : '2px')};`] |
| 31 | + : []).join('\n')} |
| 32 | + }`).join('\n')} |
| 33 | + @utility thing-* { opacity: --value(number); } |
| 34 | + @utility thing-width-* { |
| 35 | + width: calc(--value(integer) * 1px); |
| 36 | + height: --modifier([length]); |
| 37 | + } |
| 38 | + @utility badge { padding: 1rem; } |
| 39 | + @utility badge/icon { color: red; } |
| 40 | + @utility pull { margin-left: 1rem; } |
| 41 | + @utility -pull { margin-right: -1rem; } |
| 42 | + ` |
| 43 | + const preserved = [ |
| 44 | + 'thing-width-2/[3px] thing-0.5 effect-4-a', |
| 45 | + 'thing-0.5 thing-width-2/[3px] effect-4-a', |
| 46 | + 'badge/icon badge', |
| 47 | + 'pull -pull', |
| 48 | + '-pull pull', |
| 49 | + 'border-x-13 border-blue-500', |
| 50 | + 'hover:children:effect-7-a children:hover:effect-7-b', |
| 51 | + 'effect-7-a! effect-7-b', |
| 52 | + ] |
| 53 | + const cases = [ |
| 54 | + ...rows.flatMap((row) => ['', 'hover:', '!'].map((modifier) => mapCase(row, |
| 55 | + (name) => modifier === '!' ? `${name}!` : `${modifier}${name}`, |
| 56 | + ))), |
| 57 | + ...preserved.map((input): [string, string] => [input, input]), |
| 58 | + ].map((row) => mapCase(row, (name) => prefix ? `${prefix}:${name}` : name)) |
| 59 | + const usedClasses = [...new Set(cases.flatMap(([input]) => input.split(' ')))] |
| 60 | + for (const prune of [undefined, { usedClasses }]) { |
| 61 | + const fixture = await generateFixture(stylesheet, undefined, { encoding, prune }) |
| 62 | + expectMerges(fixture.twMerge, cases) |
| 63 | + for (const format of ['ts', 'js'] as const) { |
| 64 | + const emitted = await importEmittedModule(emitModule(fixture.plan, { format }), format) |
| 65 | + expectMerges(emitted.twMerge, cases) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +/** Decorate both sides of an independently specified case without deriving its expected merge result from runtime behavior. */ |
| 72 | +function mapCase([input, expected]: [string, string], decorate: (name: string) => string): [string, string] { |
| 73 | + const apply = (list: string) => list.split(' ').map(decorate).join(' ') |
| 74 | + return [apply(input), apply(expected)] |
| 75 | +} |
0 commit comments