forked from unocss/unocss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.test.ts
140 lines (131 loc) · 3.9 KB
/
order.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { createGenerator, movePseudoElementsEnd } from '@unocss/core'
import { describe, expect, test } from 'vitest'
import { variantMatcher } from '@unocss/preset-mini/utils'
import presetMini from '@unocss/preset-mini'
describe('order', () => {
test('static', async () => {
const uno = createGenerator({
rules: [
['foo', { name: 'bar1' }],
['foo', { name: 'bar2' }],
],
presets: [],
})
const { css } = await uno.generate('foo', { preflights: false })
expect(css).toContain('bar2')
})
test('dynamic', async () => {
const uno = createGenerator({
rules: [
[/^foo$/, () => ({ name: 'bar1' })],
[/^foo$/, () => ({ name: 'bar2' })],
],
presets: [],
})
const { css } = await uno.generate('foo', { preflights: false })
expect(css).toContain('bar2')
})
test('variant ordering', async () => {
const uno = createGenerator({
rules: [
[/^foo-.$/, ([m]) => ({ name: m })],
],
presets: [],
variants: [
variantMatcher('light', input => `.light $$ ${input}`),
variantMatcher('group', input => `.group ${input}`),
(v, ctx) => {
const match = variantMatcher('dark', input => `.dark $$ ${input}`).match(v, ctx)
if (typeof match === 'object') {
return {
...match,
order: 1,
}
}
},
],
})
const code = [
'light:group:foo-1',
'group:light:foo-2',
'dark:group:foo-3',
'group:dark:foo-4',
].join(' ')
const { css } = await uno.generate(code, { preflights: false })
const { css: css2 } = await uno.generate(code, { preflights: false })
expect(css).toMatchSnapshot()
expect(css).toEqual(css2)
})
test('multiple variant sorting', async () => {
const uno = createGenerator({
presets: [
presetMini(),
],
})
const { css } = await uno.generate([
'dark:group-hover:group-focus-within:bg-blue-600',
'group-hover:group-focus-within:dark:bg-red-600',
].join(' '), { preflights: false })
expect(css).toMatchSnapshot()
})
test('movePseudoElementsEnd', () => {
expect(movePseudoElementsEnd('.marker\\:file\\:hover\\:selection\\:mb-4::marker::file-selector-button:hover::selection'))
.toMatchSnapshot()
})
test('variant sorting', async () => {
const uno = createGenerator({
rules: [
[/^foo-.$/, ([m]) => ({ foo: m })],
],
presets: [],
variants: [
(input: string) => {
const m = input.match(/^(\w+):/)
if (m) {
return {
matcher: input.slice(m[0].length),
selector: s => `${m[1]} ${s}`,
sort: {
pre: -1,
post: 1,
}[m[1]],
}
}
},
],
})
const code = [
'any:foo-1',
'post:foo-1',
'pre:foo-1',
'zzz:foo-1',
].join(' ')
const { css } = await uno.generate(code, { preflights: false })
const { css: css2 } = await uno.generate(code, { preflights: false })
expect(css).toMatchSnapshot()
expect(css).toEqual(css2)
})
test('fully controlled rules merged and sorted by body', async () => {
const uno = createGenerator({
rules: [
['uno', { '--var': 'uno' }],
[/^foo-(.+)$/, ([, match]) => {
return `/* sort: ${match} */ .foo{--foo:0}`
}],
[/^bar-(.+)$/, ([, match]) => {
return [
`/* sort: ${match} */ .foo{--foo:0}`,
{ '--bar': match },
]
}],
['css', { '--var': 'css' }],
],
presets: [],
})
const code = 'foo-uno uno css bar-uno bar-css foo-css'
const { css } = await uno.generate(code, { preflights: false })
const { css: css2 } = await uno.generate(code, { preflights: false })
expect(css).toMatchSnapshot()
expect(css).toEqual(css2)
})
})