forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_component_spec.js
221 lines (211 loc) · 4.69 KB
/
async_component_spec.js
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
var Vue = require('src')
var _ = Vue.util
describe('Async components', function () {
var el
beforeEach(function () {
el = document.createElement('div')
document.body.appendChild(el)
spyWarns()
})
afterEach(function () {
document.body.removeChild(el)
})
it('normal', function (done) {
new Vue({
el: el,
template: '<test></test>',
components: {
test: function (resolve) {
setTimeout(function () {
resolve({
template: 'ok'
})
next()
}, 0)
}
}
})
function next () {
expect(el.textContent).toBe('ok')
done()
}
})
it('dynamic', function (done) {
var vm = new Vue({
el: el,
template: '<component :is="view"></component>',
data: {
view: 'view-a'
},
components: {
'view-a': function (resolve) {
setTimeout(function () {
resolve({
template: 'A'
})
step1()
}, 0)
},
'view-b': function (resolve) {
setTimeout(function () {
resolve({
template: 'B'
})
step2()
}, 0)
}
}
})
var aCalled = false
function step1 () {
// ensure A is resolved only once
expect(aCalled).toBe(false)
aCalled = true
expect(el.textContent).toBe('A')
vm.view = 'view-b'
}
function step2 () {
expect(el.textContent).toBe('B')
vm.view = 'view-a'
_.nextTick(function () {
expect(el.textContent).toBe('A')
done()
})
}
})
it('invalidate pending on dynamic switch', function (done) {
var vm = new Vue({
el: el,
template: '<component :is="view"></component>',
data: {
view: 'view-a'
},
components: {
'view-a': function (resolve) {
setTimeout(function () {
resolve({
template: 'A'
})
step1()
}, 100)
},
'view-b': function (resolve) {
setTimeout(function () {
resolve({
template: 'B'
})
step2()
}, 200)
}
}
})
expect(el.textContent).toBe('')
vm.view = 'view-b'
function step1 () {
// called after A resolves, but A should have been
// invalidated so no Ctor should be set
expect(vm._directives[0].Component).toBe(null)
}
function step2 () {
// B should resolve successfully
expect(el.textContent).toBe('B')
done()
}
})
it('invalidate pending on teardown', function (done) {
var vm = new Vue({
el: el,
template: '<test></test>',
data: {
view: 'view-a'
},
components: {
test: function (resolve) {
setTimeout(function () {
resolve({
template: 'A'
})
next()
}, 100)
}
}
})
expect(el.textContent).toBe('')
// cache directive isntance before destroy
var dir = vm._directives[0]
vm.$destroy()
function next () {
// called after A resolves, but A should have been
// invalidated so no Ctor should be set
expect(dir.Component).toBe(null)
done()
}
})
it('avoid duplicate requests', function (done) {
var factoryCallCount = 0
var instanceCount = 0
new Vue({
el: el,
template:
'<test></test>' +
'<test></test>',
components: {
test: factory
}
})
function factory (resolve) {
factoryCallCount++
setTimeout(function () {
resolve({
template: 'A',
created: function () {
instanceCount++
}
})
next()
}, 0)
}
function next () {
expect(factoryCallCount).toBe(1)
expect(el.textContent).toBe('AA')
expect(instanceCount).toBe(2)
done()
}
})
it('warn reject', function () {
new Vue({
el: el,
template: '<test></test>',
components: {
test: function (resolve, reject) {
reject('nooooo')
}
}
})
expect(hasWarned('Reason: nooooo')).toBe(true)
})
it('v-for', function (done) {
new Vue({
el: el,
template: '<test v-for="n in list" :n="n"></test>',
data: {
list: [1, 2, 3]
},
components: {
test: function (resolve) {
setTimeout(function () {
resolve({
props: ['n'],
template: '{{n}}'
})
next()
}, 0)
}
}
})
function next () {
expect(el.textContent).toBe('123')
done()
}
})
})