8
8
checkHangingAct ,
9
9
getConsoleErrorSpy ,
10
10
} from "../../../test/shared/utils" ;
11
+ import { describe , expect , beforeEach , afterEach , vi , it } from "vitest" ;
11
12
12
13
const MANAGED_COMPONENT = 1 ;
13
14
const MANAGED_HOOK = 2 ;
@@ -52,7 +53,7 @@ describe("useSignals", () => {
52
53
scratch = document . createElement ( "div" ) ;
53
54
document . body . appendChild ( scratch ) ;
54
55
root = await createRoot ( scratch ) ;
55
- getConsoleErrorSpy ( ) . resetHistory ( ) ;
56
+ getConsoleErrorSpy ( ) . mockClear ( ) ;
56
57
} ) ;
57
58
58
59
afterEach ( async ( ) => {
@@ -142,23 +143,23 @@ describe("useSignals", () => {
142
143
} ) ;
143
144
144
145
it ( "should not rerender components when signals they use do not change" , async ( ) => {
145
- const child1Spy = sinon . spy ( ) ;
146
+ const child1Spy = vi . fn ( ) ;
146
147
const signal1 = signal ( 0 ) ;
147
148
function Child1 ( ) {
148
149
child1Spy ( ) ;
149
150
useSignals ( ) ;
150
151
return < p > { signal1 . value } </ p > ;
151
152
}
152
153
153
- const child2Spy = sinon . spy ( ) ;
154
+ const child2Spy = vi . fn ( ) ;
154
155
const signal2 = signal ( 0 ) ;
155
156
function Child2 ( ) {
156
157
child2Spy ( ) ;
157
158
useSignals ( ) ;
158
159
return < p > { signal2 . value } </ p > ;
159
160
}
160
161
161
- const parentSpy = sinon . spy ( ) ;
162
+ const parentSpy = vi . fn ( ) ;
162
163
function Parent ( ) {
163
164
parentSpy ( ) ;
164
165
return (
@@ -170,39 +171,39 @@ describe("useSignals", () => {
170
171
}
171
172
172
173
function resetSpies ( ) {
173
- child1Spy . resetHistory ( ) ;
174
- child2Spy . resetHistory ( ) ;
175
- parentSpy . resetHistory ( ) ;
174
+ child1Spy . mockClear ( ) ;
175
+ child2Spy . mockClear ( ) ;
176
+ parentSpy . mockClear ( ) ;
176
177
}
177
178
178
179
resetSpies ( ) ;
179
180
await render ( < Parent /> ) ;
180
181
expect ( scratch . innerHTML ) . to . equal ( "<p>0</p><p>0</p>" ) ;
181
- expect ( child1Spy ) . to . have . been . calledOnce ;
182
- expect ( child2Spy ) . to . have . been . calledOnce ;
183
- expect ( parentSpy ) . to . have . been . calledOnce ;
182
+ expect ( child1Spy ) . toHaveBeenCalledOnce ( ) ;
183
+ expect ( child2Spy ) . toHaveBeenCalledOnce ( ) ;
184
+ expect ( parentSpy ) . toHaveBeenCalledOnce ( ) ;
184
185
185
186
resetSpies ( ) ;
186
187
await act ( ( ) => {
187
188
signal1 . value += 1 ;
188
189
} ) ;
189
190
expect ( scratch . innerHTML ) . to . equal ( "<p>1</p><p>0</p>" ) ;
190
- expect ( child1Spy ) . to . have . been . calledOnce ;
191
- expect ( child2Spy ) . to . not . have . been . called ;
192
- expect ( parentSpy ) . to . not . have . been . called ;
191
+ expect ( child1Spy ) . toHaveBeenCalledOnce ( ) ;
192
+ expect ( child2Spy ) . not . toHaveBeenCalled ( ) ;
193
+ expect ( parentSpy ) . not . toHaveBeenCalled ( ) ;
193
194
194
195
resetSpies ( ) ;
195
196
await act ( ( ) => {
196
197
signal2 . value += 1 ;
197
198
} ) ;
198
199
expect ( scratch . innerHTML ) . to . equal ( "<p>1</p><p>1</p>" ) ;
199
- expect ( child1Spy ) . to . not . have . been . called ;
200
- expect ( child2Spy ) . to . have . been . calledOnce ;
201
- expect ( parentSpy ) . to . not . have . been . called ;
200
+ expect ( child1Spy ) . not . toHaveBeenCalled ( ) ;
201
+ expect ( child2Spy ) . toHaveBeenCalledOnce ( ) ;
202
+ expect ( parentSpy ) . not . toHaveBeenCalled ( ) ;
202
203
} ) ;
203
204
204
205
it ( "should not rerender components when signals they use change but they are not mounted" , async ( ) => {
205
- const child1Spy = sinon . spy ( ) ;
206
+ const child1Spy = vi . fn ( ) ;
206
207
const signal1 = signal ( 0 ) ;
207
208
function Child ( ) {
208
209
child1Spy ( ) ;
@@ -231,11 +232,11 @@ describe("useSignals", () => {
231
232
await act ( ( ) => {
232
233
signal1 . value += 1 ;
233
234
} ) ;
234
- expect ( child1Spy ) . to . have . been . calledTwice ;
235
+ expect ( child1Spy ) . toHaveBeenCalledTimes ( 2 ) ;
235
236
} ) ;
236
237
237
238
it ( "should not rerender components that only update signals in event handlers" , async ( ) => {
238
- const buttonSpy = sinon . spy ( ) ;
239
+ const buttonSpy = vi . fn ( ) ;
239
240
function AddOneButton ( { num } : { num : Signal < number > } ) {
240
241
useSignals ( ) ;
241
242
buttonSpy ( ) ;
@@ -250,7 +251,7 @@ describe("useSignals", () => {
250
251
) ;
251
252
}
252
253
253
- const displaySpy = sinon . spy ( ) ;
254
+ const displaySpy = vi . fn ( ) ;
254
255
function DisplayNumber ( { num } : { num : Signal < number > } ) {
255
256
useSignals ( ) ;
256
257
displaySpy ( ) ;
@@ -269,20 +270,20 @@ describe("useSignals", () => {
269
270
270
271
await render ( < App /> ) ;
271
272
expect ( scratch . innerHTML ) . to . equal ( "<button>Add One</button><p>0</p>" ) ;
272
- expect ( buttonSpy ) . to . have . been . calledOnce ;
273
- expect ( displaySpy ) . to . have . been . calledOnce ;
273
+ expect ( buttonSpy ) . toHaveBeenCalledOnce ( ) ;
274
+ expect ( displaySpy ) . toHaveBeenCalledOnce ( ) ;
274
275
275
276
await act ( ( ) => {
276
277
scratch . querySelector ( "button" ) ! . click ( ) ;
277
278
} ) ;
278
279
279
280
expect ( scratch . innerHTML ) . to . equal ( "<button>Add One</button><p>1</p>" ) ;
280
- expect ( buttonSpy ) . to . have . been . calledOnce ;
281
- expect ( displaySpy ) . to . have . been . calledTwice ;
281
+ expect ( buttonSpy ) . toHaveBeenCalledOnce ( ) ;
282
+ expect ( displaySpy ) . toHaveBeenCalledTimes ( 2 ) ;
282
283
} ) ;
283
284
284
285
it ( "should not rerender components that only read signals in event handlers" , async ( ) => {
285
- const buttonSpy = sinon . spy ( ) ;
286
+ const buttonSpy = vi . fn ( ) ;
286
287
function AddOneButton ( { num } : { num : Signal < number > } ) {
287
288
useSignals ( ) ;
288
289
buttonSpy ( ) ;
@@ -297,7 +298,7 @@ describe("useSignals", () => {
297
298
) ;
298
299
}
299
300
300
- const displaySpy = sinon . spy ( ) ;
301
+ const displaySpy = vi . fn ( ) ;
301
302
function DisplayNumber ( { num } : { num : Signal < number > } ) {
302
303
useSignals ( ) ;
303
304
displaySpy ( ) ;
@@ -316,42 +317,42 @@ describe("useSignals", () => {
316
317
}
317
318
318
319
function resetSpies ( ) {
319
- buttonSpy . resetHistory ( ) ;
320
- displaySpy . resetHistory ( ) ;
320
+ buttonSpy . mockClear ( ) ;
321
+ displaySpy . mockClear ( ) ;
321
322
}
322
323
323
324
resetSpies ( ) ;
324
325
await render ( < App /> ) ;
325
326
expect ( scratch . innerHTML ) . to . equal ( "<button>Add One</button><p>0</p>" ) ;
326
- expect ( buttonSpy ) . to . have . been . calledOnce ;
327
- expect ( displaySpy ) . to . have . been . calledOnce ;
327
+ expect ( buttonSpy ) . toHaveBeenCalledOnce ( ) ;
328
+ expect ( displaySpy ) . toHaveBeenCalledOnce ( ) ;
328
329
329
330
resetSpies ( ) ;
330
331
await act ( ( ) => {
331
332
scratch . querySelector ( "button" ) ! . click ( ) ;
332
333
} ) ;
333
334
334
335
expect ( scratch . innerHTML ) . to . equal ( "<button>Add One</button><p>2</p>" ) ;
335
- expect ( buttonSpy ) . to . not . have . been . called ;
336
- expect ( displaySpy ) . to . have . been . calledOnce ;
336
+ expect ( buttonSpy ) . not . toHaveBeenCalled ( ) ;
337
+ expect ( displaySpy ) . toHaveBeenCalledOnce ( ) ;
337
338
338
339
resetSpies ( ) ;
339
340
await act ( ( ) => {
340
341
adder . value += 1 ;
341
342
} ) ;
342
343
343
344
expect ( scratch . innerHTML ) . to . equal ( "<button>Add One</button><p>2</p>" ) ;
344
- expect ( buttonSpy ) . to . not . have . been . called ;
345
- expect ( displaySpy ) . to . not . have . been . called ;
345
+ expect ( buttonSpy ) . not . toHaveBeenCalled ( ) ;
346
+ expect ( displaySpy ) . not . toHaveBeenCalled ( ) ;
346
347
347
348
resetSpies ( ) ;
348
349
await act ( ( ) => {
349
350
scratch . querySelector ( "button" ) ! . click ( ) ;
350
351
} ) ;
351
352
352
353
expect ( scratch . innerHTML ) . to . equal ( "<button>Add One</button><p>5</p>" ) ;
353
- expect ( buttonSpy ) . to . not . have . been . called ;
354
- expect ( displaySpy ) . to . have . been . calledOnce ;
354
+ expect ( buttonSpy ) . not . toHaveBeenCalled ( ) ;
355
+ expect ( displaySpy ) . toHaveBeenCalledOnce ( ) ;
355
356
} ) ;
356
357
357
358
it ( "should properly rerender components that use custom hooks" , async ( ) => {
0 commit comments