-
Notifications
You must be signed in to change notification settings - Fork 697
/
Copy pathPinInput.spec.ts
131 lines (114 loc) · 5.16 KB
/
PinInput.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
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
import { describe, it, expect, test } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import PinInput, { type PinInputProps } from '../../src/runtime/components/PinInput.vue'
import ComponentRender from '../component-render'
import theme from '#build/ui/pin-input'
import { renderForm } from '../utils/form'
import type { FormInputEvents } from '~/src/module'
describe('PinInput', () => {
const sizes = Object.keys(theme.variants.size) as any
const variants = Object.keys(theme.variants.variant) as any
it.each([
// Props
['with modelValue', { props: { modelValue: ['1'] } }],
['with defaultValue', { props: { defaultValue: ['1'] } }],
['with id', { props: { id: 'pin-input-id' } }],
['with name', { props: { name: 'pin-input-name' } }],
['with type', { props: { type: 'number' } }],
['with placeholder', { props: { placeholder: '*' } }],
['with length', { props: { length: 6 } }],
['with disabled', { props: { disabled: true } }],
['with required', { props: { required: true } }],
['with mask', { props: { mask: true } }],
['with otp', { props: { otp: true } }],
...sizes.map((size: string) => [`with size ${size}`, { props: { size } }]),
...variants.map((variant: string) => [`with primary variant ${variant}`, { props: { variant } }]),
...variants.map((variant: string) => [`with primary variant ${variant} highlight`, { props: { variant, highlight: true } }]),
...variants.map((variant: string) => [`with neutral variant ${variant}`, { props: { variant, color: 'neutral' } }]),
...variants.map((variant: string) => [`with neutral variant ${variant} highlight`, { props: { variant, color: 'neutral', highlight: true } }]),
['with ariaLabel', { attrs: { 'aria-label': 'Aria label' } }],
['with as', { props: { as: 'span' } }],
['with class', { props: { class: 'absolute' } }],
['with ui', { props: { ui: { base: 'rounded-full' } } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: PinInputProps }) => {
const html = await ComponentRender(nameOrHtml, options, PinInput)
expect(html).toMatchSnapshot()
})
describe('emits', () => {
test('update:modelValue event', async () => {
const wrapper = mount(PinInput)
const input = wrapper.findComponent({ name: 'PinInputRoot' })
await input.vm.$emit('update:modelValue', ['1', '2', '3'])
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[['1', '2', '3']]] })
})
test('change event', async () => {
const wrapper = mount(PinInput)
const input = wrapper.findComponent({ name: 'PinInputRoot' })
await input.vm.$emit('complete', ['1', '2', '3', '4', '5'])
await flushPromises()
expect(wrapper.emitted()).toMatchObject({ change: [[{ type: 'change' }]] })
})
test('blur event', async () => {
const wrapper = mount(PinInput)
const lastPin = wrapper.find('input[aria-label="pin input 5 of 0"]')
lastPin.trigger('blur')
await flushPromises()
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?.length !== 5)
return [{ name: 'value', message: 'Error message' }]
return []
}
},
slotTemplate: `
<UFormField name="value">
<UPinInput id="input" v-model="state.value" />
</UFormField>
`
})
const input = wrapper.findComponent({ name: 'PinInputRoot' })
return {
wrapper,
input
}
}
test('validate on change works', async () => {
const { input, wrapper } = await createForm(['change'])
await input.vm.$emit('complete', ['1', '2', '3', '4'])
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.vm.$emit('update:modelValue', ['1', '2', '3', '4', '5'])
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('validate on blur works', async () => {
const { input, wrapper } = await createForm(['blur'])
const lastPin = wrapper.find('input[aria-label="pin input 5 of 5"]')
await input.vm.$emit('update:modelValue', ['1', '2', '3', '4'])
lastPin.trigger('blur')
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.vm.$emit('update:modelValue', ['1', '2', '3', '4', '5'])
lastPin.trigger('blur')
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', ['1', '2', '3', '4'])
await flushPromises()
expect(wrapper.text()).toContain('Error message')
await input.vm.$emit('update:modelValue', ['1', '2', '3', '4', '5'])
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
})
})