1
1
import { setupRerender } from 'preact/test-utils' ;
2
2
import { createElement , render , Component , Fragment } from 'preact' ;
3
3
import { setupScratch , teardown } from '../../_util/helpers' ;
4
+ import { vi } from 'vitest' ;
4
5
5
6
/** @jsx createElement */
6
7
@@ -42,8 +43,8 @@ describe('Lifecycle methods', () => {
42
43
43
44
let thrower ;
44
45
45
- sinon . spy ( Receiver . prototype , 'componentDidCatch' ) ;
46
- sinon . spy ( Receiver . prototype , 'render' ) ;
46
+ vi . spyOn ( Receiver . prototype , 'componentDidCatch' ) ;
47
+ vi . spyOn ( Receiver . prototype , 'render' ) ;
47
48
48
49
function throwExpectedError ( ) {
49
50
throw ( expectedError = new Error ( 'Error!' ) ) ;
@@ -63,19 +64,19 @@ describe('Lifecycle methods', () => {
63
64
return < div > ThrowErr: componentDidCatch</ div > ;
64
65
}
65
66
} ;
66
- sinon . spy ( ThrowErr . prototype , 'componentDidCatch' ) ;
67
+ vi . spyOn ( ThrowErr . prototype , 'componentDidCatch' ) ;
67
68
68
69
expectedError = undefined ;
69
70
70
- Receiver . prototype . componentDidCatch . resetHistory ( ) ;
71
- Receiver . prototype . render . resetHistory ( ) ;
71
+ Receiver . prototype . componentDidCatch . mockClear ( ) ;
72
+ Receiver . prototype . render . mockClear ( ) ;
72
73
} ) ;
73
74
74
75
afterEach ( ( ) => {
75
76
expect (
76
77
ThrowErr . prototype . componentDidCatch ,
77
78
"Throwing component should not catch it's own error."
78
- ) . to . not . be . called ;
79
+ ) . not . toHaveBeenCalled ( ) ;
79
80
thrower = undefined ;
80
81
} ) ;
81
82
@@ -101,8 +102,9 @@ describe('Lifecycle methods', () => {
101
102
) ;
102
103
rerender ( ) ;
103
104
104
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
105
- expectedError
105
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
106
+ expectedError ,
107
+ expect . anything ( )
106
108
) ;
107
109
} ) ;
108
110
@@ -136,8 +138,9 @@ describe('Lifecycle methods', () => {
136
138
</ Receiver > ,
137
139
scratch
138
140
) ;
139
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
140
- expectedError
141
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
142
+ expectedError ,
143
+ expect . anything ( )
141
144
) ;
142
145
} ) ;
143
146
@@ -150,8 +153,9 @@ describe('Lifecycle methods', () => {
150
153
</ Receiver > ,
151
154
scratch
152
155
) ;
153
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
154
- expectedError
156
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
157
+ expectedError ,
158
+ expect . anything ( )
155
159
) ;
156
160
} ) ;
157
161
@@ -164,26 +168,28 @@ describe('Lifecycle methods', () => {
164
168
</ Receiver > ,
165
169
scratch
166
170
) ;
167
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
168
- expectedError
171
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
172
+ expectedError ,
173
+ expect . anything ( )
169
174
) ;
170
175
} ) ;
171
176
172
177
it ( 'should be called when child fails in getDerivedStateFromProps' , ( ) => {
173
178
ThrowErr . getDerivedStateFromProps = throwExpectedError ;
174
179
175
- sinon . spy ( ThrowErr . prototype , 'render' ) ;
180
+ vi . spyOn ( ThrowErr . prototype , 'render' ) ;
176
181
render (
177
182
< Receiver >
178
183
< ThrowErr />
179
184
</ Receiver > ,
180
185
scratch
181
186
) ;
182
187
183
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
184
- expectedError
188
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
189
+ expectedError ,
190
+ expect . anything ( )
185
191
) ;
186
- expect ( ThrowErr . prototype . render ) . not . to . have . been . called ;
192
+ expect ( ThrowErr . prototype . render ) . not . toHaveBeenCalled ( ) ;
187
193
} ) ;
188
194
189
195
it ( 'should be called when child fails in getSnapshotBeforeUpdate' , ( ) => {
@@ -198,8 +204,9 @@ describe('Lifecycle methods', () => {
198
204
thrower . forceUpdate ( ) ;
199
205
rerender ( ) ;
200
206
201
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
202
- expectedError
207
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
208
+ expectedError ,
209
+ expect . anything ( )
203
210
) ;
204
211
} ) ;
205
212
@@ -215,8 +222,9 @@ describe('Lifecycle methods', () => {
215
222
216
223
thrower . forceUpdate ( ) ;
217
224
rerender ( ) ;
218
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
219
- expectedError
225
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
226
+ expectedError ,
227
+ expect . anything ( )
220
228
) ;
221
229
} ) ;
222
230
@@ -232,8 +240,9 @@ describe('Lifecycle methods', () => {
232
240
233
241
thrower . forceUpdate ( ) ;
234
242
rerender ( ) ;
235
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
236
- expectedError
243
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
244
+ expectedError ,
245
+ expect . anything ( )
237
246
) ;
238
247
} ) ;
239
248
@@ -259,14 +268,15 @@ describe('Lifecycle methods', () => {
259
268
}
260
269
}
261
270
262
- sinon . spy ( Receiver . prototype , 'componentDidCatch' ) ;
271
+ vi . spyOn ( Receiver . prototype , 'componentDidCatch' ) ;
263
272
render ( < Receiver /> , scratch ) ;
264
273
265
274
receiver . setState ( { foo : 'baz' } ) ;
266
275
rerender ( ) ;
267
276
268
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
269
- expectedError
277
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
278
+ expectedError ,
279
+ expect . anything ( )
270
280
) ;
271
281
} ) ;
272
282
@@ -292,14 +302,15 @@ describe('Lifecycle methods', () => {
292
302
}
293
303
}
294
304
295
- sinon . spy ( Receiver . prototype , 'componentDidCatch' ) ;
305
+ vi . spyOn ( Receiver . prototype , 'componentDidCatch' ) ;
296
306
render ( < Receiver /> , scratch ) ;
297
307
298
308
receiver . setState ( { foo : 'baz' } ) ;
299
309
rerender ( ) ;
300
310
301
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
302
- expectedError
311
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
312
+ expectedError ,
313
+ expect . anything ( )
303
314
) ;
304
315
} ) ;
305
316
@@ -318,8 +329,9 @@ describe('Lifecycle methods', () => {
318
329
</ Receiver > ,
319
330
scratch
320
331
) ;
321
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
322
- expectedError
332
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
333
+ expectedError ,
334
+ expect . anything ( )
323
335
) ;
324
336
} ) ;
325
337
@@ -347,10 +359,11 @@ describe('Lifecycle methods', () => {
347
359
}
348
360
}
349
361
350
- sinon . spy ( Receiver . prototype , 'componentDidCatch' ) ;
362
+ vi . spyOn ( Receiver . prototype , 'componentDidCatch' ) ;
351
363
render ( < Receiver /> , scratch ) ;
352
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
353
- expectedError
364
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
365
+ expectedError ,
366
+ expect . anything ( )
354
367
) ;
355
368
} ) ;
356
369
@@ -376,10 +389,11 @@ describe('Lifecycle methods', () => {
376
389
}
377
390
}
378
391
379
- sinon . spy ( Receiver . prototype , 'componentDidCatch' ) ;
392
+ vi . spyOn ( Receiver . prototype , 'componentDidCatch' ) ;
380
393
render ( < Receiver /> , scratch ) ;
381
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
382
- expectedError
394
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
395
+ expectedError ,
396
+ expect . anything ( )
383
397
) ;
384
398
} ) ;
385
399
@@ -404,8 +418,10 @@ describe('Lifecycle methods', () => {
404
418
</ Receiver > ,
405
419
scratch
406
420
) ;
407
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledOnceWith (
408
- expectedError
421
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledOnce ( ) ;
422
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
423
+ expectedError ,
424
+ expect . anything ( )
409
425
) ;
410
426
} ) ;
411
427
@@ -420,8 +436,9 @@ describe('Lifecycle methods', () => {
420
436
</ Receiver > ,
421
437
scratch
422
438
) ;
423
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
424
- expectedError
439
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
440
+ expectedError ,
441
+ expect . anything ( )
425
442
) ;
426
443
} ) ;
427
444
@@ -438,8 +455,9 @@ describe('Lifecycle methods', () => {
438
455
</ Receiver > ,
439
456
scratch
440
457
) ;
441
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
442
- expectedError
458
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
459
+ expectedError ,
460
+ expect . anything ( )
443
461
) ;
444
462
} ) ;
445
463
@@ -481,7 +499,7 @@ describe('Lifecycle methods', () => {
481
499
throwExpectedError ( ) ;
482
500
}
483
501
484
- sinon . spy ( Adapter . prototype , 'componentDidCatch' ) ;
502
+ vi . spyOn ( Adapter . prototype , 'componentDidCatch' ) ;
485
503
render (
486
504
< Receiver >
487
505
< Adapter >
@@ -491,11 +509,13 @@ describe('Lifecycle methods', () => {
491
509
scratch
492
510
) ;
493
511
494
- expect ( Adapter . prototype . componentDidCatch ) . to . have . been . calledWith (
495
- expectedError
512
+ expect ( Adapter . prototype . componentDidCatch ) . toHaveBeenCalledWith (
513
+ expectedError ,
514
+ expect . anything ( )
496
515
) ;
497
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
498
- adaptedError
516
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
517
+ adaptedError ,
518
+ expect . anything ( )
499
519
) ;
500
520
501
521
rerender ( ) ;
@@ -521,7 +541,7 @@ describe('Lifecycle methods', () => {
521
541
throwExpectedError ( ) ;
522
542
}
523
543
524
- sinon . spy ( Adapter . prototype , 'componentDidCatch' ) ;
544
+ vi . spyOn ( Adapter . prototype , 'componentDidCatch' ) ;
525
545
526
546
render (
527
547
< Receiver >
@@ -533,11 +553,13 @@ describe('Lifecycle methods', () => {
533
553
) ;
534
554
rerender ( ) ;
535
555
536
- expect ( Adapter . prototype . componentDidCatch ) . to . have . been . calledWith (
537
- expectedError
556
+ expect ( Adapter . prototype . componentDidCatch ) . toHaveBeenCalledWith (
557
+ expectedError ,
558
+ expect . anything ( )
538
559
) ;
539
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
540
- expectedError
560
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
561
+ expectedError ,
562
+ expect . anything ( )
541
563
) ;
542
564
expect ( scratch ) . to . have . property ( 'textContent' , 'Error: Error!' ) ;
543
565
} ) ;
@@ -556,7 +578,7 @@ describe('Lifecycle methods', () => {
556
578
throw new Error ( 'Error!' ) ;
557
579
}
558
580
559
- sinon . spy ( Adapter . prototype , 'componentDidCatch' ) ;
581
+ vi . spyOn ( Adapter . prototype , 'componentDidCatch' ) ;
560
582
561
583
render (
562
584
< Receiver >
@@ -568,10 +590,8 @@ describe('Lifecycle methods', () => {
568
590
) ;
569
591
rerender ( ) ;
570
592
571
- expect ( Adapter . prototype . componentDidCatch , 'Adapter' ) . to . have . been
572
- . called ;
573
- expect ( Receiver . prototype . componentDidCatch , 'Receiver' ) . to . have . been
574
- . called ;
593
+ expect ( Adapter . prototype . componentDidCatch ) . toHaveBeenCalled ( ) ;
594
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalled ( ) ;
575
595
expect ( scratch ) . to . have . property ( 'textContent' , 'Error: Error!' ) ;
576
596
} ) ;
577
597
@@ -595,7 +615,7 @@ describe('Lifecycle methods', () => {
595
615
throwExpectedError ( ) ;
596
616
}
597
617
598
- sinon . spy ( TopReceiver . prototype , 'componentDidCatch' ) ;
618
+ vi . spyOn ( TopReceiver . prototype , 'componentDidCatch' ) ;
599
619
600
620
render (
601
621
< TopReceiver >
@@ -607,9 +627,10 @@ describe('Lifecycle methods', () => {
607
627
) ;
608
628
rerender ( ) ;
609
629
610
- expect ( TopReceiver . prototype . componentDidCatch ) . not . to . have . been . called ;
611
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
612
- expectedError
630
+ expect ( TopReceiver . prototype . componentDidCatch ) . not . toHaveBeenCalled ( ) ;
631
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
632
+ expectedError ,
633
+ expect . anything ( )
613
634
) ;
614
635
expect ( scratch ) . to . have . property ( 'textContent' , 'Error: Error!' ) ;
615
636
} ) ;
@@ -624,8 +645,9 @@ describe('Lifecycle methods', () => {
624
645
</ Receiver > ,
625
646
scratch
626
647
) ;
627
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
628
- expectedError
648
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
649
+ expectedError ,
650
+ expect . anything ( )
629
651
) ;
630
652
} ) ;
631
653
@@ -646,8 +668,9 @@ describe('Lifecycle methods', () => {
646
668
</ Receiver > ,
647
669
scratch
648
670
) ;
649
- expect ( Receiver . prototype . componentDidCatch ) . to . have . been . calledWith (
650
- expectedError
671
+ expect ( Receiver . prototype . componentDidCatch ) . toHaveBeenCalledWith (
672
+ expectedError ,
673
+ expect . anything ( )
651
674
) ;
652
675
} ) ;
653
676
0 commit comments