forked from unocss/unocss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preset-attributify.test.ts
220 lines (190 loc) · 6.02 KB
/
preset-attributify.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { createGenerator, toEscapedSelector as e } from '@unocss/core'
import presetUno from '@unocss/preset-uno'
import { autocompleteExtractorAttributify, presetAttributify, variantAttributify } from '@unocss/preset-attributify'
import { describe, expect, test } from 'vitest'
describe('attributify', async () => {
const uno = createGenerator({
presets: [
presetAttributify({ strict: true }),
presetUno({ attributifyPseudo: true }),
],
rules: [
[/^custom-(\d+)$/, ([_, value], { rawSelector }) => {
// return a string instead of an object
const selector = e(rawSelector)
return `
${selector} {
font-size: ${value}px;
}
/* you can have multiple rules */
${selector}::after {
content: 'after';
}
.foo > ${selector} {
color: red;
}
/* or media queries */
@media (min-width: 680px) {
${selector} {
font-size: 16px;
}
}
`
}],
],
})
describe('cases', () => {
const cases = import.meta.glob('./cases/preset-attributify/*/input.html', { as: 'raw' })
for (const [path, input] of Object.entries(cases)) {
test(path, async () => {
const { css } = await uno.generate(await input(), { preflights: false })
await expect(css).toMatchFileSnapshot(path.replace('input.html', 'output.css'))
})
}
})
const fixture1 = (await import('./cases/preset-attributify/case-1/input.html?raw')).default
test('variant', async () => {
const variant = variantAttributify({
prefix: 'un-',
prefixedOnly: false,
strict: true,
})
const promises = Array.from(await uno.applyExtractors(fixture1) || [])
.map(async (i) => {
const r = await variant.match(i, {} as any)
return typeof r === 'string' ? r : r ? r.matcher : r
})
expect(await Promise.all(promises))
.toMatchSnapshot()
})
test('prefixedOnly', async () => {
const uno = createGenerator({
presets: [
presetAttributify({ strict: true, prefix: 'un-', prefixedOnly: true }),
presetUno({ attributifyPseudo: true }),
],
})
const { css } = await uno.generate(fixture1, { preflights: false })
expect(css).toMatchSnapshot()
})
describe('autocomplete extractor', async () => {
test('without prefix', async () => {
const res = await autocompleteExtractorAttributify().extract({
content: fixture1,
cursor: 187,
})
expect(res).not.toBeNull()
expect(res!.extracted).toMatchInlineSnapshot('"bg-blue-400"')
expect(res!.transformSuggestions!([`${res!.extracted}1`, `${res!.extracted}2`]))
.toMatchInlineSnapshot(`
[
"blue-4001",
"blue-4002",
]
`)
const reversed = res!.resolveReplacement(`${res!.extracted}1`)
expect(reversed).toMatchInlineSnapshot(`
{
"end": 192,
"replacement": "blue-4001",
"start": 184,
}
`)
expect(fixture1.slice(reversed.start, reversed.end))
.toMatchInlineSnapshot('"blue-400"')
})
test('with prefix', async () => {
const fixtureWithPrefix = `
<div un-text-cent>
<div un-text="cent
</div>
`
const res1 = await autocompleteExtractorAttributify({ prefix: 'un-' }).extract({
content: fixtureWithPrefix,
cursor: 18,
})
expect(res1).not.toBeNull()
expect(res1!.extracted).toMatchInlineSnapshot('"text-cent"')
expect(res1!.transformSuggestions!([`${res1!.extracted}1`, `${res1!.extracted}2`]))
.toMatchInlineSnapshot(`
[
"un-text-cent1",
"un-text-cent2",
]
`)
const reversed1 = res1!.resolveReplacement(`${res1!.extracted}1`)
expect(reversed1).toMatchInlineSnapshot(`
{
"end": 18,
"replacement": "text-cent1",
"start": 6,
}
`)
expect(fixtureWithPrefix.slice(reversed1.start, reversed1.end))
.toMatchInlineSnapshot('"un-text-cent"')
const res2 = await autocompleteExtractorAttributify({ prefix: 'un-' }).extract({
content: fixtureWithPrefix,
cursor: 40,
})
expect(res2).not.toBeNull()
expect(res2!.extracted).toMatchInlineSnapshot('"text-cent"')
expect(res2!.transformSuggestions!([`${res2!.extracted}1`, `${res2!.extracted}2`]))
.toMatchInlineSnapshot(`
[
"cent1",
"cent2",
]
`)
const reversed2 = res2!.resolveReplacement(`${res2!.extracted}1`)
expect(reversed2).toMatchInlineSnapshot(`
{
"end": 40,
"replacement": "cent1",
"start": 36,
}
`)
expect(fixtureWithPrefix.slice(reversed2.start, reversed2.end))
.toMatchInlineSnapshot('"cent"')
})
test('only prefix', async () => {
const fixtureOnlyPrefix = `
<div text-cent>
<div text="cent
</div>
`
const res1 = await autocompleteExtractorAttributify({ prefix: 'un-', prefixedOnly: true }).extract({
content: fixtureOnlyPrefix,
cursor: 15,
})
expect(res1).toBeNull()
const res2 = await autocompleteExtractorAttributify({ prefix: 'un-', prefixedOnly: true }).extract({
content: fixtureOnlyPrefix,
cursor: 34,
})
expect(res2).toBeNull()
})
})
test('with trueToNonValued', async () => {
const uno = createGenerator({
presets: [
presetAttributify({ trueToNonValued: true }),
presetUno(),
],
})
const { css } = await uno.generate(`
<div flex="true"></div>
<div grid="~ cols-2"></div>
`, { preflights: false })
expect(css).toMatchSnapshot()
})
test('with incomplete element', async () => {
await uno.generate('<div class="w-fullllllllllllll"')
}, 20)
test('merge attribute name and value-only', async () => {
const { css } = await uno.generate(`
<div bg="[&:nth-child(3)]:[#123456]"></div>
<div class="foo" bg="[&.foo]:[&:nth-child(3)]:[#123]"></div>
`, { preflights: false })
expect(css).toMatchSnapshot()
})
})