1
+ import type { Scheduler , SchedulerJob } from '../src/baseWatch'
1
2
import {
2
- type Scheduler ,
3
- type SchedulerJob ,
3
+ BaseWatchErrorCodes ,
4
+ EffectScope ,
5
+ type Ref ,
4
6
baseWatch ,
5
7
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'
9
10
10
11
const queue : SchedulerJob [ ] = [ ]
11
12
13
+ // these codes are a simple scheduler
12
14
let isFlushPending = false
13
15
const resolvedPromise = /*#__PURE__*/ Promise . resolve ( ) as Promise < any >
14
16
const nextTick = ( fn ?: ( ) => any ) =>
@@ -27,8 +29,75 @@ const flushJobs = () => {
27
29
} )
28
30
}
29
31
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 ( ) => {
32
101
let dummy = 0
33
102
let source : Ref < number >
34
103
const scope = new EffectScope ( )
@@ -59,7 +128,7 @@ describe('baseWatch with onEffectCleanup', () => {
59
128
expect ( dummy ) . toBe ( 30 )
60
129
} )
61
130
62
- test ( 'nested call to baseWatch' , async ( ) => {
131
+ test ( 'nested calls to baseWatch and onEffectCleanup ' , async ( ) => {
63
132
let calls : string [ ] = [ ]
64
133
let source : Ref < number >
65
134
let copyist : Ref < number >
0 commit comments