Skip to content

Commit 70adc07

Browse files
committed
test: add effectScope integration test
1 parent b0043ed commit 70adc07

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

packages/reactivity/__tests__/effectScope.spec.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
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'
211

312
describe('reactivity/effect/scope', () => {
413
it('should run the passed function once (wrapped by a effect)', () => {
@@ -173,7 +182,7 @@ describe('reactivity/effect/scope', () => {
173182
expect(dummy).toBe(7)
174183
})
175184

176-
it('should fire onScopeStopped hook', () => {
185+
it('should fire onDispose hook', () => {
177186
let dummy = 0
178187

179188
const scope = new EffectScope(() => {
@@ -190,4 +199,48 @@ describe('reactivity/effect/scope', () => {
190199
scope.stop()
191200
expect(dummy).toBe(7)
192201
})
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+
})
193246
})

0 commit comments

Comments
 (0)