-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_spec.js
272 lines (257 loc) · 6.87 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
// test cases for edge cases & bug fixes
var Vue = require('../../../src/vue')
// spies on different objects
var _ = require('../../../src/util/debug')
var __ = Vue.util
describe('Misc', function () {
beforeEach(function () {
spyOn(_, 'warn')
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: '<slot></slot>'
},
inner: {
template: 'hi',
attached: spy1,
detached: spy2
}
}
})
expect(spy1).toHaveBeenCalled()
vm.$refs.outter.$remove()
expect(spy2).toHaveBeenCalled()
})
it('v-for 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-for="n in list">{{n}}</div>',
replace: true
}
}
})
expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
})
// #922
it('template v-for inside svg', function () {
var el = document.createElement('div')
new Vue({
el: el,
template: '<svg><template v-for="n in list"><text>{{n}}</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: document.createElement('div'),
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.$set('test', test)
__.set(obj2, '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('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('nested object $set should trigger parent array notify', function (done) {
var vm = new Vue({
el: document.createElement('div'),
template: '{{items | json}}{{items[0].a}}',
data: {
items: [{}]
}
})
expect(vm.$el.textContent).toBe(JSON.stringify(vm.items, null, 2))
__.set(vm.items[0], 'a', 123)
Vue.nextTick(function () {
expect(vm.$el.textContent).toBe(JSON.stringify(vm.items, null, 2) + '123')
done()
})
})
it('warn unkown custom element', function () {
new Vue({
el: document.createElement('div'),
template: '<custom-stuff></custom-stuff>'
})
expect(hasWarned(__, 'Unknown custom element')).toBe(true)
})
})