/
index.spec.ts
314 lines (283 loc) 路 8.61 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { EMPTY_ARR } from '@vue/shared'
import { createApp, ref, nextTick, reactive } from '../src'
describe('compiler + runtime integration', () => {
it('should support runtime template compilation', () => {
const container = document.createElement('div')
const App = {
template: `{{ count }}`,
data() {
return {
count: 0
}
}
}
createApp(App).mount(container)
expect(container.innerHTML).toBe(`0`)
})
it('keep-alive with compiler + runtime integration', async () => {
const container = document.createElement('div')
const one = {
name: 'one',
template: 'one',
created: jest.fn(),
mounted: jest.fn(),
activated: jest.fn(),
deactivated: jest.fn(),
unmounted: jest.fn()
}
const toggle = ref(true)
const App = {
template: `
<keep-alive>
<one v-if="toggle"></one>
</keep-alive>
`,
data() {
return {
toggle
}
},
components: {
One: one
}
}
createApp(App).mount(container)
expect(container.innerHTML).toBe(`one`)
expect(one.created).toHaveBeenCalledTimes(1)
expect(one.mounted).toHaveBeenCalledTimes(1)
expect(one.activated).toHaveBeenCalledTimes(1)
expect(one.deactivated).toHaveBeenCalledTimes(0)
expect(one.unmounted).toHaveBeenCalledTimes(0)
toggle.value = false
await nextTick()
expect(container.innerHTML).toBe(`<!--v-if-->`)
expect(one.created).toHaveBeenCalledTimes(1)
expect(one.mounted).toHaveBeenCalledTimes(1)
expect(one.activated).toHaveBeenCalledTimes(1)
expect(one.deactivated).toHaveBeenCalledTimes(1)
expect(one.unmounted).toHaveBeenCalledTimes(0)
toggle.value = true
await nextTick()
expect(container.innerHTML).toBe(`one`)
expect(one.created).toHaveBeenCalledTimes(1)
expect(one.mounted).toHaveBeenCalledTimes(1)
expect(one.activated).toHaveBeenCalledTimes(2)
expect(one.deactivated).toHaveBeenCalledTimes(1)
expect(one.unmounted).toHaveBeenCalledTimes(0)
})
it('should support runtime template via CSS ID selector', () => {
const container = document.createElement('div')
const template = document.createElement('div')
template.id = 'template'
template.innerHTML = '{{ count }}'
document.body.appendChild(template)
const App = {
template: `#template`,
data() {
return {
count: 0
}
}
}
createApp(App).mount(container)
expect(container.innerHTML).toBe(`0`)
})
it('should support runtime template via direct DOM node', () => {
const container = document.createElement('div')
const template = document.createElement('div')
template.id = 'template'
template.innerHTML = '{{ count }}'
const App = {
template,
data() {
return {
count: 0
}
}
}
createApp(App).mount(container)
expect(container.innerHTML).toBe(`0`)
})
it('should warn template compilation errors with codeframe', () => {
const container = document.createElement('div')
const App = {
template: `<div v-if>`
}
createApp(App).mount(container)
expect(
`Template compilation error: Element is missing end tag`
).toHaveBeenWarned()
expect(
`
1 | <div v-if>
| ^`.trim()
).toHaveBeenWarned()
expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
expect(
`
1 | <div v-if>
| ^^^^`.trim()
).toHaveBeenWarned()
})
it('should support custom element via config.isCustomElement (deprecated)', () => {
const app = createApp({
template: '<custom></custom>'
})
const container = document.createElement('div')
app.config.isCustomElement = tag => tag === 'custom'
app.mount(container)
expect(container.innerHTML).toBe('<custom></custom>')
})
it('should support custom element via config.compilerOptions.isCustomElement', () => {
const app = createApp({
template: '<custom></custom>'
})
const container = document.createElement('div')
app.config.compilerOptions.isCustomElement = tag => tag === 'custom'
app.mount(container)
expect(container.innerHTML).toBe('<custom></custom>')
})
it('should support using element innerHTML as template', () => {
const app = createApp({
data: () => ({
msg: 'hello'
})
})
const container = document.createElement('div')
container.innerHTML = '{{msg}}'
app.mount(container)
expect(container.innerHTML).toBe('hello')
})
it('should support selector of rootContainer', () => {
const container = document.createElement('div')
const origin = document.querySelector
document.querySelector = jest.fn().mockReturnValue(container)
const App = {
template: `{{ count }}`,
data() {
return {
count: 0
}
}
}
createApp(App).mount('#app')
expect(container.innerHTML).toBe(`0`)
document.querySelector = origin
})
it('should warn when template is not available', () => {
const app = createApp({
template: {}
})
const container = document.createElement('div')
app.mount(container)
expect('[Vue warn]: invalid template option:').toHaveBeenWarned()
})
it('should warn when template is is not found', () => {
const app = createApp({
template: '#not-exist-id'
})
const container = document.createElement('div')
app.mount(container)
expect(
'[Vue warn]: Template element not found or is empty: #not-exist-id'
).toHaveBeenWarned()
})
it('should warn when container is not found', () => {
const origin = document.querySelector
document.querySelector = jest.fn().mockReturnValue(null)
const App = {
template: `{{ count }}`,
data() {
return {
count: 0
}
}
}
createApp(App).mount('#not-exist-id')
expect(
'[Vue warn]: Failed to mount app: mount target selector "#not-exist-id" returned null.'
).toHaveBeenWarned()
document.querySelector = origin
})
// #1813
it('should not report an error when "0" as patchFlag value', async () => {
const container = document.createElement('div')
const target = document.createElement('div')
const count = ref(0)
const origin = document.querySelector
document.querySelector = jest.fn().mockReturnValue(target)
const App = {
template: `
<teleport v-if="count < 2" to="#target">
<div>
<div>{{ count }}</div>
</div>
</teleport>
`,
data() {
return {
count
}
}
}
createApp(App).mount(container)
expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
expect(target.innerHTML).toBe(`<div><div>0</div></div>`)
count.value++
await nextTick()
expect(container.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
expect(target.innerHTML).toBe(`<div><div>1</div></div>`)
count.value++
await nextTick()
expect(container.innerHTML).toBe(`<!--v-if-->`)
expect(target.innerHTML).toBe(``)
document.querySelector = origin
})
test('v-if + v-once', async () => {
const ok = ref(true)
const App = {
setup() {
return { ok }
},
template: `<div>{{ ok }}<div v-if="ok" v-once>{{ ok }}</div></div>`
}
const container = document.createElement('div')
createApp(App).mount(container)
expect(container.innerHTML).toBe(`<div>true<div>true</div></div>`)
ok.value = false
await nextTick()
expect(container.innerHTML).toBe(`<div>false<div>true</div></div>`)
})
test('v-for + v-once', async () => {
const list = reactive([1])
const App = {
setup() {
return { list }
},
template: `<div>{{ list.length }}<div v-for="i in list" v-once>{{ i }}</div></div>`
}
const container = document.createElement('div')
createApp(App).mount(container)
expect(container.innerHTML).toBe(`<div>1<div>1</div></div>`)
list.push(2)
await nextTick()
expect(container.innerHTML).toBe(`<div>2<div>1</div></div>`)
})
// #2413
it('EMPTY_ARR should not change', () => {
const App = {
template: `<div v-for="v of ['a']">{{ v }}</div>`
}
const container = document.createElement('div')
createApp(App).mount(container)
expect(EMPTY_ARR.length).toBe(0)
})
test('BigInt support', () => {
const app = createApp({
template: `<div>{{ BigInt(BigInt(100000111)) + BigInt(2000000000n) * 30000000n }}</div>`
})
const root = document.createElement('div')
app.mount(root)
expect(root.innerHTML).toBe('<div>60000000100000111</div>')
})
})