Skip to content

Commit d99e9a6

Browse files
committed
test: onEffectCleanup in runtime-core
1 parent 56c87ec commit d99e9a6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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+
onEffectCleanup,
89
reactive,
910
ref,
1011
watch,
@@ -320,6 +321,35 @@ describe('api: watch', () => {
320321
expect(cleanup).toHaveBeenCalledTimes(2)
321322
})
322323

324+
it('onEffectCleanup', async () => {
325+
const count = ref(0)
326+
const cleanupEffect = vi.fn()
327+
const cleanupWatch = vi.fn()
328+
329+
const stopEffect = watchEffect(() => {
330+
onEffectCleanup(cleanupEffect)
331+
count.value
332+
})
333+
const stopWatch = watch(count, () => {
334+
onEffectCleanup(cleanupWatch)
335+
})
336+
337+
count.value++
338+
await nextTick()
339+
expect(cleanupEffect).toHaveBeenCalledTimes(1)
340+
expect(cleanupWatch).toHaveBeenCalledTimes(0)
341+
342+
count.value++
343+
await nextTick()
344+
expect(cleanupEffect).toHaveBeenCalledTimes(2)
345+
expect(cleanupWatch).toHaveBeenCalledTimes(1)
346+
347+
stopEffect()
348+
expect(cleanupEffect).toHaveBeenCalledTimes(3)
349+
stopWatch()
350+
expect(cleanupWatch).toHaveBeenCalledTimes(2)
351+
})
352+
323353
it('flush timing: pre (default)', async () => {
324354
const count = ref(0)
325355
const count2 = ref(0)

0 commit comments

Comments
 (0)