Skip to content

Commit 894aefd

Browse files
committed
chore: update test
1 parent 490eca4 commit 894aefd

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

packages/reactivity/__tests__/effectScope.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,44 @@ describe('reactivity/effect/scope', () => {
322322
scope.resume()
323323
expect(fnSpy).toHaveBeenCalledTimes(3)
324324
})
325+
326+
test('clean up watchers during effect scope stop', async () => {
327+
const count = ref(0)
328+
const scope = effectScope()
329+
let watcherCalls = 0
330+
let cleanupCalls = 0
331+
332+
scope.run(() => {
333+
const stop1 = watch(count, () => {
334+
watcherCalls++
335+
})
336+
watch(count, (val, old, onCleanup) => {
337+
watcherCalls++
338+
onCleanup(() => {
339+
cleanupCalls++
340+
stop1()
341+
})
342+
})
343+
watch(count, () => {
344+
watcherCalls++
345+
})
346+
})
347+
348+
expect(watcherCalls).toBe(0)
349+
expect(cleanupCalls).toBe(0)
350+
351+
count.value++
352+
await nextTick()
353+
expect(watcherCalls).toBe(3)
354+
expect(cleanupCalls).toBe(0)
355+
356+
scope.stop()
357+
count.value++
358+
await nextTick()
359+
expect(watcherCalls).toBe(3)
360+
expect(cleanupCalls).toBe(1)
361+
362+
expect(scope.effects.length).toBe(0)
363+
expect(scope.cleanups.length).toBe(0)
364+
})
325365
})

packages/reactivity/__tests__/watch.spec.ts

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
type WatchOptions,
66
type WatchScheduler,
77
computed,
8-
effectScope,
98
onWatcherCleanup,
109
ref,
1110
watch,
@@ -278,44 +277,4 @@ describe('watch', () => {
278277

279278
expect(dummy).toEqual([1, 2, 3])
280279
})
281-
282-
test('clean up watchers during effect scope stop', async () => {
283-
const count = ref(0)
284-
const scope = effectScope()
285-
let watcherCalls = 0
286-
let cleanupCalls = 0
287-
288-
scope.run(() => {
289-
const stop1 = watch(count, () => {
290-
watcherCalls++
291-
})
292-
watch(count, (val, old, onCleanup) => {
293-
watcherCalls++
294-
onCleanup(() => {
295-
cleanupCalls++
296-
stop1()
297-
})
298-
})
299-
watch(count, () => {
300-
watcherCalls++
301-
})
302-
})
303-
304-
expect(watcherCalls).toBe(0)
305-
expect(cleanupCalls).toBe(0)
306-
307-
count.value++
308-
await nextTick()
309-
expect(watcherCalls).toBe(3)
310-
expect(cleanupCalls).toBe(0)
311-
312-
scope.stop()
313-
count.value++
314-
await nextTick()
315-
expect(watcherCalls).toBe(3)
316-
expect(cleanupCalls).toBe(1)
317-
318-
expect(scope.effects.length).toBe(0)
319-
expect(scope.cleanups.length).toBe(0)
320-
})
321280
})

0 commit comments

Comments
 (0)