-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods-lifecycle.spec.js
182 lines (170 loc) · 4.73 KB
/
methods-lifecycle.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
import Vue from 'vue'
import Dep from 'core/observer/dep'
describe('Instance methods lifecycle', () => {
describe('$mount', () => {
it('empty mount', () => {
const vm = new Vue({
data: { msg: 'hi' },
template: '<div>{{ msg }}</div>'
}).$mount()
expect(vm.$el.tagName).toBe('DIV')
expect(vm.$el.textContent).toBe('hi')
})
it('mount to existing element', () => {
const el = document.createElement('div')
el.innerHTML = '{{ msg }}'
const vm = new Vue({
data: { msg: 'hi' }
}).$mount(el)
expect(vm.$el.tagName).toBe('DIV')
expect(vm.$el.textContent).toBe('hi')
})
it('mount to id', () => {
const el = document.createElement('div')
el.id = 'mount-test'
el.innerHTML = '{{ msg }}'
document.body.appendChild(el)
const vm = new Vue({
data: { msg: 'hi' }
}).$mount('#mount-test')
expect(vm.$el.tagName).toBe('DIV')
expect(vm.$el.textContent).toBe('hi')
})
it('Dep.target should be undefined in lifecycle', () => {
const vm = new Vue({
template: '<div><my-component></my-component></div>',
components: {
myComponent: {
template: '<div>hi</div>',
mounted () {
const _msg = this.msg
expect(Dep.target).toBe(undefined)
},
computed: {
msg () {
return 1
}
}
}
}
}).$mount()
})
it('Dep.target should be undefined during invocation of child immediate watcher', done => {
let calls = 0, childData
const parentUpdate = jasmine.createSpy()
new Vue({
template: '<div><my-component></my-component></div>',
updated: parentUpdate,
components: {
myComponent: {
template: '<div>{{ a }}</div>',
data() {
return childData = { a: 123 }
},
watch: {
a: {
handler() {
++calls
expect(Dep.target).toBe(undefined)
},
immediate: true
}
}
}
}
}).$mount()
expect(calls).toBe(1)
childData.a++
waitForUpdate(() => {
expect(parentUpdate).not.toHaveBeenCalled()
}).then(done)
})
})
describe('$destroy', () => {
it('remove self from parent', () => {
const vm = new Vue({
template: '<test></test>',
components: {
test: { template: '<div></div>' }
}
}).$mount()
vm.$children[0].$destroy()
expect(vm.$children.length).toBe(0)
})
it('teardown watchers', () => {
const vm = new Vue({
data: { a: 123 },
template: '<div></div>'
}).$mount()
vm.$watch('a', () => {})
vm.$destroy()
expect(vm._watcher.active).toBe(false)
expect(vm._watchers.every(w => !w.active)).toBe(true)
})
it('remove self from data observer', () => {
const vm = new Vue({ data: { a: 1 }})
vm.$destroy()
expect(vm.$data.__ob__.vmCount).toBe(0)
})
it('avoid duplicate calls', () => {
const spy = jasmine.createSpy('destroy')
const vm = new Vue({
beforeDestroy: spy
})
vm.$destroy()
vm.$destroy()
expect(spy.calls.count()).toBe(1)
})
})
describe('$forceUpdate', () => {
it('should force update', done => {
const vm = new Vue({
data: {
a: {}
},
template: '<div>{{ a.b }}</div>'
}).$mount()
expect(vm.$el.textContent).toBe('')
vm.a.b = 'foo'
waitForUpdate(() => {
// should not work because adding new property
expect(vm.$el.textContent).toBe('')
vm.$forceUpdate()
}).then(() => {
expect(vm.$el.textContent).toBe('foo')
}).then(done)
})
})
describe('$nextTick', () => {
it('should be called after DOM update in correct context', done => {
const vm = new Vue({
template: '<div>{{ msg }}</div>',
data: {
msg: 'foo'
}
}).$mount()
vm.msg = 'bar'
vm.$nextTick(function () {
expect(this).toBe(vm)
expect(vm.$el.textContent).toBe('bar')
done()
})
})
if (typeof Promise !== 'undefined') {
it('should be called after DOM update in correct context, when using Promise syntax', done => {
const vm = new Vue({
template: '<div>{{ msg }}</div>',
data: {
msg: 'foo'
}
}).$mount()
vm.msg = 'bar'
vm.$nextTick().then(ctx => {
expect(ctx).toBe(vm)
expect(vm.$el.textContent).toBe('bar')
done()
})
})
}
})
})