Skip to content

Commit 4c297c0

Browse files
committed
test(react/optimization): replace 'userEvent' with 'fireEvent', 'findByText' with 'getByText'
1 parent c93671a commit 4c297c0

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

tests/react/optimization.test.tsx

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { useEffect } from 'react'
2-
import { render, screen, waitFor } from '@testing-library/react'
3-
import userEvent from '@testing-library/user-event'
2+
import { fireEvent, render, screen } from '@testing-library/react'
43
import { expect, it } from 'vitest'
54
import { useAtom } from 'jotai/react'
65
import { atom } from 'jotai/vanilla'
76

8-
it('only relevant render function called (#156)', async () => {
7+
it('only relevant render function called (#156)', () => {
98
const count1Atom = atom(0)
109
const count2Atom = atom(0)
1110

@@ -47,24 +46,22 @@ it('only relevant render function called (#156)', async () => {
4746
const viewCount1AfterMount = viewCount1
4847
const viewCount2AfterMount = viewCount2
4948

50-
await userEvent.click(screen.getByText('button1'))
51-
49+
fireEvent.click(screen.getByText('button1'))
5250
expect(screen.getByText('count1: 1')).toBeInTheDocument()
5351
expect(screen.getByText('count2: 0')).toBeInTheDocument()
5452

5553
expect(viewCount1).toBe(viewCount1AfterMount + 1)
5654
expect(viewCount2).toBe(viewCount2AfterMount + 0)
5755

58-
await userEvent.click(screen.getByText('button2'))
59-
56+
fireEvent.click(screen.getByText('button2'))
6057
expect(screen.getByText('count1: 1')).toBeInTheDocument()
6158
expect(screen.getByText('count2: 1')).toBeInTheDocument()
6259

6360
expect(viewCount1).toBe(viewCount1AfterMount + 1)
6461
expect(viewCount2).toBe(viewCount2AfterMount + 1)
6562
})
6663

67-
it('only render once using atoms with write-only atom', async () => {
64+
it('only render once using atoms with write-only atom', () => {
6865
const count1Atom = atom(0)
6966
const count2Atom = atom(0)
7067
const incrementAtom = atom(null, (_get, set, _arg) => {
@@ -97,19 +94,19 @@ it('only render once using atoms with write-only atom', async () => {
9794
</>,
9895
)
9996

100-
expect(await screen.findByText('count1: 0, count2: 0')).toBeInTheDocument()
97+
expect(screen.getByText('count1: 0, count2: 0')).toBeInTheDocument()
10198
const viewCountAfterMount = viewCount
10299

103-
await userEvent.click(screen.getByText('button'))
104-
expect(await screen.findByText('count1: 1, count2: 1')).toBeInTheDocument()
100+
fireEvent.click(screen.getByText('button'))
101+
expect(screen.getByText('count1: 1, count2: 1')).toBeInTheDocument()
105102
expect(viewCount).toBe(viewCountAfterMount + 1)
106103

107-
await userEvent.click(screen.getByText('button'))
108-
expect(await screen.findByText('count1: 2, count2: 2')).toBeInTheDocument()
104+
fireEvent.click(screen.getByText('button'))
105+
expect(screen.getByText('count1: 2, count2: 2')).toBeInTheDocument()
109106
expect(viewCount).toBe(viewCountAfterMount + 2)
110107
})
111108

112-
it('useless re-renders with static atoms (#355)', async () => {
109+
it('useless re-renders with static atoms (#355)', () => {
113110
// check out https://codesandbox.io/s/m82r5 to see the expected re-renders
114111
const countAtom = atom(0)
115112
const unrelatedAtom = atom(0)
@@ -135,19 +132,19 @@ it('useless re-renders with static atoms (#355)', async () => {
135132
</>,
136133
)
137134

138-
await screen.findByText('count: 0')
135+
expect(screen.getByText('count: 0')).toBeInTheDocument()
139136
const viewCountAfterMount = viewCount
140137

141-
await userEvent.click(screen.getByText('button'))
142-
await screen.findByText('count: 1')
138+
fireEvent.click(screen.getByText('button'))
139+
expect(screen.getByText('count: 1')).toBeInTheDocument()
143140
expect(viewCount).toBe(viewCountAfterMount + 1)
144141

145-
await userEvent.click(screen.getByText('button'))
146-
await screen.findByText('count: 2')
142+
fireEvent.click(screen.getByText('button'))
143+
expect(screen.getByText('count: 2')).toBeInTheDocument()
147144
expect(viewCount).toBe(viewCountAfterMount + 2)
148145
})
149146

150-
it('does not re-render if value is the same (#1158)', async () => {
147+
it('does not re-render if value is the same (#1158)', () => {
151148
const countAtom = atom(0)
152149

153150
let viewCount = 0
@@ -170,27 +167,27 @@ it('does not re-render if value is the same (#1158)', async () => {
170167
</>,
171168
)
172169

173-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
170+
expect(screen.getByText('count: 0')).toBeInTheDocument()
174171
const viewCountAfterMount = viewCount
175172

176-
await userEvent.click(screen.getByText('noop'))
177-
expect(await screen.findByText('count: 0')).toBeInTheDocument()
173+
fireEvent.click(screen.getByText('noop'))
174+
expect(screen.getByText('count: 0')).toBeInTheDocument()
178175
expect(viewCount).toBe(viewCountAfterMount + 0)
179176

180-
await userEvent.click(screen.getByText('inc'))
181-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
177+
fireEvent.click(screen.getByText('inc'))
178+
expect(screen.getByText('count: 1')).toBeInTheDocument()
182179
expect(viewCount).toBe(viewCountAfterMount + 1)
183180

184-
await userEvent.click(screen.getByText('noop'))
185-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
181+
fireEvent.click(screen.getByText('noop'))
182+
expect(screen.getByText('count: 1')).toBeInTheDocument()
186183
expect(viewCount).toBe(viewCountAfterMount + 1)
187184

188-
await userEvent.click(screen.getByText('inc'))
189-
expect(await screen.findByText('count: 2')).toBeInTheDocument()
185+
fireEvent.click(screen.getByText('inc'))
186+
expect(screen.getByText('count: 2')).toBeInTheDocument()
190187
expect(viewCount).toBe(viewCountAfterMount + 2)
191188
})
192189

193-
it('no extra rerenders after commit with derived atoms (#1213)', async () => {
190+
it('no extra rerenders after commit with derived atoms (#1213)', () => {
194191
const baseAtom = atom({ count1: 0, count2: 0 })
195192
const count1Atom = atom((get) => get(baseAtom).count1)
196193
const count2Atom = atom((get) => get(baseAtom).count2)
@@ -243,30 +240,27 @@ it('no extra rerenders after commit with derived atoms (#1213)', async () => {
243240
</>,
244241
)
245242

246-
await waitFor(() => {
247-
expect(screen.getByText('count1: 0')).toBeInTheDocument()
248-
expect(screen.getByText('count2: 0')).toBeInTheDocument()
249-
})
243+
expect(screen.getByText('count1: 0')).toBeInTheDocument()
244+
expect(screen.getByText('count2: 0')).toBeInTheDocument()
245+
250246
expect(viewCount1 > 0).toBeTruthy()
251247
expect(viewCount2 > 0).toBeTruthy()
252248

253-
await userEvent.click(screen.getByText('inc1'))
254-
249+
fireEvent.click(screen.getByText('inc1'))
255250
expect(screen.getByText('count1: 1')).toBeInTheDocument()
256251
expect(screen.getByText('count2: 0')).toBeInTheDocument()
257252

258253
expect(viewCount1).toBe(viewCount1AfterCommit)
259254

260-
await userEvent.click(screen.getByText('inc2'))
261-
255+
fireEvent.click(screen.getByText('inc2'))
262256
expect(screen.getByText('count1: 1')).toBeInTheDocument()
263257
expect(screen.getByText('count2: 1')).toBeInTheDocument()
264258

265259
expect(viewCount2).toBe(viewCount2AfterCommit)
266260

267-
await userEvent.click(screen.getByText('inc1'))
268-
261+
fireEvent.click(screen.getByText('inc1'))
269262
expect(screen.getByText('count1: 2')).toBeInTheDocument()
270263
expect(screen.getByText('count2: 1')).toBeInTheDocument()
264+
271265
expect(viewCount1).toBe(viewCount1AfterCommit)
272266
})

0 commit comments

Comments
 (0)