Skip to content

Commit

Permalink
test: useEffectScope script
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Aug 14, 2024
1 parent 6d23929 commit 73cfbf4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tests/effectScope.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { test, expect } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import { ref, reactive, useRef, watch, EffectScope, onUpdated, useEffectScope } from '../src'
import { ref, reactive, useRef, watch, EffectScope, onUpdated, useEffectScope, onScopeDispose } from '../src'

test('<useEffectScope>', () => {
let renderCount = 0
let scopeRunCount = 0
let scopeDisposed = false
const foo = ref(0)
const bar = reactive({ count: 10 })

Expand All @@ -23,6 +24,9 @@ test('<useEffectScope>', () => {
watch(bar, () => {
barWatchChanged.value++
})
onScopeDispose(() => {
scopeDisposed = true
})
})

return {
Expand All @@ -34,6 +38,7 @@ test('<useEffectScope>', () => {

expect(renderCount).toBe(0)
expect(scopeRunCount).toBe(1)
expect(scopeDisposed).toBe(false)
expect(result.current.scope).toBeInstanceOf(EffectScope)

act(() => foo.value++)
Expand All @@ -56,18 +61,21 @@ test('<useEffectScope>', () => {
expect(foo.value).toBe(2)
expect(bar.count).toBe(12)
expect(renderCount).toBe(2)
expect(scopeDisposed).toBe(false)

act(() => result.current.scope.resume())
expect(result.current.fooWatchChanged.value).toBe(2)
expect(result.current.barWatchChanged.value).toBe(2)
expect(renderCount).toBe(3)
expect(scopeDisposed).toBe(false)

act(() => result.current.scope.stop())
act(() => foo.value++)
act(() => bar.count++)
expect(result.current.fooWatchChanged.value).toBe(2)
expect(result.current.barWatchChanged.value).toBe(2)
expect(renderCount).toBe(3)
expect(scopeDisposed).toBe(true)

act(() => result.current.scope.resume())
act(() => foo.value++)
Expand Down

0 comments on commit 73cfbf4

Please sign in to comment.