-
Notifications
You must be signed in to change notification settings - Fork 685
/
Copy pathSwitch.spec.ts
105 lines (96 loc) · 4.14 KB
/
Switch.spec.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
import { describe, it, expect, test } from 'vitest'
import Switch, { type SwitchProps, type SwitchSlots } from '../../src/runtime/components/Switch.vue'
import ComponentRender from '../component-render'
import theme from '#build/ui/switch'
import { flushPromises, mount } from '@vue/test-utils'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
describe('Switch', () => {
const sizes = Object.keys(theme.variants.size) as any
it.each([
// Props
['with modelValue', { props: { modelValue: true } }],
['with defaultValue', { props: { defaultValue: true } }],
['with id', { props: { id: 'id' } }],
['with name', { props: { name: 'name' } }],
['with value', { props: { value: 'value' } }],
['with disabled', { props: { disabled: true } }],
['with checkedIcon', { props: { checkedIcon: 'i-lucide-check', defaultValue: true } }],
['with uncheckedIcon', { props: { uncheckedIcon: 'i-lucide-x' } }],
['with loading', { props: { loading: true } }],
['with loadingIcon', { props: { loading: true, loadingIcon: 'i-lucide-sparkles' } }],
['with label', { props: { label: 'Label' } }],
['with required', { props: { label: 'Label', required: true } }],
['with description', { props: { label: 'Label', description: 'Description' } }],
...sizes.map((size: string) => [`with size ${size}`, { props: { size } }]),
['with color neutral', { props: { color: 'neutral', defaultValue: true } }],
['with ariaLabel', { attrs: { 'aria-label': 'Aria label' } }],
['with as', { props: { as: 'section' } }],
['with class', { props: { class: 'inline-flex' } }],
['with ui', { props: { ui: { wrapper: 'ms-4' } } }],
// Slots
['with label slot', { slots: { label: () => 'Label slot' } }],
['with description slot', { slots: { label: () => 'Description slot' } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: SwitchProps, slots?: Partial<SwitchSlots> }) => {
const html = await ComponentRender(nameOrHtml, options, Switch)
expect(html).toMatchSnapshot()
})
describe('emits', () => {
test('update:modelValue event', async () => {
const wrapper = mount(Switch)
const input = wrapper.findComponent({ name: 'SwitchRoot' })
await input.vm.$emit('update:modelValue', true)
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[true]] })
})
test('change event', async () => {
const wrapper = mount(Switch)
const input = wrapper.findComponent({ name: 'SwitchRoot' })
await input.vm.$emit('update:modelValue', true)
expect(wrapper.emitted()).toMatchObject({ change: [[{ type: 'change' }]] })
})
})
describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
const wrapper = await renderForm({
props: {
validateOn,
validateOnInputDelay: 0,
async validate(state: any) {
if (!state.value)
return [{ name: 'value', message: 'Error message' }]
return []
}
},
slotTemplate: `
<UFormField name="value">
<USwitch v-model="state.value" />
</UFormField>
`
})
const input = wrapper.findComponent({ name: 'SwitchRoot' })
return {
wrapper,
input
}
}
test('validate on change works', async () => {
const { input, wrapper } = await createForm(['change'])
await input.setValue(false)
await input.vm.$emit('update:modelValue', false)
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.vm.$emit('update:modelValue', true)
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('validate on input works', async () => {
const { input, wrapper } = await createForm(['input'])
await input.vm.$emit('update:modelValue', false)
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.vm.$emit('update:modelValue', true)
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
})
})