@@ -16,7 +16,9 @@ import {
16
16
serializeInner ,
17
17
TestElement ,
18
18
h ,
19
- createApp
19
+ createApp ,
20
+ watchPostEffect ,
21
+ watchSyncEffect
20
22
} from '@vue/runtime-test'
21
23
import {
22
24
ITERATE_KEY ,
@@ -28,7 +30,6 @@ import {
28
30
Ref ,
29
31
effectScope
30
32
} from '@vue/reactivity'
31
- import { watchPostEffect } from '../src/apiWatch'
32
33
33
34
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
34
35
@@ -444,6 +445,48 @@ describe('api: watch', () => {
444
445
expect ( result2 ) . toBe ( true )
445
446
} )
446
447
448
+ it ( 'watchSyncEffect' , async ( ) => {
449
+ const count = ref ( 0 )
450
+ const count2 = ref ( 0 )
451
+
452
+ let callCount = 0
453
+ let result1
454
+ let result2
455
+ const assertion = jest . fn ( count => {
456
+ callCount ++
457
+ // on mount, the watcher callback should be called before DOM render
458
+ // on update, should be called before the count is updated
459
+ const expectedDOM = callCount === 1 ? `` : `${ count - 1 } `
460
+ result1 = serializeInner ( root ) === expectedDOM
461
+
462
+ // in a sync callback, state mutation on the next line should not have
463
+ // executed yet on the 2nd call, but will be on the 3rd call.
464
+ const expectedState = callCount < 3 ? 0 : 1
465
+ result2 = count2 . value === expectedState
466
+ } )
467
+
468
+ const Comp = {
469
+ setup ( ) {
470
+ watchSyncEffect ( ( ) => {
471
+ assertion ( count . value )
472
+ } )
473
+ return ( ) => count . value
474
+ }
475
+ }
476
+ const root = nodeOps . createElement ( 'div' )
477
+ render ( h ( Comp ) , root )
478
+ expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
479
+ expect ( result1 ) . toBe ( true )
480
+ expect ( result2 ) . toBe ( true )
481
+
482
+ count . value ++
483
+ count2 . value ++
484
+ await nextTick ( )
485
+ expect ( assertion ) . toHaveBeenCalledTimes ( 3 )
486
+ expect ( result1 ) . toBe ( true )
487
+ expect ( result2 ) . toBe ( true )
488
+ } )
489
+
447
490
it ( 'should not fire on component unmount w/ flush: post' , async ( ) => {
448
491
const toggle = ref ( true )
449
492
const cb = jest . fn ( )
0 commit comments