@@ -13,47 +13,19 @@ import { mockWarn } from '@vue/shared'
13
13
describe ( 'api: watch' , ( ) => {
14
14
mockWarn ( )
15
15
16
- it ( 'basic usage ' , async ( ) => {
16
+ it ( 'watch(effect) ' , async ( ) => {
17
17
const state = reactive ( { count : 0 } )
18
18
let dummy
19
19
watch ( ( ) => {
20
20
dummy = state . count
21
21
} )
22
- await nextTick ( )
23
22
expect ( dummy ) . toBe ( 0 )
24
23
25
24
state . count ++
26
25
await nextTick ( )
27
26
expect ( dummy ) . toBe ( 1 )
28
27
} )
29
28
30
- it ( 'triggers when initial value is null' , async ( ) => {
31
- const state = ref ( null )
32
- const spy = jest . fn ( )
33
- watch ( ( ) => state . value , spy )
34
- await nextTick ( )
35
- expect ( spy ) . toHaveBeenCalled ( )
36
- } )
37
-
38
- it ( 'triggers when initial value is undefined' , async ( ) => {
39
- const state = ref ( )
40
- const spy = jest . fn ( )
41
- watch ( ( ) => state . value , spy )
42
- await nextTick ( )
43
- expect ( spy ) . toHaveBeenCalled ( )
44
- state . value = 3
45
- await nextTick ( )
46
- expect ( spy ) . toHaveBeenCalledTimes ( 2 )
47
- // testing if undefined can trigger the watcher
48
- state . value = undefined
49
- await nextTick ( )
50
- expect ( spy ) . toHaveBeenCalledTimes ( 3 )
51
- // it shouldn't trigger if the same value is set
52
- state . value = undefined
53
- await nextTick ( )
54
- expect ( spy ) . toHaveBeenCalledTimes ( 3 )
55
- } )
56
-
57
29
it ( 'watching single source: getter' , async ( ) => {
58
30
const state = reactive ( { count : 0 } )
59
31
let dummy
@@ -68,9 +40,6 @@ describe('api: watch', () => {
68
40
}
69
41
}
70
42
)
71
- await nextTick ( )
72
- expect ( dummy ) . toMatchObject ( [ 0 , undefined ] )
73
-
74
43
state . count ++
75
44
await nextTick ( )
76
45
expect ( dummy ) . toMatchObject ( [ 1 , 0 ] )
@@ -87,9 +56,6 @@ describe('api: watch', () => {
87
56
prevCount + 1
88
57
}
89
58
} )
90
- await nextTick ( )
91
- expect ( dummy ) . toMatchObject ( [ 0 , undefined ] )
92
-
93
59
count . value ++
94
60
await nextTick ( )
95
61
expect ( dummy ) . toMatchObject ( [ 1 , 0 ] )
@@ -107,9 +73,6 @@ describe('api: watch', () => {
107
73
prevCount + 1
108
74
}
109
75
} )
110
- await nextTick ( )
111
- expect ( dummy ) . toMatchObject ( [ 1 , undefined ] )
112
-
113
76
count . value ++
114
77
await nextTick ( )
115
78
expect ( dummy ) . toMatchObject ( [ 2 , 1 ] )
@@ -127,8 +90,6 @@ describe('api: watch', () => {
127
90
vals . concat ( 1 )
128
91
oldVals . concat ( 1 )
129
92
} )
130
- await nextTick ( )
131
- expect ( dummy ) . toMatchObject ( [ [ 1 , 1 , 2 ] , [ ] ] )
132
93
133
94
state . count ++
134
95
count . value ++
@@ -149,8 +110,6 @@ describe('api: watch', () => {
149
110
count + 1
150
111
oldStatus === true
151
112
} )
152
- await nextTick ( )
153
- expect ( dummy ) . toMatchObject ( [ [ 1 , false ] , [ ] ] )
154
113
155
114
state . count ++
156
115
status . value = true
@@ -164,7 +123,6 @@ describe('api: watch', () => {
164
123
const stop = watch ( ( ) => {
165
124
dummy = state . count
166
125
} )
167
- await nextTick ( )
168
126
expect ( dummy ) . toBe ( 0 )
169
127
170
128
stop ( )
@@ -174,15 +132,14 @@ describe('api: watch', () => {
174
132
expect ( dummy ) . toBe ( 0 )
175
133
} )
176
134
177
- it ( 'cleanup registration (basic )' , async ( ) => {
135
+ it ( 'cleanup registration (effect )' , async ( ) => {
178
136
const state = reactive ( { count : 0 } )
179
137
const cleanup = jest . fn ( )
180
138
let dummy
181
139
const stop = watch ( onCleanup => {
182
140
onCleanup ( cleanup )
183
141
dummy = state . count
184
142
} )
185
- await nextTick ( )
186
143
expect ( dummy ) . toBe ( 0 )
187
144
188
145
state . count ++
@@ -202,22 +159,30 @@ describe('api: watch', () => {
202
159
onCleanup ( cleanup )
203
160
dummy = count
204
161
} )
162
+
163
+ count . value ++
205
164
await nextTick ( )
206
- expect ( dummy ) . toBe ( 0 )
165
+ expect ( cleanup ) . toHaveBeenCalledTimes ( 0 )
166
+ expect ( dummy ) . toBe ( 1 )
207
167
208
168
count . value ++
209
169
await nextTick ( )
210
170
expect ( cleanup ) . toHaveBeenCalledTimes ( 1 )
211
- expect ( dummy ) . toBe ( 1 )
171
+ expect ( dummy ) . toBe ( 2 )
212
172
213
173
stop ( )
214
174
expect ( cleanup ) . toHaveBeenCalledTimes ( 2 )
215
175
} )
216
176
217
- it ( 'flush timing: post' , async ( ) => {
177
+ it ( 'flush timing: post (default) ' , async ( ) => {
218
178
const count = ref ( 0 )
179
+ let callCount = 0
219
180
const assertion = jest . fn ( count => {
220
- expect ( serializeInner ( root ) ) . toBe ( `${ count } ` )
181
+ callCount ++
182
+ // on mount, the watcher callback should be called before DOM render
183
+ // on update, should be called after the count is updated
184
+ const expectedDOM = callCount === 1 ? `` : `${ count } `
185
+ expect ( serializeInner ( root ) ) . toBe ( expectedDOM )
221
186
} )
222
187
223
188
const Comp = {
@@ -230,7 +195,6 @@ describe('api: watch', () => {
230
195
}
231
196
const root = nodeOps . createElement ( 'div' )
232
197
render ( h ( Comp ) , root )
233
- await nextTick ( )
234
198
expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
235
199
236
200
count . value ++
@@ -270,7 +234,6 @@ describe('api: watch', () => {
270
234
}
271
235
const root = nodeOps . createElement ( 'div' )
272
236
render ( h ( Comp ) , root )
273
- await nextTick ( )
274
237
expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
275
238
276
239
count . value ++
@@ -313,7 +276,6 @@ describe('api: watch', () => {
313
276
}
314
277
const root = nodeOps . createElement ( 'div' )
315
278
render ( h ( Comp ) , root )
316
- await nextTick ( )
317
279
expect ( assertion ) . toHaveBeenCalledTimes ( 1 )
318
280
319
281
count . value ++
@@ -346,9 +308,6 @@ describe('api: watch', () => {
346
308
{ deep : true }
347
309
)
348
310
349
- await nextTick ( )
350
- expect ( dummy ) . toEqual ( [ 0 , 1 , 1 , true ] )
351
-
352
311
state . nested . count ++
353
312
await nextTick ( )
354
313
expect ( dummy ) . toEqual ( [ 1 , 1 , 1 , true ] )
@@ -369,32 +328,53 @@ describe('api: watch', () => {
369
328
expect ( dummy ) . toEqual ( [ 1 , 2 , 2 , false ] )
370
329
} )
371
330
372
- it ( 'lazy ' , async ( ) => {
331
+ it ( 'immediate ' , async ( ) => {
373
332
const count = ref ( 0 )
374
333
const cb = jest . fn ( )
375
- watch ( count , cb , { lazy : true } )
376
- await nextTick ( )
377
- expect ( cb ) . not . toHaveBeenCalled ( )
334
+ watch ( count , cb , { immediate : true } )
335
+ expect ( cb ) . toHaveBeenCalledTimes ( 1 )
378
336
count . value ++
379
337
await nextTick ( )
380
- expect ( cb ) . toHaveBeenCalled ( )
338
+ expect ( cb ) . toHaveBeenCalledTimes ( 2 )
381
339
} )
382
340
383
- it ( 'ignore lazy option when using simple callback' , async ( ) => {
341
+ it ( 'immediate: triggers when initial value is null' , async ( ) => {
342
+ const state = ref ( null )
343
+ const spy = jest . fn ( )
344
+ watch ( ( ) => state . value , spy , { immediate : true } )
345
+ expect ( spy ) . toHaveBeenCalled ( )
346
+ } )
347
+
348
+ it ( 'immediate: triggers when initial value is undefined' , async ( ) => {
349
+ const state = ref ( )
350
+ const spy = jest . fn ( )
351
+ watch ( ( ) => state . value , spy , { immediate : true } )
352
+ expect ( spy ) . toHaveBeenCalled ( )
353
+ state . value = 3
354
+ await nextTick ( )
355
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 )
356
+ // testing if undefined can trigger the watcher
357
+ state . value = undefined
358
+ await nextTick ( )
359
+ expect ( spy ) . toHaveBeenCalledTimes ( 3 )
360
+ // it shouldn't trigger if the same value is set
361
+ state . value = undefined
362
+ await nextTick ( )
363
+ expect ( spy ) . toHaveBeenCalledTimes ( 3 )
364
+ } )
365
+
366
+ it ( 'warn immediate option when using effect signature' , async ( ) => {
384
367
const count = ref ( 0 )
385
368
let dummy
386
369
// @ts -ignore
387
370
watch (
388
371
( ) => {
389
372
dummy = count . value
390
373
} ,
391
- { lazy : true }
374
+ { immediate : false }
392
375
)
393
- expect ( dummy ) . toBeUndefined ( )
394
- expect ( `lazy option is only respected` ) . toHaveBeenWarned ( )
395
-
396
- await nextTick ( )
397
376
expect ( dummy ) . toBe ( 0 )
377
+ expect ( `"immediate" option is only respected` ) . toHaveBeenWarned ( )
398
378
399
379
count . value ++
400
380
await nextTick ( )
0 commit comments