1- import { watch , reactive , computed , nextTick , ref , h } from '../src/index'
1+ import {
2+ watch ,
3+ watchEffect ,
4+ reactive ,
5+ computed ,
6+ nextTick ,
7+ ref ,
8+ h
9+ } from '../src/index'
210import { render , nodeOps , serializeInner } from '@vue/runtime-test'
311import {
412 ITERATE_KEY ,
@@ -13,10 +21,10 @@ import { mockWarn } from '@vue/shared'
1321describe ( 'api: watch' , ( ) => {
1422 mockWarn ( )
1523
16- it ( 'watch( effect) ' , async ( ) => {
24+ it ( 'effect' , async ( ) => {
1725 const state = reactive ( { count : 0 } )
1826 let dummy
19- watch ( ( ) => {
27+ watchEffect ( ( ) => {
2028 dummy = state . count
2129 } )
2230 expect ( dummy ) . toBe ( 0 )
@@ -117,10 +125,10 @@ describe('api: watch', () => {
117125 expect ( dummy ) . toMatchObject ( [ [ 2 , true ] , [ 1 , false ] ] )
118126 } )
119127
120- it ( 'stopping the watcher' , async ( ) => {
128+ it ( 'stopping the watcher (effect) ' , async ( ) => {
121129 const state = reactive ( { count : 0 } )
122130 let dummy
123- const stop = watch ( ( ) => {
131+ const stop = watchEffect ( ( ) => {
124132 dummy = state . count
125133 } )
126134 expect ( dummy ) . toBe ( 0 )
@@ -132,11 +140,32 @@ describe('api: watch', () => {
132140 expect ( dummy ) . toBe ( 0 )
133141 } )
134142
143+ it ( 'stopping the watcher (with source)' , async ( ) => {
144+ const state = reactive ( { count : 0 } )
145+ let dummy
146+ const stop = watch (
147+ ( ) => state . count ,
148+ count => {
149+ dummy = count
150+ }
151+ )
152+
153+ state . count ++
154+ await nextTick ( )
155+ expect ( dummy ) . toBe ( 1 )
156+
157+ stop ( )
158+ state . count ++
159+ await nextTick ( )
160+ // should not update
161+ expect ( dummy ) . toBe ( 1 )
162+ } )
163+
135164 it ( 'cleanup registration (effect)' , async ( ) => {
136165 const state = reactive ( { count : 0 } )
137166 const cleanup = jest . fn ( )
138167 let dummy
139- const stop = watch ( onCleanup => {
168+ const stop = watchEffect ( onCleanup => {
140169 onCleanup ( cleanup )
141170 dummy = state . count
142171 } )
@@ -187,7 +216,7 @@ describe('api: watch', () => {
187216
188217 const Comp = {
189218 setup ( ) {
190- watch ( ( ) => {
219+ watchEffect ( ( ) => {
191220 assertion ( count . value )
192221 } )
193222 return ( ) => count . value
@@ -221,7 +250,7 @@ describe('api: watch', () => {
221250
222251 const Comp = {
223252 setup ( ) {
224- watch (
253+ watchEffect (
225254 ( ) => {
226255 assertion ( count . value , count2 . value )
227256 } ,
@@ -263,7 +292,7 @@ describe('api: watch', () => {
263292
264293 const Comp = {
265294 setup ( ) {
266- watch (
295+ watchEffect (
267296 ( ) => {
268297 assertion ( count . value )
269298 } ,
@@ -363,14 +392,14 @@ describe('api: watch', () => {
363392 expect ( spy ) . toHaveBeenCalledTimes ( 3 )
364393 } )
365394
366- it ( 'warn immediate option when using effect signature ' , async ( ) => {
395+ it ( 'warn immediate option when using effect' , async ( ) => {
367396 const count = ref ( 0 )
368397 let dummy
369- // @ts -ignore
370- watch (
398+ watchEffect (
371399 ( ) => {
372400 dummy = count . value
373401 } ,
402+ // @ts -ignore
374403 { immediate : false }
375404 )
376405 expect ( dummy ) . toBe ( 0 )
@@ -388,7 +417,7 @@ describe('api: watch', () => {
388417 events . push ( e )
389418 } )
390419 const obj = reactive ( { foo : 1 , bar : 2 } )
391- watch (
420+ watchEffect (
392421 ( ) => {
393422 dummy = [ obj . foo , 'bar' in obj , Object . keys ( obj ) ]
394423 } ,
@@ -423,7 +452,7 @@ describe('api: watch', () => {
423452 events . push ( e )
424453 } )
425454 const obj = reactive ( { foo : 1 } )
426- watch (
455+ watchEffect (
427456 ( ) => {
428457 dummy = obj . foo
429458 } ,
0 commit comments