Skip to content

Commit 74996b6

Browse files
committed
test: onWatcherCleanup in apiWatch
1 parent a5769e1 commit 74996b6

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

packages/reactivity/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export {
5353
enableTracking,
5454
pauseTracking,
5555
resetTracking,
56+
onEffectCleanup,
5657
ReactiveEffect,
5758
EffectFlags,
5859
type ReactiveEffectRunner,

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
defineComponent,
66
getCurrentInstance,
77
nextTick,
8+
onWatcherCleanup,
89
reactive,
910
ref,
1011
watch,
@@ -394,6 +395,35 @@ describe('api: watch', () => {
394395
expect(cleanup).toHaveBeenCalledTimes(2)
395396
})
396397

398+
it('onWatcherCleanup', async () => {
399+
const count = ref(0)
400+
const cleanupEffect = vi.fn()
401+
const cleanupWatch = vi.fn()
402+
403+
const stopEffect = watchEffect(() => {
404+
onWatcherCleanup(cleanupEffect)
405+
count.value
406+
})
407+
const stopWatch = watch(count, () => {
408+
onWatcherCleanup(cleanupWatch)
409+
})
410+
411+
count.value++
412+
await nextTick()
413+
expect(cleanupEffect).toHaveBeenCalledTimes(1)
414+
expect(cleanupWatch).toHaveBeenCalledTimes(0)
415+
416+
count.value++
417+
await nextTick()
418+
expect(cleanupEffect).toHaveBeenCalledTimes(2)
419+
expect(cleanupWatch).toHaveBeenCalledTimes(1)
420+
421+
stopEffect()
422+
expect(cleanupEffect).toHaveBeenCalledTimes(3)
423+
stopWatch()
424+
expect(cleanupWatch).toHaveBeenCalledTimes(2)
425+
})
426+
397427
it('flush timing: pre (default)', async () => {
398428
const count = ref(0)
399429
const count2 = ref(0)

packages/runtime-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export {
2828
// effect
2929
effect,
3030
stop,
31+
onWatcherCleanup,
3132
ReactiveEffect,
3233
// effect scope
3334
effectScope,

0 commit comments

Comments
 (0)