@@ -5,16 +5,26 @@ import {
5
5
computed ,
6
6
nextTick ,
7
7
ref ,
8
- h
8
+ defineComponent ,
9
+ getCurrentInstance ,
10
+ ComponentInternalInstance ,
11
+ ComponentPublicInstance
9
12
} from '../src/index'
10
- import { render , nodeOps , serializeInner , TestElement } from '@vue/runtime-test'
13
+ import {
14
+ render ,
15
+ nodeOps ,
16
+ serializeInner ,
17
+ TestElement ,
18
+ h
19
+ } from '@vue/runtime-test'
11
20
import {
12
21
ITERATE_KEY ,
13
22
DebuggerEvent ,
14
23
TrackOpTypes ,
15
24
TriggerOpTypes ,
16
25
triggerRef ,
17
- shallowRef
26
+ shallowRef ,
27
+ Ref
18
28
} from '@vue/reactivity'
19
29
20
30
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
@@ -799,4 +809,52 @@ describe('api: watch', () => {
799
809
await nextTick ( )
800
810
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
801
811
} )
812
+
813
+ // https://github.com/vuejs/vue-next/issues/2381
814
+ test ( '$watch should always register its effects with itw own instance' , async ( ) => {
815
+ let instance : ComponentInternalInstance | null
816
+ let _show : Ref < boolean >
817
+
818
+ const Child = defineComponent ( {
819
+ render : ( ) => h ( 'div' ) ,
820
+ mounted ( ) {
821
+ instance = getCurrentInstance ( )
822
+ } ,
823
+ unmounted ( ) { }
824
+ } )
825
+
826
+ const Comp = defineComponent ( {
827
+ setup ( ) {
828
+ const comp = ref < ComponentPublicInstance | undefined > ( )
829
+ const show = ref ( true )
830
+ _show = show
831
+ return { comp, show }
832
+ } ,
833
+ render ( ) {
834
+ return this . show
835
+ ? h ( Child , {
836
+ ref : vm => void ( this . comp = vm as ComponentPublicInstance )
837
+ } )
838
+ : null
839
+ } ,
840
+ mounted ( ) {
841
+ // this call runs while Comp is currentInstance, but
842
+ // the effect for this `$watch` should nontheless be registered with Child
843
+ this . comp ! . $watch ( ( ) => this . show , ( ) => void 0 )
844
+ }
845
+ } )
846
+
847
+ render ( h ( Comp ) , nodeOps . createElement ( 'div' ) )
848
+
849
+ expect ( instance ! ) . toBeDefined ( )
850
+ expect ( instance ! . effects ) . toBeInstanceOf ( Array )
851
+ expect ( instance ! . effects ! . length ) . toBe ( 1 )
852
+
853
+ _show ! . value = false
854
+
855
+ await nextTick ( )
856
+ await nextTick ( )
857
+
858
+ expect ( instance ! . effects ! [ 0 ] . active ) . toBe ( false )
859
+ } )
802
860
} )
0 commit comments