forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods-data.spec.js
More file actions
116 lines (106 loc) · 2.67 KB
/
methods-data.spec.js
File metadata and controls
116 lines (106 loc) · 2.67 KB
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
import Vue from 'vue'
describe('Instance methods data', () => {
it('$set/$delete', done => {
const vm = new Vue({
template: '<div>{{ a.msg }}</div>',
data: {
a: {}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('')
vm.$set(vm.a, 'msg', 'hello')
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.$delete(vm.a, 'msg')
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})
describe('$watch', () => {
let vm, spy
beforeEach(() => {
spy = jasmine.createSpy('watch')
vm = new Vue({
data: {
a: {
b: 1
}
},
methods: {
foo: spy
}
})
})
it('basic usage', done => {
vm.$watch('a.b', spy)
vm.a.b = 2
waitForUpdate(() => {
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(2, 1)
vm.a = { b: 3 }
}).then(() => {
expect(spy.calls.count()).toBe(2)
expect(spy).toHaveBeenCalledWith(3, 2)
}).then(done)
})
it('immediate', () => {
vm.$watch('a.b', spy, { immediate: true })
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(1)
})
it('unwatch', done => {
const unwatch = vm.$watch('a.b', spy)
unwatch()
vm.a.b = 2
waitForUpdate(() => {
expect(spy.calls.count()).toBe(0)
}).then(done)
})
it('function watch', done => {
vm.$watch(function () {
return this.a.b
}, spy)
vm.a.b = 2
waitForUpdate(() => {
expect(spy).toHaveBeenCalledWith(2, 1)
}).then(done)
})
it('deep watch', done => {
var oldA = vm.a
vm.$watch('a', spy, { deep: true })
vm.a.b = 2
waitForUpdate(() => {
expect(spy).toHaveBeenCalledWith(oldA, oldA)
vm.a = { b: 3 }
}).then(() => {
expect(spy).toHaveBeenCalledWith(vm.a, oldA)
}).then(done)
})
it('handler option', done => {
var oldA = vm.a
vm.$watch('a', {
handler: spy,
deep: true
})
vm.a.b = 2
waitForUpdate(() => {
expect(spy).toHaveBeenCalledWith(oldA, oldA)
vm.a = { b: 3 }
}).then(() => {
expect(spy).toHaveBeenCalledWith(vm.a, oldA)
}).then(done)
})
it('handler option in string', () => {
vm.$watch('a.b', {
handler: 'foo',
immediate: true
})
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(1)
})
it('warn expresssion', () => {
vm.$watch('a + b', spy)
expect('Watcher only accepts simple dot-delimited paths').toHaveBeenWarned()
})
})
})