-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSelect.spec.ts
More file actions
311 lines (276 loc) · 11.7 KB
/
Copy pathSelect.spec.ts
File metadata and controls
311 lines (276 loc) · 11.7 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { describe, it, expect, test } from 'vitest'
import { axe } from 'vitest-axe'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { renderEach } from '../component-render'
import { flushPromises, mount } from '@vue/test-utils'
import Select from '../../src/runtime/components/Select.vue'
import theme from '#build/ui/input'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '../../src/module'
import { expectEmitPayloadType } from '../utils/types'
describe('Select', () => {
const sizes = Object.keys(theme.variants.size) as any
const variants = Object.keys(theme.variants.variant) as any
const items = [{
label: 'Backlog',
value: 'backlog',
icon: 'i-lucide-circle-help'
}, {
label: 'Todo',
value: 'todo',
icon: 'i-lucide-circle-plus'
}, {
label: 'In Progress',
value: 'in_progress',
icon: 'i-lucide-circle-arrow-up'
}, {
label: 'Done',
value: 'done',
icon: 'i-lucide-circle-check'
}, {
label: 'Canceled',
value: 'canceled',
icon: 'i-lucide-circle-x'
}]
const itemsWithDescription = [...items.map(item => ({ ...item, description: 'Description' }))]
const itemsWithBadge = [
...items.map((item, i) => ({
...item,
badge: i % 2 === 0 ? 'Badge' : { color: 'primary', variant: 'solid', size: 'sm', label: 'Badge' }
}))
]
const props = { open: true, portal: false, items }
renderEach(Select, [
// Props
['with items', { props }],
['with items with description', { props: { ...props, items: itemsWithDescription } }],
['with items with badge', { props: { ...props, items: itemsWithBadge } }],
['with modelValue', { props: { ...props, modelValue: items[0]?.value } }],
['with defaultValue', { props: { ...props, defaultValue: items[0]?.value } }],
['with valueKey', { props: { ...props, valueKey: 'label', defaultValue: 'Backlog' } }],
['with labelKey', { props: { ...props, labelKey: 'value' } }],
['with descriptionKey', { props: { ...props, descriptionKey: 'description' } }],
['with multiple', { props: { ...props, multiple: true } }],
['with multiple and modelValue', { props: { ...props, multiple: true, modelValue: [items[0], items[1]] } }],
['with id', { props: { ...props, id: 'id' } }],
['with name', { props: { ...props, name: 'name' } }],
['with placeholder', { props: { ...props, placeholder: 'Search...' } }],
['with disabled', { props: { ...props, disabled: true } }],
['with required', { props: { ...props, required: true } }],
['with icon', { props: { icon: 'i-lucide-search' } }],
['with leading and icon', { props: { leading: true, icon: 'i-lucide-arrow-left' } }],
['with leadingIcon', { props: { leadingIcon: 'i-lucide-arrow-left' } }],
['with trailing and icon', { props: { trailing: true, icon: 'i-lucide-arrow-right' } }],
['with trailingIcon', { props: { trailingIcon: 'i-lucide-arrow-right' } }],
['with avatar', { props: { avatar: { src: 'https://github.com/benjamincanac.png' } } }],
['with avatar and leadingIcon', { props: { avatar: { src: 'https://github.com/benjamincanac.png' }, leadingIcon: 'i-lucide-arrow-left' } }],
['with avatar and trailingIcon', { props: { avatar: { src: 'https://github.com/benjamincanac.png' }, trailingIcon: 'i-lucide-arrow-right' } }],
['with loading', { props: { loading: true } }],
['with loading and avatar', { props: { loading: true, avatar: { src: 'https://github.com/benjamincanac.png' } } }],
['with loading trailing', { props: { loading: true, trailing: true } }],
['with loading trailing and avatar', { props: { loading: true, trailing: true, avatar: { src: 'https://github.com/benjamincanac.png' } } }],
['with loadingIcon', { props: { loading: true, loadingIcon: 'i-lucide-loader' } }],
['with trailingIcon', { props: { ...props, trailingIcon: 'i-lucide-chevron-down' } }],
['with selectedIcon', { props: { ...props, selectedIcon: 'i-lucide-check' } }],
['with arrow', { props: { ...props, arrow: true } }],
...sizes.map((size: string) => [`with size ${size}`, { props: { ...props, size } }]),
...variants.map((variant: string) => [`with primary variant ${variant}`, { props: { ...props, variant } }]),
...variants.map((variant: string) => [`with neutral variant ${variant}`, { props: { ...props, variant, color: 'neutral' } }]),
['with ariaLabel', { props, attrs: { 'aria-label': 'Aria label' } }],
['with class', { props: { ...props, class: 'rounded-full' } }],
['with ui', { props: { ...props, ui: { group: 'p-2' } } }],
// Slots
['with leading slot', { props, slots: { leading: () => 'Leading slot' } }],
['with trailing slot', { props, slots: { trailing: () => 'Trailing slot' } }],
['with item slot', { props, slots: { item: () => 'Item slot' } }],
['with item-leading slot', { props, slots: { 'item-leading': () => 'Item leading slot' } }],
['with item-label slot', { props, slots: { 'item-label': () => 'Item label slot' } }],
['with item-description slot', { props: { ...props, items: itemsWithDescription }, slots: { 'item-description': () => 'Item description slot' } }],
['with item-trailing slot', { props, slots: { 'item-trailing': () => 'Item trailing slot' } }]
])
renderEach(
Select,
[
['with .trim modifier', { props: { modelModifiers: { trim: true } } }, { input: 'input ', expected: 'input' }],
['with .number modifier', { props: { modelModifiers: { number: true } } }, { input: '42', expected: 42 }],
['with .nullable modifier', { props: { modelModifiers: { nullable: true } } }, { input: null, expected: null }],
['with .optional modifier', { props: { modelModifiers: { optional: true } } }, { input: undefined, expected: undefined }]
],
'%s works',
async (_, options, spec) => {
const wrapper = mount(Select, {
...options
})
const select = wrapper.findComponent({ name: 'SelectRoot' })
await select.setValue(spec.input)
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[spec.expected]] })
}
)
it('passes accessibility tests', async () => {
const wrapper = await mountSuspended(Select, {
props: {
...props,
modelValue: items[0]?.value,
required: true,
avatar: {
src: 'https://github.com/benjamincanac.png',
alt: 'Benjamin Canac'
}
},
attrs: {
'aria-label': 'Select an item'
}
})
expect(await axe(wrapper.element)).toHaveNoViolations()
})
describe('it should display correct label', () => {
test.each([null, undefined, ''])('falsy model value %s should display placeholder', (modelValue) => {
const wrapper = mount(Select, {
props: {
items,
modelValue,
placeholder: 'Select an item'
}
})
expect(wrapper.text()).toBe('Select an item')
})
test('with string array and string value', () => {
const wrapper = mount(Select, {
props: {
items: ['Apple', 'Banana', 'Cherry'],
modelValue: 'Banana'
}
})
expect(wrapper.text()).toBe('Banana')
})
test('with multiple and empty array value should display placeholder', () => {
const wrapper = mount(Select, {
props: {
items,
multiple: true,
modelValue: [],
placeholder: 'Select items'
}
})
expect(wrapper.text()).toBe('Select items')
})
test('with falsy modelValue and options items contain falsy', () => {
const wrapper = mount(Select, {
props: {
items: [
{
label: 'John Doe',
value: null
},
{
label: 'John Lennon',
value: 1
}
],
valueKey: 'value',
modelValue: null
}
})
expect(wrapper.text()).toBe('John Doe')
})
})
describe('emits', () => {
test('update:modelValue event', async () => {
const wrapper = mount(Select, { props: { items: ['Option 1', 'Option 2'] } })
const input = wrapper.findComponent({ name: 'SelectRoot' })
await input.setValue('Option 1')
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [['Option 1']] })
})
test('change event', async () => {
const wrapper = mount(Select, { props: { items: ['Option 1', 'Option 2'] } })
const input = wrapper.findComponent({ name: 'SelectRoot' })
await input.setValue('Option 1')
expect(wrapper.emitted()).toMatchObject({ change: [[{ type: 'change' }]] })
})
test('blur event', async () => {
const wrapper = mount(Select, { props: { items: ['Option 1', 'Option 2'] } })
const input = wrapper.findComponent({ name: 'SelectRoot' })
await input.vm.$emit('update:open', false)
expect(wrapper.emitted()).toMatchObject({ blur: [[{ type: 'blur' }]] })
})
})
describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
const wrapper = await renderForm({
props: {
validateOn,
validateOnInputDelay: 0,
async validate(state: any) {
if (state.value !== 'Option 2')
return [{ name: 'value', message: 'Error message' }]
return []
}
},
slotVars: {
items: ['Option 1', 'Option 2']
},
slotTemplate: `
<UFormField name="value">
<USelect id="input" v-model="state.value" :items="items" />
</UFormField>
`
})
const input = wrapper.findComponent({ name: 'SelectRoot' })
return {
wrapper,
input
}
}
test('validate on blur works', async () => {
const { input, wrapper } = await createForm(['blur'])
await input.vm.$emit('update:open', false)
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.setValue('Option 2')
await input.vm.$emit('update:open', false)
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('validate on change works', async () => {
const { input, wrapper } = await createForm(['change'])
input.setValue('Option 1')
await flushPromises()
expect(wrapper.text()).toContain('Error message')
input.setValue('Option 2')
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('validate on input works', async () => {
const { input, wrapper } = await createForm(['input'])
input.setValue('Option 1')
await flushPromises()
expect(wrapper.text()).toContain('Error message')
input.setValue('Option 2')
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('should have the correct types', () => {
// with object item
expectEmitPayloadType('update:modelValue', () => Select({
items: [{ label: 'foo', value: 'bar' }]
})).toEqualTypeOf<[string]>()
// with string item
expectEmitPayloadType('update:modelValue', () => Select({
items: ['foo']
})).toEqualTypeOf<[string]>()
// with groups
expectEmitPayloadType('update:modelValue', () => Select({
items: [['foo']]
})).toEqualTypeOf<[string]>()
// with groups and mixed types
expectEmitPayloadType('update:modelValue', () => Select({
items: [['foo', { value: 1 }], [{ value: 'bar' }, 2]]
})).toEqualTypeOf<[string | number]>()
// with groups, multiple, mixed types and valueKey
expectEmitPayloadType('update:modelValue', () => Select({
items: [['foo', { value: 1 }], [{ value: 'bar' }, 2]],
valueKey: 'value' // TODO: value is already the default valueKey
})).toEqualTypeOf<[string | number]>()
})
})
})