-
Notifications
You must be signed in to change notification settings - Fork 688
/
Copy pathSlider.spec.ts
104 lines (90 loc) · 3.66 KB
/
Slider.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
import { describe, it, expect, test } from 'vitest'
import Slider, { type SliderProps } from '../../src/runtime/components/Slider.vue'
import ComponentRender from '../component-render'
import theme from '#build/ui/slider'
import { flushPromises, mount } from '@vue/test-utils'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
describe('Slider', () => {
const sizes = Object.keys(theme.variants.size) as any
it.each([
// Props
['with modelValue', { props: { modelValue: 10 } }],
['with defaultValue', { props: { defaultValue: 10 } }],
['with multiple thumbs', { props: { defaultValue: [0, 10] } }],
['with name', { props: { name: 'custom-name' } }],
['with disabled', { props: { disabled: true } }],
['with inverted', { props: { inverted: true } }],
['with orientation vertical', { props: { orientation: 'vertical' as const } }],
['with min max step', { props: { min: 4, max: 12, step: 2 } }],
['with min steps between thumbs', { props: { defaultValue: [0, 30], minStepsBetweenThumbs: 30 } }],
...sizes.map((size: string) => [`with size ${size}`, { props: { size } }]),
['with color neutral', { props: { color: 'neutral', defaultValue: 10 } }],
['with ariaLabel', { attrs: { 'aria-label': 'Aria label' } }],
['with as', { props: { as: 'section' } }],
['with class', { props: { class: 'w-48' } }],
['with ui', { props: { ui: { track: 'bg-(--ui-bg-elevated)' } } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: SliderProps }) => {
const html = await ComponentRender(nameOrHtml, options, Slider)
expect(html).toMatchSnapshot()
})
describe('emits', () => {
test('update:modelValue event', async () => {
const wrapper = mount(Slider)
const input = wrapper.findComponent({ name: 'SliderRoot' })
input.vm.$emit('update:modelValue', 1)
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[1], [1]] })
})
test('change event', async () => {
const wrapper = mount(Slider)
const input = wrapper.findComponent({ name: 'SliderRoot' })
input.vm.$emit('valueCommit')
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 < 20)
return [{ name: 'value', message: 'Error message' }]
return []
}
},
slotTemplate: `
<UFormField name="value">
<USlider v-model="state.value" />
</UFormField>
`
})
const input = wrapper.findComponent({ name: 'SliderRoot' })
return {
wrapper,
input
}
}
test('validate on change works', async () => {
const { input, wrapper } = await createForm(['change'])
await input.setValue(10)
input.vm.$emit('valueCommit')
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.setValue(40)
input.vm.$emit('valueCommit')
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('validate on input works', async () => {
const { input, wrapper } = await createForm(['input'])
await input.setValue(10)
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.setValue(40)
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
})
})