@@ -260,12 +260,13 @@ describe('api: watch', () => {
260
260
it ( 'flush timing: post (default)' , async ( ) => {
261
261
const count = ref ( 0 )
262
262
let callCount = 0
263
+ let result
263
264
const assertion = jest . fn ( count => {
264
265
callCount ++
265
266
// on mount, the watcher callback should be called before DOM render
266
267
// on update, should be called after the count is updated
267
268
const expectedDOM = callCount === 1 ? `` : `${ count } `
268
- expect ( serializeInner ( root ) ) . toBe ( expectedDOM )
269
+ result = serializeInner ( root ) === expectedDOM
269
270
} )
270
271
271
272
const Comp = {
@@ -279,27 +280,31 @@ describe('api: watch', () => {
279
280
const root = nodeOps . createElement ( 'div' )
280
281
render ( h ( Comp ) , root )
281
282
expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
283
+ expect ( result ) . toBe ( true )
282
284
283
285
count . value ++
284
286
await nextTick ( )
285
287
expect ( assertion ) . toHaveBeenCalledTimes ( 2 )
288
+ expect ( result ) . toBe ( true )
286
289
} )
287
290
288
291
it ( 'flush timing: pre' , async ( ) => {
289
292
const count = ref ( 0 )
290
293
const count2 = ref ( 0 )
291
294
292
295
let callCount = 0
296
+ let result1
297
+ let result2
293
298
const assertion = jest . fn ( ( count , count2Value ) => {
294
299
callCount ++
295
300
// on mount, the watcher callback should be called before DOM render
296
301
// on update, should be called before the count is updated
297
302
const expectedDOM = callCount === 1 ? `` : `${ count - 1 } `
298
- expect ( serializeInner ( root ) ) . toBe ( expectedDOM )
303
+ result1 = serializeInner ( root ) === expectedDOM
299
304
300
305
// in a pre-flush callback, all state should have been updated
301
- const expectedState = callCount === 1 ? 0 : 1
302
- expect ( count2Value ) . toBe ( expectedState )
306
+ const expectedState = callCount - 1
307
+ result2 = count === expectedState && count2Value === expectedState
303
308
} )
304
309
305
310
const Comp = {
@@ -318,30 +323,36 @@ describe('api: watch', () => {
318
323
const root = nodeOps . createElement ( 'div' )
319
324
render ( h ( Comp ) , root )
320
325
expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
326
+ expect ( result1 ) . toBe ( true )
327
+ expect ( result2 ) . toBe ( true )
321
328
322
329
count . value ++
323
330
count2 . value ++
324
331
await nextTick ( )
325
332
// two mutations should result in 1 callback execution
326
333
expect ( assertion ) . toHaveBeenCalledTimes ( 2 )
334
+ expect ( result1 ) . toBe ( true )
335
+ expect ( result2 ) . toBe ( true )
327
336
} )
328
337
329
338
it ( 'flush timing: sync' , async ( ) => {
330
339
const count = ref ( 0 )
331
340
const count2 = ref ( 0 )
332
341
333
342
let callCount = 0
343
+ let result1
344
+ let result2
334
345
const assertion = jest . fn ( count => {
335
346
callCount ++
336
347
// on mount, the watcher callback should be called before DOM render
337
348
// on update, should be called before the count is updated
338
349
const expectedDOM = callCount === 1 ? `` : `${ count - 1 } `
339
- expect ( serializeInner ( root ) ) . toBe ( expectedDOM )
350
+ result1 = serializeInner ( root ) === expectedDOM
340
351
341
352
// in a sync callback, state mutation on the next line should not have
342
353
// executed yet on the 2nd call, but will be on the 3rd call.
343
354
const expectedState = callCount < 3 ? 0 : 1
344
- expect ( count2 . value ) . toBe ( expectedState )
355
+ result2 = count2 . value === expectedState
345
356
} )
346
357
347
358
const Comp = {
@@ -360,11 +371,57 @@ describe('api: watch', () => {
360
371
const root = nodeOps . createElement ( 'div' )
361
372
render ( h ( Comp ) , root )
362
373
expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
374
+ expect ( result1 ) . toBe ( true )
375
+ expect ( result2 ) . toBe ( true )
363
376
364
377
count . value ++
365
378
count2 . value ++
366
379
await nextTick ( )
367
380
expect ( assertion ) . toHaveBeenCalledTimes ( 3 )
381
+ expect ( result1 ) . toBe ( true )
382
+ expect ( result2 ) . toBe ( true )
383
+ } )
384
+
385
+ it ( 'should not fire on component unmount w/ flush: post' , async ( ) => {
386
+ const toggle = ref ( true )
387
+ const cb = jest . fn ( )
388
+ const Comp = {
389
+ setup ( ) {
390
+ watch ( toggle , cb )
391
+ } ,
392
+ render ( ) { }
393
+ }
394
+ const App = {
395
+ render ( ) {
396
+ return toggle . value ? h ( Comp ) : null
397
+ }
398
+ }
399
+ render ( h ( App ) , nodeOps . createElement ( 'div' ) )
400
+ expect ( cb ) . not . toHaveBeenCalled ( )
401
+ toggle . value = false
402
+ await nextTick ( )
403
+ expect ( cb ) . not . toHaveBeenCalled ( )
404
+ } )
405
+
406
+ it ( 'should fire on component unmount w/ flush: pre' , async ( ) => {
407
+ const toggle = ref ( true )
408
+ const cb = jest . fn ( )
409
+ const Comp = {
410
+ setup ( ) {
411
+ watch ( toggle , cb , { flush : 'pre' } )
412
+ } ,
413
+ render ( ) { }
414
+ }
415
+ const App = {
416
+ render ( ) {
417
+ return toggle . value ? h ( Comp ) : null
418
+ }
419
+ }
420
+ render ( h ( App ) , nodeOps . createElement ( 'div' ) )
421
+ expect ( cb ) . not . toHaveBeenCalled ( )
422
+ toggle . value = false
423
+ await nextTick ( )
424
+ expect ( cb ) . toHaveBeenCalledTimes ( 1 )
368
425
} )
369
426
370
427
it ( 'deep' , async ( ) => {
0 commit comments