-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
Copy pathmisc.spec.ts
263 lines (237 loc) · 6.09 KB
/
misc.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
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
import Vue from '@vue/compat'
import { nextTick } from '../../runtime-core/src/scheduler'
import {
DeprecationTypes,
deprecationData,
toggleDeprecationWarning,
} from '../../runtime-core/src/compat/compatConfig'
import { triggerEvent } from './utils'
import { h } from '@vue/runtime-core'
beforeEach(() => {
toggleDeprecationWarning(true)
Vue.configureCompat({
MODE: 2,
GLOBAL_MOUNT: 'suppress-warning',
})
})
afterEach(() => {
toggleDeprecationWarning(false)
Vue.configureCompat({ MODE: 3 })
})
test('mode as function', () => {
const Foo = {
name: 'Foo',
render: (h: any) => h('div', 'foo'),
}
const Bar = {
name: 'Bar',
data: () => ({ msg: 'bar' }),
render: (ctx: any) => h('div', ctx.msg),
}
toggleDeprecationWarning(false)
Vue.configureCompat({
MODE: comp => (comp && comp.name === 'Bar' ? 3 : 2),
})
const vm = new Vue({
components: { Foo, Bar },
template: `<div><foo/><bar/></div>`,
}).$mount()
expect(vm.$el).toBeInstanceOf(HTMLDivElement)
expect(vm.$el.innerHTML).toBe(`<div>foo</div><div>bar</div>`)
})
test('WATCH_ARRAY', async () => {
const spy = vi.fn()
const vm = new Vue({
data() {
return {
foo: [],
}
},
watch: {
foo: spy,
},
}) as any
expect(
deprecationData[DeprecationTypes.WATCH_ARRAY].message,
).toHaveBeenWarned()
expect(spy).not.toHaveBeenCalled()
vm.foo.push(1)
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
test('PROPS_DEFAULT_THIS', () => {
let thisCtx: any
const Child = {
customOption: 1,
inject: ['provided'],
props: {
foo: null,
bar: {
default(this: any) {
// copy values since injection must be sync
thisCtx = {
foo: this.foo,
$options: this.$options,
provided: this.provided,
}
return this.foo + 1
},
},
},
template: `{{ bar }}`,
}
const vm = new Vue({
components: { Child },
provide: {
provided: 2,
},
template: `<child :foo="0" />`,
}).$mount()
expect(vm.$el.textContent).toBe('1')
// other props
expect(thisCtx.foo).toBe(0)
// $options
expect(thisCtx.$options.customOption).toBe(1)
// injections
expect(thisCtx.provided).toBe(2)
expect(
(deprecationData[DeprecationTypes.PROPS_DEFAULT_THIS].message as Function)(
'bar',
),
).toHaveBeenWarned()
})
test('V_ON_KEYCODE_MODIFIER', () => {
const spy = vi.fn()
const vm = new Vue({
template: `<input @keyup.1="spy">`,
methods: { spy },
}).$mount()
expect(vm.$el).toBeInstanceOf(HTMLInputElement)
triggerEvent(vm.$el, 'keyup', e => {
e.key = '_'
e.keyCode = 1
})
expect(spy).toHaveBeenCalled()
expect(
deprecationData[DeprecationTypes.V_ON_KEYCODE_MODIFIER].message,
).toHaveBeenWarned()
})
test('CUSTOM_DIR', async () => {
const myDir = {
bind: vi.fn(),
inserted: vi.fn(),
update: vi.fn(),
componentUpdated: vi.fn(),
unbind: vi.fn(),
} as any
const getCalls = () =>
Object.keys(myDir).map(key => myDir[key].mock.calls.length)
const vm = new Vue({
data() {
return {
ok: true,
foo: 1,
}
},
template: `<div v-if="ok" v-my-dir="foo"/>`,
directives: {
myDir,
},
}).$mount() as any
expect(getCalls()).toMatchObject([1, 1, 0, 0, 0])
expect(
(deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
'bind',
'beforeMount',
),
).toHaveBeenWarned()
expect(
(deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
'inserted',
'mounted',
),
).toHaveBeenWarned()
vm.foo++
await nextTick()
expect(getCalls()).toMatchObject([1, 1, 1, 1, 0])
expect(
(deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
'update',
'updated',
),
).toHaveBeenWarned()
expect(
(deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
'componentUpdated',
'updated',
),
).toHaveBeenWarned()
})
test('ATTR_FALSE_VALUE', () => {
const vm = new Vue({
template: `<div :id="false" :foo="false"/>`,
}).$mount()
expect(vm.$el).toBeInstanceOf(HTMLDivElement)
expect(vm.$el.hasAttribute('id')).toBe(false)
expect(vm.$el.hasAttribute('foo')).toBe(false)
expect(
(deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
'id',
),
).toHaveBeenWarned()
expect(
(deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
'foo',
),
).toHaveBeenWarned()
})
test("ATTR_FALSE_VALUE with false value shouldn't throw warning", () => {
const vm = new Vue({
template: `<div :id="false" :foo="false"/>`,
compatConfig: {
ATTR_FALSE_VALUE: false,
},
}).$mount()
expect(vm.$el).toBeInstanceOf(HTMLDivElement)
expect(vm.$el.hasAttribute('id')).toBe(true)
expect(vm.$el.getAttribute('id')).toBe('false')
expect(vm.$el.hasAttribute('foo')).toBe(true)
expect(vm.$el.getAttribute('foo')).toBe('false')
expect(
(deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
'id',
),
).not.toHaveBeenWarned()
expect(
(deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
'foo',
),
).not.toHaveBeenWarned()
})
test('ATTR_ENUMERATED_COERCION', () => {
const vm = new Vue({
template: `<div :draggable="null" :spellcheck="0" contenteditable="foo" />`,
}).$mount()
expect(vm.$el).toBeInstanceOf(HTMLDivElement)
expect(vm.$el.getAttribute('draggable')).toBe('false')
expect(vm.$el.getAttribute('spellcheck')).toBe('true')
expect(vm.$el.getAttribute('contenteditable')).toBe('true')
expect(
(
deprecationData[DeprecationTypes.ATTR_ENUMERATED_COERCION]
.message as Function
)('draggable', null, 'false'),
).toHaveBeenWarned()
expect(
(
deprecationData[DeprecationTypes.ATTR_ENUMERATED_COERCION]
.message as Function
)('spellcheck', 0, 'true'),
).toHaveBeenWarned()
expect(
(
deprecationData[DeprecationTypes.ATTR_ENUMERATED_COERCION]
.message as Function
)('contenteditable', 'foo', 'true'),
).toHaveBeenWarned()
})