forked from unocss/unocss
/
autocomplete.test.ts
230 lines (205 loc) · 5.21 KB
/
autocomplete.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
221
222
223
224
225
226
227
228
229
230
import { createGenerator } from '@unocss/core'
import presetUno from '@unocss/preset-uno'
import { describe, expect, it } from 'vitest'
import { createAutocomplete, parseAutocomplete } from '@unocss/autocomplete'
import presetAttributify from '@unocss/preset-attributify'
describe('autocomplete', () => {
const uno = createGenerator({
presets: [
presetAttributify(),
presetUno(),
],
shortcuts: [
{
'foo': 'text-red',
'foo-bar': 'text-red',
},
[/^bg-mode-(.+)$/, ([, mode]) => `bg-blend-${mode}`, { autocomplete: ['bg-mode-(color|normal)'] }],
],
})
const ac = createAutocomplete(uno)
async function enumerateSuggestions(inputs: string[]) {
return Object.fromEntries(await Promise.all(inputs.map(async input => [
input,
(await ac.suggest(input)).slice(0, 10).join(' '),
])))
}
const fixture = `
<div bg="blue-500">
<div border="~ b-
</div>
`
it('should resolve autocomplete config', () => {
expect(ac.templates.length).toBeGreaterThan(0)
ac.templates.forEach((i) => {
if (typeof i === 'string')
parseAutocomplete(i, uno.config.theme)
})
})
it('should work', async () => {
expect((await ac.suggest('m-1'))[0])
.toMatchInlineSnapshot('"m-1"')
expect((await ac.suggest('invalid'))[0])
.not.toBe('invalid')
})
it('should provide autocomplete', async () => {
expect(
await enumerateSuggestions([
// sort this list in alphabetical order
'align-',
'aspect-',
'bg-',
'bg-gradient-',
'bg-r',
'border',
'border-',
'border-r',
'border-spacing-',
'columns-',
'divide-',
'fill-',
'filter-',
'fle',
'font-',
'font-',
'keyframes-',
'leading-',
'line-clamp-',
'm-',
'max-w-',
'mx-',
'object-',
'origin-',
'outline-',
'outline-offset-',
'placeholder-',
'scroll-',
'scroll-m-',
'shadow-',
'space-',
'text-r',
'text-red-',
'touch-',
'transition-',
'transform-scale-z-',
'transform-scale-x-',
'transform-scale-',
'transform-skew-x-',
'transform-skew-y-',
'grid-row',
'grid-row-start-',
'grid-row-end-',
'grid-rows-',
'grid-auto-flow-',
'grid-flow-',
'auto-flow-',
'grid-auto-rows-',
'v-',
'w-',
'z-',
]),
).toMatchSnapshot()
})
it('should provide skip DEFAULT', async () => {
expect((await ac.suggest('text-red-')))
.toMatchSnapshot()
})
it('should provide variants', async () => {
expect(await ac.suggest('lt-'))
.toMatchInlineSnapshot(`
[
"lt-2xl:",
"lt-lg:",
"lt-md:",
"lt-sm:",
"lt-xl:",
]
`)
})
it('should accept variants', async () => {
expect(await ac.suggest('dark:md:m-'))
.toMatchSnapshot()
})
it('should skip single-pass variants', async () => {
expect(await ac.suggest('dark:dar')).not.toContain('dark:')
expect(await ac.suggest('active:fir')).toContain('active:first:')
})
it('should support extractors', async () => {
const res = await ac.suggestInFile(fixture, 40)
expect(res.suggestions.every(i => i[0].startsWith('border-'))).toBeTruthy()
expect(res.suggestions.some(i => i[1].startsWith('border-'))).toBeFalsy()
const replacement = res.resolveReplacement(res.suggestions[0][0])
expect(replacement).toMatchInlineSnapshot(`
{
"end": 40,
"replacement": "b-0",
"start": 38,
}
`)
expect(fixture.slice(0, replacement.start) + replacement.replacement + fixture.slice(replacement.end))
.toMatchInlineSnapshot(`
"
<div bg=\\"blue-500\\">
<div border=\\"~ b-0
</div>
"
`)
})
it('should escape regex', async () => {
expect((await ac.suggest('m-['))[0])
.toMatchInlineSnapshot('undefined')
})
it('should suggest static shortcuts', async () => {
expect(await ac.suggest('foo'))
.toMatchInlineSnapshot(`
[
"foo",
"foo-bar",
]
`)
})
it('should suggest shortcuts with autocomplete key', async () => {
expect(await ac.suggest('bg-mode-'))
.toMatchInlineSnapshot(`
[
"bg-mode-color",
"bg-mode-normal",
]
`)
})
})
describe('use uno cache', () => {
const uno = createGenerator({
presets: [
presetUno(),
],
shortcuts: [
{
'foo': 'text-red',
'foo-bar': 'text-red',
},
[/^btn-(red|green)$/, m => `text-${m[1]}`],
],
})
const ac = createAutocomplete(uno)
it('use cache', async () => {
expect(await ac.suggest('btn'))
.toMatchInlineSnapshot('[]')
await uno.generate('btn-red btn-green m-100', { preflights: false })
ac.reset()
expect(await ac.suggest('btn'))
.toMatchInlineSnapshot(`
[
"btn-green",
"btn-red",
]
`)
expect(await ac.suggest('m-1'))
.toMatchInlineSnapshot(`
[
"m-1",
"m-100",
]
`)
})
})