Skip to content

Commit

Permalink
test: onWatcherCleanup in apiWatch
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Mar 13, 2024
1 parent a5769e1 commit 74996b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export {
enableTracking,
pauseTracking,
resetTracking,
onEffectCleanup,
ReactiveEffect,
EffectFlags,
type ReactiveEffectRunner,
Expand Down
30 changes: 30 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
defineComponent,
getCurrentInstance,
nextTick,
onWatcherCleanup,
reactive,
ref,
watch,
Expand Down Expand Up @@ -394,6 +395,35 @@ describe('api: watch', () => {
expect(cleanup).toHaveBeenCalledTimes(2)
})

it('onWatcherCleanup', async () => {
const count = ref(0)
const cleanupEffect = vi.fn()
const cleanupWatch = vi.fn()

const stopEffect = watchEffect(() => {
onWatcherCleanup(cleanupEffect)
count.value
})
const stopWatch = watch(count, () => {
onWatcherCleanup(cleanupWatch)
})

count.value++
await nextTick()
expect(cleanupEffect).toHaveBeenCalledTimes(1)
expect(cleanupWatch).toHaveBeenCalledTimes(0)

count.value++
await nextTick()
expect(cleanupEffect).toHaveBeenCalledTimes(2)
expect(cleanupWatch).toHaveBeenCalledTimes(1)

stopEffect()
expect(cleanupEffect).toHaveBeenCalledTimes(3)
stopWatch()
expect(cleanupWatch).toHaveBeenCalledTimes(2)
})

it('flush timing: pre (default)', async () => {
const count = ref(0)
const count2 = ref(0)
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
// effect
effect,
stop,
onWatcherCleanup,
ReactiveEffect,
// effect scope
effectScope,
Expand Down

0 comments on commit 74996b6

Please sign in to comment.