1
- import { reactive , effect , EffectScope , onDispose } from '../src'
1
+ import { nextTick , watch , watchEffect } from '@vue/runtime-core'
2
+ import {
3
+ reactive ,
4
+ effect ,
5
+ EffectScope ,
6
+ onDispose ,
7
+ computed ,
8
+ ref ,
9
+ ComputedRef
10
+ } from '../src'
2
11
3
12
describe ( 'reactivity/effect/scope' , ( ) => {
4
13
it ( 'should run the passed function once (wrapped by a effect)' , ( ) => {
@@ -173,7 +182,7 @@ describe('reactivity/effect/scope', () => {
173
182
expect ( dummy ) . toBe ( 7 )
174
183
} )
175
184
176
- it ( 'should fire onScopeStopped hook' , ( ) => {
185
+ it ( 'should fire onDispose hook' , ( ) => {
177
186
let dummy = 0
178
187
179
188
const scope = new EffectScope ( ( ) => {
@@ -190,4 +199,48 @@ describe('reactivity/effect/scope', () => {
190
199
scope . stop ( )
191
200
expect ( dummy ) . toBe ( 7 )
192
201
} )
202
+
203
+ it ( 'test with higher level APIs' , async ( ) => {
204
+ const r = ref ( 1 )
205
+
206
+ const computedSpy = jest . fn ( )
207
+ const watchSpy = jest . fn ( )
208
+ const watchEffectSpy = jest . fn ( )
209
+
210
+ let c : ComputedRef
211
+ const scope = new EffectScope ( ( ) => {
212
+ c = computed ( ( ) => {
213
+ computedSpy ( )
214
+ return r . value + 1
215
+ } )
216
+
217
+ watch ( r , watchSpy )
218
+ watchEffect ( ( ) => {
219
+ watchEffectSpy ( )
220
+ r . value
221
+ } )
222
+ } )
223
+
224
+ c ! . value // computed is lazy so trigger collection
225
+ expect ( computedSpy ) . toHaveBeenCalledTimes ( 1 )
226
+ expect ( watchSpy ) . toHaveBeenCalledTimes ( 0 )
227
+ expect ( watchEffectSpy ) . toHaveBeenCalledTimes ( 1 )
228
+
229
+ r . value ++
230
+ c ! . value
231
+ await nextTick ( )
232
+ expect ( computedSpy ) . toHaveBeenCalledTimes ( 2 )
233
+ expect ( watchSpy ) . toHaveBeenCalledTimes ( 1 )
234
+ expect ( watchEffectSpy ) . toHaveBeenCalledTimes ( 2 )
235
+
236
+ scope . stop ( )
237
+
238
+ r . value ++
239
+ c ! . value
240
+ await nextTick ( )
241
+ // should not trigger anymore
242
+ expect ( computedSpy ) . toHaveBeenCalledTimes ( 2 )
243
+ expect ( watchSpy ) . toHaveBeenCalledTimes ( 1 )
244
+ expect ( watchEffectSpy ) . toHaveBeenCalledTimes ( 2 )
245
+ } )
193
246
} )
0 commit comments