Skip to content

Commit 044011b

Browse files
authored
test(class): switch to fake timers, replace 'waitFor' with 'vi.waitFor', and 'findByText' with 'getByText' (#1118)
1 parent 1ccf21a commit 044011b

1 file changed

Lines changed: 33 additions & 17 deletions

File tree

tests/class.test.tsx

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { StrictMode, useEffect, useRef } from 'react'
2-
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
3-
import { expect, it } from 'vitest'
2+
import { fireEvent, render, screen } from '@testing-library/react'
3+
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
44
import { proxy, 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(0)
816
useEffect(() => {
@@ -37,10 +45,14 @@ it('simple class without methods', async () => {
3745
</StrictMode>,
3846
)
3947

40-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
48+
await vi.waitFor(() =>
49+
expect(screen.getByText('count: 0')).toBeInTheDocument(),
50+
)
4151

4252
fireEvent.click(screen.getByText('button'))
43-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
53+
await vi.waitFor(() =>
54+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
55+
)
4456
})
4557

4658
it('no extra re-renders with class', async () => {
@@ -86,19 +98,19 @@ it('no extra re-renders with class', async () => {
8698
</>,
8799
)
88100

89-
await waitFor(() => {
101+
await vi.waitFor(() => {
90102
expect(screen.getByText('count: 0 (0)')).toBeInTheDocument()
91103
expect(screen.getByText('count2: 0 (0)')).toBeInTheDocument()
92104
})
93105

94106
fireEvent.click(screen.getByText('button'))
95-
await waitFor(() => {
107+
await vi.waitFor(() => {
96108
expect(screen.getByText('count: 1 (1)')).toBeInTheDocument()
97109
expect(screen.getByText('count2: 0 (0)')).toBeInTheDocument()
98110
})
99111

100112
fireEvent.click(screen.getByText('button2'))
101-
await waitFor(() => {
113+
await vi.waitFor(() => {
102114
expect(screen.getByText('count: 1 (1)')).toBeInTheDocument()
103115
expect(screen.getByText('count2: 1 (1)')).toBeInTheDocument()
104116
})
@@ -137,10 +149,14 @@ it('inherited class without methods', async () => {
137149
</StrictMode>,
138150
)
139151

140-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
152+
await vi.waitFor(() =>
153+
expect(screen.getByText('count: 0')).toBeInTheDocument(),
154+
)
141155

142156
fireEvent.click(screen.getByText('button'))
143-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
157+
await vi.waitFor(() =>
158+
expect(screen.getByText('count: 1')).toBeInTheDocument(),
159+
)
144160
})
145161

146162
it('class with a method', async () => {
@@ -184,13 +200,13 @@ it('class with a method', async () => {
184200
</>,
185201
)
186202

187-
await waitFor(() => {
203+
await vi.waitFor(() => {
188204
expect(screen.getByText('doubled: 0 (0)')).toBeInTheDocument()
189205
expect(screen.getByText('count: 0 (0)')).toBeInTheDocument()
190206
})
191207

192208
fireEvent.click(screen.getByText('button'))
193-
await waitFor(() => {
209+
await vi.waitFor(() => {
194210
expect(screen.getByText('doubled: 2 (1)')).toBeInTheDocument()
195211
expect(screen.getByText('count: 1 (1)')).toBeInTheDocument()
196212
})
@@ -247,19 +263,19 @@ it('inherited class with a method', async () => {
247263
</>,
248264
)
249265

250-
await waitFor(() => {
266+
await vi.waitFor(() => {
251267
expect(screen.getByText('doubled: 0 (0)')).toBeInTheDocument()
252268
expect(screen.getByText('count2: 0 (0)')).toBeInTheDocument()
253269
})
254270

255271
fireEvent.click(screen.getByText('button'))
256-
await waitFor(() => {
272+
await vi.waitFor(() => {
257273
expect(screen.getByText('doubled: 2 (1)')).toBeInTheDocument()
258274
expect(screen.getByText('count2: 0 (0)')).toBeInTheDocument()
259275
})
260276

261277
fireEvent.click(screen.getByText('button2'))
262-
await waitFor(() => {
278+
await vi.waitFor(() => {
263279
expect(screen.getByText('doubled: 2 (1)')).toBeInTheDocument()
264280
expect(screen.getByText('count2: 1 (1)')).toBeInTheDocument()
265281
})
@@ -314,19 +330,19 @@ it('no extra re-renders with getters', async () => {
314330
</>,
315331
)
316332

317-
await waitFor(() => {
333+
await vi.waitFor(() => {
318334
expect(screen.getByText('count: 0 (0)')).toBeInTheDocument()
319335
expect(screen.getByText('sum: 0 (0)')).toBeInTheDocument()
320336
})
321337

322338
fireEvent.click(screen.getByText('button'))
323-
await waitFor(() => {
339+
await vi.waitFor(() => {
324340
expect(screen.getByText('count: 1 (1)')).toBeInTheDocument()
325341
expect(screen.getByText('sum: 1 (1)')).toBeInTheDocument()
326342
})
327343

328344
fireEvent.click(screen.getByText('button2'))
329-
await waitFor(() => {
345+
await vi.waitFor(() => {
330346
expect(screen.getByText('count: 1 (1)')).toBeInTheDocument()
331347
expect(screen.getByText('sum: 2 (2)')).toBeInTheDocument()
332348
})

0 commit comments

Comments
 (0)