Skip to content

Commit 1ccf21a

Browse files
authored
test(basic): switch to fake timers, replace 'waitFor' with 'vi.waitFor', and 'findByText' with 'getByText' (#1117)
1 parent d801bde commit 1ccf21a

1 file changed

Lines changed: 98 additions & 38 deletions

File tree

tests/basic.test.tsx

Lines changed: 98 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { StrictMode, useEffect, useRef, useState } from 'react'
2-
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
3-
import { expect, it, vi } from 'vitest'
2+
import { fireEvent, render, screen } from '@testing-library/react'
3+
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
44
import { proxy, snapshot, useSnapshot } from 'valtio'
55

6+
beforeEach(() => {
7+
vi.useFakeTimers()
8+
})
9+
10+
afterEach(() => {
11+
vi.useRealTimers()
12+
})
13+
614
const useCommitCount = () => {
715
const commitCountRef = useRef(1)
816
useEffect(() => {
@@ -30,10 +38,14 @@ it('simple counter', async () => {
3038
</StrictMode>,
3139
)
3240

33-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
41+
await vi.waitFor(async () =>
42+
expect(screen.getByText('count: 0')).toBeInTheDocument(),
43+
)
3444

3545
fireEvent.click(screen.getByText('button'))
36-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
46+
await vi.waitFor(async () =>
47+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
48+
)
3749
unmount()
3850
})
3951

@@ -71,19 +83,19 @@ it('no extra re-renders (commits)', async () => {
7183
</>,
7284
)
7385

74-
await waitFor(() => {
86+
await vi.waitFor(() => {
7587
expect(screen.getByText('count: 0 (1)')).toBeInTheDocument()
7688
expect(screen.getByText('count2: 0 (1)')).toBeInTheDocument()
7789
})
7890

7991
fireEvent.click(screen.getByText('button'))
80-
await waitFor(() => {
92+
await vi.waitFor(() => {
8193
expect(screen.getByText('count: 1 (2)')).toBeInTheDocument()
8294
expect(screen.getByText('count2: 0 (1)')).toBeInTheDocument()
8395
})
8496

8597
fireEvent.click(screen.getByText('button2'))
86-
await waitFor(() => {
98+
await vi.waitFor(() => {
8799
expect(screen.getByText('count: 1 (2)')).toBeInTheDocument()
88100
expect(screen.getByText('count2: 1 (2)')).toBeInTheDocument()
89101
})
@@ -123,7 +135,7 @@ it('no extra re-renders (render func calls in non strict mode)', async () => {
123135
</>,
124136
)
125137

126-
await waitFor(() => {
138+
await vi.waitFor(() => {
127139
expect(screen.getByText('count: 0')).toBeInTheDocument()
128140
expect(screen.getByText('count2: 0')).toBeInTheDocument()
129141
})
@@ -133,7 +145,7 @@ it('no extra re-renders (render func calls in non strict mode)', async () => {
133145
expect(renderFn2).lastCalledWith(0)
134146

135147
fireEvent.click(screen.getByText('button'))
136-
await waitFor(() => {
148+
await vi.waitFor(() => {
137149
expect(screen.getByText('count: 1')).toBeInTheDocument()
138150
expect(screen.getByText('count2: 0')).toBeInTheDocument()
139151
})
@@ -143,7 +155,7 @@ it('no extra re-renders (render func calls in non strict mode)', async () => {
143155
expect(renderFn2).lastCalledWith(0)
144156

145157
fireEvent.click(screen.getByText('button2'))
146-
await waitFor(() => {
158+
await vi.waitFor(() => {
147159
expect(screen.getByText('count: 1')).toBeInTheDocument()
148160
expect(screen.getByText('count2: 1')).toBeInTheDocument()
149161
})
@@ -153,7 +165,7 @@ it('no extra re-renders (render func calls in non strict mode)', async () => {
153165
expect(renderFn2).lastCalledWith(1)
154166

155167
fireEvent.click(screen.getByText('button2'))
156-
await waitFor(() => {
168+
await vi.waitFor(() => {
157169
expect(screen.getByText('count: 1')).toBeInTheDocument()
158170
expect(screen.getByText('count2: 2')).toBeInTheDocument()
159171
})
@@ -163,7 +175,7 @@ it('no extra re-renders (render func calls in non strict mode)', async () => {
163175
expect(renderFn2).lastCalledWith(2)
164176

165177
fireEvent.click(screen.getByText('button'))
166-
await waitFor(() => {
178+
await vi.waitFor(() => {
167179
expect(screen.getByText('count: 2')).toBeInTheDocument()
168180
expect(screen.getByText('count2: 2')).toBeInTheDocument()
169181
})
@@ -192,10 +204,14 @@ it('object in object', async () => {
192204
</StrictMode>,
193205
)
194206

195-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
207+
await vi.waitFor(() =>
208+
expect(screen.getByText('count: 0')).toBeInTheDocument(),
209+
)
196210

197211
fireEvent.click(screen.getByText('button'))
198-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
212+
await vi.waitFor(() =>
213+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
214+
)
199215
})
200216

201217
it('array in object', async () => {
@@ -219,10 +235,14 @@ it('array in object', async () => {
219235
</StrictMode>,
220236
)
221237

222-
expect(await screen.findByText('counts: 0,1,2')).toBeInTheDocument()
238+
await vi.waitFor(() =>
239+
expect(screen.getByText('counts: 0,1,2')).toBeInTheDocument(),
240+
)
223241

224242
fireEvent.click(screen.getByText('button'))
225-
expect(await screen.findByText('counts: 0,1,2,3')).toBeInTheDocument()
243+
await vi.waitFor(() =>
244+
expect(screen.getByText('counts: 0,1,2,3')).toBeInTheDocument(),
245+
)
226246
})
227247

228248
it('array pop and splice', async () => {
@@ -245,13 +265,19 @@ it('array pop and splice', async () => {
245265
</StrictMode>,
246266
)
247267

248-
expect(await screen.findByText('counts: 0,1,2')).toBeInTheDocument()
268+
await vi.waitFor(() =>
269+
expect(screen.getByText('counts: 0,1,2')).toBeInTheDocument(),
270+
)
249271

250272
fireEvent.click(screen.getByText('button'))
251-
expect(await screen.findByText('counts: 0,1')).toBeInTheDocument()
273+
await vi.waitFor(() =>
274+
expect(screen.getByText('counts: 0,1')).toBeInTheDocument(),
275+
)
252276

253277
fireEvent.click(screen.getByText('button2'))
254-
expect(await screen.findByText('counts: 0,10,11,1')).toBeInTheDocument()
278+
await vi.waitFor(() =>
279+
expect(screen.getByText('counts: 0,10,11,1')).toBeInTheDocument(),
280+
)
255281
})
256282

257283
it('array length after direct assignment', async () => {
@@ -285,13 +311,19 @@ it('array length after direct assignment', async () => {
285311
</StrictMode>,
286312
)
287313

288-
expect(await screen.findByText('counts: 0,1,2')).toBeInTheDocument()
314+
await vi.waitFor(() =>
315+
expect(screen.getByText('counts: 0,1,2')).toBeInTheDocument(),
316+
)
289317

290318
fireEvent.click(screen.getByText('increment'))
291-
expect(await screen.findByText('counts: 0,1,2,3')).toBeInTheDocument()
319+
await vi.waitFor(() =>
320+
expect(screen.getByText('counts: 0,1,2,3')).toBeInTheDocument(),
321+
)
292322

293323
fireEvent.click(screen.getByText('jump'))
294-
expect(await screen.findByText('counts: 0,1,2,3,,,,,,9')).toBeInTheDocument()
324+
await vi.waitFor(() =>
325+
expect(screen.getByText('counts: 0,1,2,3,,,,,,9')).toBeInTheDocument(),
326+
)
295327
})
296328

297329
it('deleting property', async () => {
@@ -313,10 +345,14 @@ it('deleting property', async () => {
313345
</StrictMode>,
314346
)
315347

316-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
348+
await vi.waitFor(() =>
349+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
350+
)
317351

318352
fireEvent.click(screen.getByText('button'))
319-
expect(await screen.findByText('count: none')).toBeInTheDocument()
353+
await vi.waitFor(() =>
354+
expect(screen.getByText('count: none')).toBeInTheDocument(),
355+
)
320356
})
321357

322358
it('circular object', async () => {
@@ -340,10 +376,14 @@ it('circular object', async () => {
340376
</StrictMode>,
341377
)
342378

343-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
379+
await vi.waitFor(() => {
380+
expect(screen.getByText('count: 0')).toBeInTheDocument()
381+
})
344382

345383
fireEvent.click(screen.getByText('button'))
346-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
384+
await vi.waitFor(() => {
385+
expect(screen.getByText('count: 1')).toBeInTheDocument()
386+
})
347387
})
348388

349389
it('circular object with non-proxy object (#375)', async () => {
@@ -362,7 +402,9 @@ it('circular object with non-proxy object (#375)', async () => {
362402
</StrictMode>,
363403
)
364404

365-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
405+
await vi.waitFor(() =>
406+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
407+
)
366408
})
367409

368410
it('render from outside', async () => {
@@ -390,11 +432,15 @@ it('render from outside', async () => {
390432
</StrictMode>,
391433
)
392434

393-
expect(await screen.findByText('anotherCount: 0')).toBeInTheDocument()
435+
await vi.waitFor(() =>
436+
expect(screen.getByText('anotherCount: 0')).toBeInTheDocument(),
437+
)
394438

395439
fireEvent.click(screen.getByText('button'))
396440
fireEvent.click(screen.getByText('toggle'))
397-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
441+
await vi.waitFor(() =>
442+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
443+
)
398444
})
399445

400446
it('counter with sync option', async () => {
@@ -418,13 +464,19 @@ it('counter with sync option', async () => {
418464
</>,
419465
)
420466

421-
expect(await screen.findByText('count: 0 (1)')).toBeInTheDocument()
467+
await vi.waitFor(() =>
468+
expect(screen.getByText('count: 0 (1)')).toBeInTheDocument(),
469+
)
422470

423471
fireEvent.click(screen.getByText('button'))
424-
expect(await screen.findByText('count: 1 (2)')).toBeInTheDocument()
472+
await vi.waitFor(() =>
473+
expect(screen.getByText('count: 1 (2)')).toBeInTheDocument(),
474+
)
425475

426476
fireEvent.click(screen.getByText('button'))
427-
expect(await screen.findByText('count: 2 (3)')).toBeInTheDocument()
477+
await vi.waitFor(() =>
478+
expect(screen.getByText('count: 2 (3)')).toBeInTheDocument(),
479+
)
428480
})
429481

430482
it('support undefined property (#439)', async () => {
@@ -443,7 +495,9 @@ it('support undefined property (#439)', async () => {
443495
</StrictMode>,
444496
)
445497

446-
expect(await screen.findByText('has prop: true')).toBeInTheDocument()
498+
await vi.waitFor(() =>
499+
expect(screen.getByText('has prop: true')).toBeInTheDocument(),
500+
)
447501
})
448502

449503
it('sync snapshot between nested components (#460)', async () => {
@@ -480,13 +534,13 @@ it('sync snapshot between nested components (#460)', async () => {
480534
</StrictMode>,
481535
)
482536

483-
await waitFor(() => {
537+
await vi.waitFor(() => {
484538
expect(screen.getByText('Parent: value1')).toBeInTheDocument()
485539
expect(screen.getByText('Child: value1')).toBeInTheDocument()
486540
})
487541

488542
fireEvent.click(screen.getByText('button'))
489-
await waitFor(() => {
543+
await vi.waitFor(() => {
490544
expect(screen.getByText('Parent: value2')).toBeInTheDocument()
491545
expect(screen.getByText('Child: value2')).toBeInTheDocument()
492546
})
@@ -517,14 +571,20 @@ it('stable snapshot object (#985)', async () => {
517571

518572
render(<TestComponent />)
519573

520-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
574+
await vi.waitFor(() =>
575+
expect(screen.getByText('count: 0')).toBeInTheDocument(),
576+
)
521577
expect(effectCount).toBe(1)
522578

523579
fireEvent.click(screen.getByText('button'))
524-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
580+
await vi.waitFor(() =>
581+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
582+
)
525583
expect(effectCount).toBe(1)
526584

527585
fireEvent.click(screen.getByText('button'))
528-
expect(await screen.findByText('count: 2')).toBeInTheDocument()
586+
await vi.waitFor(() =>
587+
expect(screen.getByText('count: 2')).toBeInTheDocument(),
588+
)
529589
expect(effectCount).toBe(1)
530590
})

0 commit comments

Comments
 (0)