@@ -155,6 +155,10 @@ describe('Instance Scope', function () {
155
155
156
156
describe ( 'computed' , function ( ) {
157
157
158
+ var spyE = jasmine . createSpy ( 'computed e' )
159
+ var spyF = jasmine . createSpy ( 'cached computed f' )
160
+ var spyCachedWatcher = jasmine . createSpy ( 'cached computed watcher' )
161
+
158
162
var Test = Vue . extend ( {
159
163
computed : {
160
164
c : function ( ) {
@@ -173,20 +177,42 @@ describe('Instance Scope', function () {
173
177
// chained computed
174
178
e : function ( ) {
175
179
return this . c + 'e'
180
+ } ,
181
+ // cached
182
+ f : {
183
+ cache : true ,
184
+ get : function ( ) {
185
+ spyF ( )
186
+ return this . ff
187
+ }
188
+ } ,
189
+ // chained cached
190
+ g : function ( ) {
191
+ return this . f + 1
192
+ } ,
193
+ // another cached, for watcher test
194
+ h : {
195
+ cache : true ,
196
+ get : function ( ) {
197
+ return this . hh
198
+ }
176
199
}
177
200
}
178
201
} )
179
202
180
- var spy = jasmine . createSpy ( )
181
203
var vm = new Test ( {
182
204
data : {
183
205
a : 'a' ,
184
- b : 'b'
206
+ b : 'b' ,
207
+ ff : 0 ,
208
+ hh : 0
209
+ } ,
210
+ watch : {
211
+ e : spyE ,
212
+ h : spyCachedWatcher
185
213
}
186
214
} )
187
215
188
- vm . $watch ( 'e' , spy )
189
-
190
216
it ( 'get' , function ( ) {
191
217
expect ( vm . c ) . toBe ( 'ab' )
192
218
expect ( vm . d ) . toBe ( 'ab' )
@@ -202,7 +228,7 @@ describe('Instance Scope', function () {
202
228
expect ( vm . d ) . toBe ( 'cd' )
203
229
expect ( vm . e ) . toBe ( 'cde' )
204
230
Vue . nextTick ( function ( ) {
205
- expect ( spy ) . toHaveBeenCalledWith ( 'cde' , 'abe' )
231
+ expect ( spyE ) . toHaveBeenCalledWith ( 'cde' , 'abe' )
206
232
done ( )
207
233
} )
208
234
} )
@@ -225,13 +251,45 @@ describe('Instance Scope', function () {
225
251
expect ( child . d ) . toBe ( 'ef' )
226
252
expect ( vm . e ) . toBe ( 'efe' )
227
253
Vue . nextTick ( function ( ) {
228
- expect ( spy ) . toHaveBeenCalledWith ( 'efe' , 'cde' )
254
+ expect ( spyE ) . toHaveBeenCalledWith ( 'efe' , 'cde' )
255
+ done ( )
256
+ } )
257
+ } )
258
+
259
+ it ( 'cached computed' , function ( ) {
260
+ expect ( spyF ) . not . toHaveBeenCalled ( )
261
+ var f = vm . f
262
+ var g = vm . g
263
+ expect ( spyF . calls . count ( ) ) . toBe ( 1 )
264
+ expect ( f ) . toBe ( 0 )
265
+ expect ( g ) . toBe ( 1 )
266
+ // get again
267
+ f = vm . f
268
+ g = vm . g
269
+ // should not be evaluated again
270
+ expect ( spyF . calls . count ( ) ) . toBe ( 1 )
271
+ expect ( f ) . toBe ( 0 )
272
+ expect ( g ) . toBe ( 1 )
273
+ // update dep
274
+ vm . ff = 1
275
+ f = vm . f
276
+ g = vm . g
277
+ expect ( spyF . calls . count ( ) ) . toBe ( 2 )
278
+ expect ( f ) . toBe ( 1 )
279
+ expect ( g ) . toBe ( 2 )
280
+ } )
281
+
282
+ it ( 'watching cached computed' , function ( done ) {
283
+ expect ( spyCachedWatcher ) . not . toHaveBeenCalled ( )
284
+ vm . hh = 2
285
+ Vue . nextTick ( function ( ) {
286
+ expect ( spyCachedWatcher ) . toHaveBeenCalledWith ( 2 , 0 )
229
287
done ( )
230
288
} )
231
289
} )
232
290
233
291
it ( 'same definition object bound to different instance' , function ( ) {
234
- vm = new Test ( {
292
+ var vm = new Test ( {
235
293
data : {
236
294
a : 'A' ,
237
295
b : 'B'
0 commit comments