forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
104 lines (88 loc) · 2.75 KB
/
index.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
import { isVue2, nextTick } from 'vue-demi'
import { useSetup } from '../../.test'
import { useVModel } from '.'
describe('useVModel', () => {
const defaultKey = isVue2 ? 'value' : 'modelValue'
const defaultValue = 'default'
const defaultProps = () => ({
[defaultKey]: defaultValue,
})
const emitMock = jest.fn((event: string, values: any[]) => {})
beforeEach(() => emitMock.mockClear())
it('should work with default value', () => {
useSetup(() => {
const data = useVModel(defaultProps())
expect(data.value).toBe(defaultValue)
})
})
it('should work with arguments', () => {
const props = {
...defaultProps(),
data: 'data',
}
useSetup(() => {
const data = useVModel(props, 'data')
expect(data.value).toBe('data')
})
})
it('should emit on value change', async() => {
useSetup(() => {
const data = useVModel(defaultProps(), undefined, emitMock)
data.value = 'changed'
})
expect(emitMock.mock.calls[0][0]).toBe(isVue2 ? 'input' : 'update:modelValue')
expect(emitMock.mock.calls[0][1]).toBe('changed')
})
it('should use eventName if set', async() => {
useSetup(() => {
const data = useVModel(defaultProps(), undefined, emitMock, { eventName: 'onChange' })
data.value = 'changed'
})
expect(emitMock.mock.calls[0][0]).toBe('onChange')
})
it('should emit w/ passive', async() => {
const props = {
...defaultProps(),
age: 18,
}
useSetup(() => {
const data = useVModel(props, 'age', emitMock, { passive: true })
data.value = 20
})
await nextTick()
expect(emitMock.mock.calls[0][0]).toBe('update:age')
expect(emitMock.mock.calls[0][1]).toBe(20)
})
it('should emit w/ object props type', async() => {
const props = {
...defaultProps(),
data: {
age: 18,
},
}
useSetup(() => {
const data = useVModel(props, 'data', emitMock, { passive: true, deep: true })
data.value.age = 20
})
await nextTick()
expect(emitMock.mock.calls[0][0]).toBe('update:data')
expect(emitMock).toHaveBeenCalledTimes(1)
expect(JSON.stringify(emitMock.mock.calls[0][1])).toBe(JSON.stringify({ age: 20 }))
})
it('should emit w/ array props type', async() => {
const props = {
...defaultProps(),
data: {
hobbys: ['coding'],
},
}
useSetup(() => {
const data = useVModel(props, 'data', emitMock, { passive: true, deep: true })
data.value.hobbys.push('basketball')
})
await nextTick()
expect(emitMock.mock.calls[0][0]).toBe('update:data')
expect(emitMock).toHaveBeenCalledTimes(1)
expect(JSON.stringify(emitMock.mock.calls[0][1])).toBe(JSON.stringify({ hobbys: ['coding', 'basketball'] }))
})
})