forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputed.spec.js
207 lines (195 loc) · 4.2 KB
/
computed.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
import Vue from 'vue'
import testObjectOption from '../../../helpers/test-object-option'
describe('Options computed', () => {
it('basic usage', done => {
const vm = new Vue({
template: '<div>{{ b }}</div>',
data: {
a: 1
},
computed: {
b () {
return this.a + 1
}
}
}).$mount()
expect(vm.b).toBe(2)
expect(vm.$el.textContent).toBe('2')
vm.a = 2
expect(vm.b).toBe(3)
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('3')
}).then(done)
})
it('with setter', done => {
const vm = new Vue({
template: '<div>{{ b }}</div>',
data: {
a: 1
},
computed: {
b: {
get () { return this.a + 1 },
set (v) { this.a = v - 1 }
}
}
}).$mount()
expect(vm.b).toBe(2)
expect(vm.$el.textContent).toBe('2')
vm.a = 2
expect(vm.b).toBe(3)
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('3')
vm.b = 1
expect(vm.a).toBe(0)
}).then(() => {
expect(vm.$el.textContent).toBe('1')
}).then(done)
})
testObjectOption('computed')
it('warn with setter and no getter', () => {
const vm = new Vue({
template: `
<div>
<test></test>
</div>
`,
components: {
test: {
data () {
return {
a: 1
}
},
computed: {
b: {
set (v) { this.a = v }
}
},
template: `<div>{{a}}</div>`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>1</div>')
expect('No getter function has been defined for computed property "b".').toHaveBeenWarned()
})
it('watching computed', done => {
const spy = jasmine.createSpy('watch computed')
const vm = new Vue({
data: {
a: 1
},
computed: {
b () { return this.a + 1 }
}
})
vm.$watch('b', spy)
vm.a = 2
waitForUpdate(() => {
expect(spy).toHaveBeenCalledWith(3, 2)
}).then(done)
})
it('caching', () => {
const spy = jasmine.createSpy('cached computed')
const vm = new Vue({
data: {
a: 1
},
computed: {
b () {
spy()
return this.a + 1
}
}
})
expect(spy.calls.count()).toBe(0)
vm.b
expect(spy.calls.count()).toBe(1)
vm.b
expect(spy.calls.count()).toBe(1)
})
it('cache: false', () => {
const spy = jasmine.createSpy('cached computed')
const vm = new Vue({
data: {
a: 1
},
computed: {
b: {
cache: false,
get () {
spy()
return this.a + 1
}
}
}
})
expect(spy.calls.count()).toBe(0)
vm.b
expect(spy.calls.count()).toBe(1)
vm.b
expect(spy.calls.count()).toBe(2)
})
it('as component', done => {
const Comp = Vue.extend({
template: `<div>{{ b }} {{ c }}</div>`,
data () {
return { a: 1 }
},
computed: {
// defined on prototype
b () {
return this.a + 1
}
}
})
const vm = new Comp({
computed: {
// defined at instantiation
c () {
return this.b + 1
}
}
}).$mount()
expect(vm.b).toBe(2)
expect(vm.c).toBe(3)
expect(vm.$el.textContent).toBe('2 3')
vm.a = 2
expect(vm.b).toBe(3)
expect(vm.c).toBe(4)
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('3 4')
}).then(done)
})
it('warn conflict with data', () => {
new Vue({
data: {
a: 1
},
computed: {
a: () => 2
}
})
expect(`computed property "a" is already defined in data`).toHaveBeenWarned()
})
it('warn conflict with props', () => {
new Vue({
props: ['a'],
propsData: { a: 1 },
computed: {
a: () => 2
}
})
expect(`computed property "a" is already defined as a prop`).toHaveBeenWarned()
})
it('rethrow computed error', () => {
const vm = new Vue({
computed: {
a: () => {
throw new Error('rethrow')
}
}
})
expect(() => vm.a).toThrowError('rethrow')
})
})