Skip to content

Commit e9555ce

Browse files
committed
test: baseWatch
1 parent d99e9a6 commit e9555ce

File tree

1 file changed

+77
-8
lines changed

1 file changed

+77
-8
lines changed

packages/reactivity/__tests__/baseWatch.spec.ts

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import type { Scheduler, SchedulerJob } from '../src/baseWatch'
12
import {
2-
type Scheduler,
3-
type SchedulerJob,
3+
BaseWatchErrorCodes,
4+
EffectScope,
5+
type Ref,
46
baseWatch,
57
onEffectCleanup,
6-
} from '../src/baseWatch'
7-
import { EffectScope } from '../src/effectScope'
8-
import { type Ref, ref } from '../src/ref'
8+
ref,
9+
} from '../src/index'
910

1011
const queue: SchedulerJob[] = []
1112

13+
// these codes are a simple scheduler
1214
let isFlushPending = false
1315
const resolvedPromise = /*#__PURE__*/ Promise.resolve() as Promise<any>
1416
const nextTick = (fn?: () => any) =>
@@ -27,8 +29,75 @@ const flushJobs = () => {
2729
})
2830
}
2931

30-
describe('baseWatch with onEffectCleanup', () => {
31-
test('basic', async () => {
32+
describe('baseWatch', () => {
33+
test('effect', () => {
34+
let dummy: any
35+
const source = ref(0)
36+
baseWatch(() => {
37+
dummy = source.value
38+
})
39+
expect(dummy).toBe(0)
40+
source.value++
41+
expect(dummy).toBe(1)
42+
})
43+
44+
test('watch', () => {
45+
let dummy: any
46+
const source = ref(0)
47+
baseWatch(source, () => {
48+
dummy = source.value
49+
})
50+
expect(dummy).toBe(undefined)
51+
source.value++
52+
expect(dummy).toBe(1)
53+
})
54+
55+
test('custom error handler', () => {
56+
const handleError = vi.fn()
57+
58+
baseWatch(
59+
() => {
60+
throw 'oops in effect'
61+
},
62+
null,
63+
{ handleError },
64+
)
65+
66+
const source = ref(0)
67+
const stop = baseWatch(
68+
source,
69+
() => {
70+
onEffectCleanup(() => {
71+
throw 'oops in cleanup'
72+
})
73+
throw 'oops in watch'
74+
},
75+
{ handleError },
76+
)
77+
78+
expect(handleError.mock.calls.length).toBe(1)
79+
expect(handleError.mock.calls[0]).toMatchObject([
80+
'oops in effect',
81+
BaseWatchErrorCodes.WATCH_CALLBACK,
82+
])
83+
84+
source.value++
85+
expect(handleError.mock.calls.length).toBe(2)
86+
expect(handleError.mock.calls[1]).toMatchObject([
87+
'oops in watch',
88+
BaseWatchErrorCodes.WATCH_CALLBACK,
89+
])
90+
91+
stop()
92+
source.value++
93+
expect(handleError.mock.calls.length).toBe(3)
94+
expect(handleError.mock.calls[2]).toMatchObject([
95+
'oops in cleanup',
96+
BaseWatchErrorCodes.WATCH_CLEANUP,
97+
])
98+
})
99+
100+
test('baseWatch with onEffectCleanup', async () => {
32101
let dummy = 0
33102
let source: Ref<number>
34103
const scope = new EffectScope()
@@ -59,7 +128,7 @@ describe('baseWatch with onEffectCleanup', () => {
59128
expect(dummy).toBe(30)
60129
})
61130

62-
test('nested call to baseWatch', async () => {
131+
test('nested calls to baseWatch and onEffectCleanup', async () => {
63132
let calls: string[] = []
64133
let source: Ref<number>
65134
let copyist: Ref<number>

0 commit comments

Comments
 (0)