forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_spec.js
335 lines (316 loc) · 8.32 KB
/
misc_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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// test cases for edge cases & bug fixes
var Vue = require('../../../src/vue')
var _ = require('../../../src/util/debug')
describe('Misc', function () {
beforeEach(function () {
spyOn(_, 'warn')
})
it('should handle directive.bind() altering its childNode structure', function () {
var vm = new Vue({
el: document.createElement('div'),
template: '<div v-test>{{test}}</div>',
data: {
test: 'hi'
},
directives: {
test: {
bind: function () {
this.el.insertBefore(document.createTextNode('yo '),
this.el.firstChild)
}
}
}
})
expect(vm.$el.textContent).toBe('yo hi')
})
it('attached/detached hooks for transcluded components', function () {
var spy1 = jasmine.createSpy('attached')
var spy2 = jasmine.createSpy('detached')
var el = document.createElement('div')
el.innerHTML = '<outer v-ref="outter"><inner></inner></outer>'
document.body.appendChild(el)
var vm = new Vue({
el: el,
components: {
outer: {
template: '<content></content>'
},
inner: {
template: 'hi',
attached: spy1,
detached: spy2
}
}
})
expect(spy1).toHaveBeenCalled()
vm.$.outter.$remove()
expect(spy2).toHaveBeenCalled()
})
it('v-repeat on component root node with replace:true', function () {
var el = document.createElement('div')
var vm = new Vue({
el: el,
template: '<test></test>',
components: {
test: {
data: function () {
return { list: [1, 2, 3] }
},
template: '<div v-repeat="list">{{$value}}</div>',
replace: true
}
}
})
expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
})
// #922
it('template repeat inside svg', function () {
var el = document.createElement('div')
new Vue({
el: el,
template: '<svg><template v-repeat="list"><text>{{$value}}</text></template></svg>',
data: {
list: [1, 2, 3]
}
})
// IE inlines svg namespace
var xmlns = /\s?xmlns=".*svg"/
expect(el.innerHTML.replace(xmlns, '')).toBe('<svg><text>1</text><text>2</text><text>3</text></svg>')
})
// #1005
it('call lifecycle hooks for child components', function () {
Vue.options.replace = true
var el = document.createElement('div')
var logs = []
function log (n) {
return function () {
logs.push(n)
}
}
document.body.appendChild(el)
var vm = new Vue({
el: el,
attached: log(0),
ready: log(1),
detached: log(2),
beforeDestroy: log(3),
destroyed: log(4),
template: '<div><test></test><test></test></div>',
components: {
test: {
template: '<span>hi</span>',
attached: log(5),
ready: log(6),
detached: log(7),
beforeDestroy: log(8),
destroyed: log(9)
}
}
})
expect(vm.$el.innerHTML).toBe('<span>hi</span><span>hi</span>')
expect(logs.join()).toBe('0,5,6,5,6,1')
logs = []
vm.$destroy(true)
expect(logs.join()).toBe('3,8,9,8,9,2,7,7,4')
Vue.options.replace = false
})
// #1006
it('destroyed hook for components inside v-if', function (done) {
var spy = jasmine.createSpy('v-if destroyed hook')
var vm = new Vue({
el: document.createElement('div'),
template: '<template v-if="ok"><test></test></template>',
data: {
ok: true
},
components: {
test: {
destroyed: spy
}
}
})
vm.ok = false
Vue.nextTick(function () {
expect(spy).toHaveBeenCalled()
done()
})
})
it('frozen model, root', function (done) {
var vm = new Vue({
el: document.createElement('div'),
template: '{{msg}}',
data: Object.freeze({
msg: 'hi!'
})
})
expect(vm.$el.textContent).toBe('hi!')
vm.msg = 'ho!'
Vue.nextTick(function () {
expect(vm.$el.textContent).toBe('hi!')
done()
})
})
it('frozen model, non-root', function (done) {
var vm = new Vue({
el: document.createElement('div'),
template: '{{msg}} {{frozen.msg}}',
data: {
msg: 'hi',
frozen: Object.freeze({
msg: 'frozen'
})
}
})
expect(vm.$el.textContent).toBe('hi frozen')
vm.msg = 'ho'
vm.frozen.msg = 'changed'
Vue.nextTick(function () {
expect(vm.$el.textContent).toBe('ho frozen')
done()
})
})
it('should not trigger deep/Array watchers when digesting', function (done) {
var spy1 = jasmine.createSpy('deep')
var spy2 = jasmine.createSpy('Array')
var spy3 = jasmine.createSpy('test')
var spy4 = jasmine.createSpy('deep-mutated')
var vm = new Vue({
el: 'body',
data: {
obj: {},
arr: [],
obj2: {}
},
watch: {
obj: {
handler: spy1,
deep: true
},
arr: spy2,
// if the watcher is watching the added value,
// it should still trigger properly
test: {
handler: spy3,
deep: true
},
// if the object is in fact mutated, it should
// still trigger.
obj2: {
handler: spy4,
deep: true
}
}
})
var test = []
var obj2 = vm.obj2
vm.$add('test', test)
obj2.$add('test', 123)
Vue.nextTick(function () {
expect(spy1).not.toHaveBeenCalled()
expect(spy2).not.toHaveBeenCalled()
expect(spy3).toHaveBeenCalledWith(test, undefined)
expect(spy4).toHaveBeenCalledWith(obj2, obj2)
done()
})
})
it('strict mode', function () {
Vue.config.strict = true
new Vue({
el: document.createElement('div'),
template: '<test></test>',
components: {
test: {
template: '<div v-strict>hi</div>'
}
},
directives: {
strict: function () {}
}
})
expect(hasWarned(_, 'Failed to resolve directive: strict')).toBe(true)
Vue.config.strict = false
})
it('strict mode for repeat instances', function () {
Vue.config.strict = true
var vm = new Vue({
el: document.createElement('div'),
template: '<div v-repeat="list"><test></test></div>',
data: {
list: [1, 2]
},
components: {
test: {
template: 'hi'
}
}
})
expect(_.warn).not.toHaveBeenCalled()
expect(vm.$el.textContent).toBe('hihi')
Vue.config.strict = false
})
it('class interpolation and v-class should work together', function (done) {
var el = document.createElement('div')
el.setAttribute('class', 'a {{classB}}')
el.setAttribute('v-class', 'c: showC')
var vm = new Vue({
el: el,
data: {
classB: 'b',
showC: true
}
})
assertClasses(['a', 'b', 'c'])
vm.classB = 'bb'
vm.showC = false
Vue.nextTick(function () {
assertClasses(['a', 'bb'])
done()
})
function assertClasses (expectedClasses) {
var classes = el.className.trim().split(/\s+/)
expect(classes.length).toBe(expectedClasses.length)
var has = expectedClasses.every(function (cls) {
return classes.indexOf(cls) > -1
})
expect(has).toBe(true)
}
})
it('handle interpolated textarea', function (done) {
var el = document.createElement('div')
el.innerHTML = '<textarea>hello {{msg}}</textarea>'
var vm = new Vue({
el: el,
data: {
msg: 'test'
}
})
expect(el.innerHTML).toBe('<textarea>hello test</textarea>')
vm.msg = 'world'
Vue.nextTick(function () {
expect(el.innerHTML).toBe('<textarea>hello world</textarea>')
done()
})
})
it('resolveAsset for repeat instance inside content in strict mode', function () {
Vue.config.strict = true
var el = document.createElement('div')
el.innerHTML =
'<outer>' +
'<template v-repeat="item in items">' +
'<inner>{{item}}</inner>' +
'</template>' +
'</outer>'
new Vue({
el: el,
data: {
items: [1, 2, 3]
},
components: {
outer: { template: '<content></content>' },
inner: { template: '<content></content>' }
}
})
expect(el.textContent).toBe('123')
Vue.config.strict = false
})
})