Skip to content

Commit 8a46c27

Browse files
committed
test(react/vanilla-utils/atomFamily): replace 'userEvent' with 'fireEvent', 'findByText' with 'getByText', add fake timer, and remove 'waitFor'
1 parent f6140d4 commit 8a46c27

File tree

1 file changed

+61
-63
lines changed

1 file changed

+61
-63
lines changed

tests/react/vanilla-utils/atomFamily.test.tsx

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { StrictMode, Suspense, useState } from 'react'
2-
import { act, render, screen, waitFor } from '@testing-library/react'
3-
import userEventOrig from '@testing-library/user-event'
4-
import { expect, it } from 'vitest'
2+
import { act, fireEvent, render, screen } from '@testing-library/react'
3+
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
54
import { useAtom, useSetAtom } from 'jotai/react'
65
import { atom } from 'jotai/vanilla'
76
import type { SetStateAction, WritableAtom } from 'jotai/vanilla'
87
import { atomFamily } from 'jotai/vanilla/utils'
98

10-
const userEvent = {
11-
click: (element: Element) => act(() => userEventOrig.click(element)),
12-
}
9+
beforeEach(() => {
10+
vi.useFakeTimers()
11+
})
12+
13+
afterEach(() => {
14+
vi.useRealTimers()
15+
})
1316

14-
it('new atomFamily impl', async () => {
17+
it('new atomFamily impl', () => {
1518
const myFamily = atomFamily((param: string) => atom(param))
1619

1720
const Displayer = ({ index }: { index: string }) => {
@@ -21,31 +24,33 @@ it('new atomFamily impl', async () => {
2124

2225
render(
2326
<StrictMode>
24-
<Displayer index={'a'} />
27+
<Displayer index="a" />
2528
</StrictMode>,
2629
)
2730

28-
expect(await screen.findByText('count: a')).toBeInTheDocument()
31+
expect(screen.getByText('count: a')).toBeInTheDocument()
2932
})
3033

31-
it('primitive atomFamily returns same reference for same parameters', async () => {
34+
it('primitive atomFamily returns same reference for same parameters', () => {
3235
const myFamily = atomFamily((num: number) => atom({ num }))
36+
3337
expect(myFamily(0)).toEqual(myFamily(0))
3438
expect(myFamily(0)).not.toEqual(myFamily(1))
3539
expect(myFamily(1)).not.toEqual(myFamily(0))
3640
})
3741

38-
it('read-only derived atomFamily returns same reference for same parameters', async () => {
42+
it('read-only derived atomFamily returns same reference for same parameters', () => {
3943
const arrayAtom = atom([0])
4044
const myFamily = atomFamily((num: number) =>
4145
atom((get) => get(arrayAtom)[num] as number),
4246
)
47+
4348
expect(myFamily(0)).toEqual(myFamily(0))
4449
expect(myFamily(0)).not.toEqual(myFamily(1))
4550
expect(myFamily(1)).not.toEqual(myFamily(0))
4651
})
4752

48-
it('removed atom creates a new reference', async () => {
53+
it('removed atom creates a new reference', () => {
4954
const bigAtom = atom([0])
5055
const myFamily = atomFamily((num: number) =>
5156
atom((get) => get(bigAtom)[num] as number),
@@ -66,7 +71,7 @@ it('removed atom creates a new reference', async () => {
6671
expect(myFamily(0)).toEqual(newReference)
6772
})
6873

69-
it('primitive atomFamily initialized with props', async () => {
74+
it('primitive atomFamily initialized with props', () => {
7075
const myFamily = atomFamily((param: number) => atom(param))
7176

7277
const Displayer = ({ index }: { index: number }) => {
@@ -96,19 +101,19 @@ it('primitive atomFamily initialized with props', async () => {
96101
</StrictMode>,
97102
)
98103

99-
expect(await screen.findByText('count: 1')).toBeInTheDocument()
104+
expect(screen.getByText('count: 1')).toBeInTheDocument()
100105

101-
await userEvent.click(screen.getByText('button'))
102-
expect(await screen.findByText('count: 11')).toBeInTheDocument()
106+
fireEvent.click(screen.getByText('button'))
107+
expect(screen.getByText('count: 11')).toBeInTheDocument()
103108

104-
await userEvent.click(screen.getByText('increment'))
105-
expect(await screen.findByText('count: 2')).toBeInTheDocument()
109+
fireEvent.click(screen.getByText('increment'))
110+
expect(screen.getByText('count: 2')).toBeInTheDocument()
106111

107-
await userEvent.click(screen.getByText('button'))
108-
expect(await screen.findByText('count: 12')).toBeInTheDocument()
112+
fireEvent.click(screen.getByText('button'))
113+
expect(screen.getByText('count: 12')).toBeInTheDocument()
109114
})
110115

111-
it('derived atomFamily functionality as usual', async () => {
116+
it('derived atomFamily functionality as usual', () => {
112117
const arrayAtom = atom([0, 0, 0])
113118

114119
const myFamily = atomFamily((param: number) =>
@@ -173,35 +178,27 @@ it('derived atomFamily functionality as usual', async () => {
173178
</StrictMode>,
174179
)
175180

176-
await waitFor(() => {
177-
expect(screen.getByText('index: 0, count: 0')).toBeInTheDocument()
178-
expect(screen.getByText('index: 1, count: 0')).toBeInTheDocument()
179-
expect(screen.getByText('index: 2, count: 0')).toBeInTheDocument()
180-
})
181+
expect(screen.getByText('index: 0, count: 0')).toBeInTheDocument()
182+
expect(screen.getByText('index: 1, count: 0')).toBeInTheDocument()
183+
expect(screen.getByText('index: 2, count: 0')).toBeInTheDocument()
181184

182-
await userEvent.click(screen.getByText('increment #1'))
183-
await waitFor(() => {
184-
expect(screen.getByText('index: 0, count: 0')).toBeInTheDocument()
185-
expect(screen.getByText('index: 1, count: 1')).toBeInTheDocument()
186-
expect(screen.getByText('index: 2, count: 0')).toBeInTheDocument()
187-
})
185+
fireEvent.click(screen.getByText('increment #1'))
186+
expect(screen.getByText('index: 0, count: 0')).toBeInTheDocument()
187+
expect(screen.getByText('index: 1, count: 1')).toBeInTheDocument()
188+
expect(screen.getByText('index: 2, count: 0')).toBeInTheDocument()
188189

189-
await userEvent.click(screen.getByText('increment #0'))
190-
await waitFor(() => {
191-
expect(screen.getByText('index: 0, count: 1')).toBeInTheDocument()
192-
expect(screen.getByText('index: 1, count: 1')).toBeInTheDocument()
193-
expect(screen.getByText('index: 2, count: 0')).toBeInTheDocument()
194-
})
190+
fireEvent.click(screen.getByText('increment #0'))
191+
expect(screen.getByText('index: 0, count: 1')).toBeInTheDocument()
192+
expect(screen.getByText('index: 1, count: 1')).toBeInTheDocument()
193+
expect(screen.getByText('index: 2, count: 0')).toBeInTheDocument()
195194

196-
await userEvent.click(screen.getByText('increment #2'))
197-
await waitFor(() => {
198-
expect(screen.getByText('index: 0, count: 1')).toBeInTheDocument()
199-
expect(screen.getByText('index: 1, count: 1')).toBeInTheDocument()
200-
expect(screen.getByText('index: 2, count: 1')).toBeInTheDocument()
201-
})
195+
fireEvent.click(screen.getByText('increment #2'))
196+
expect(screen.getByText('index: 0, count: 1')).toBeInTheDocument()
197+
expect(screen.getByText('index: 1, count: 1')).toBeInTheDocument()
198+
expect(screen.getByText('index: 2, count: 1')).toBeInTheDocument()
202199
})
203200

204-
it('custom equality function work', async () => {
201+
it('custom equality function work', () => {
205202
const bigAtom = atom([0])
206203

207204
const badFamily = atomFamily((num: { index: number }) =>
@@ -223,10 +220,9 @@ it('custom equality function work', async () => {
223220

224221
it('a derived atom from an async atomFamily (#351)', async () => {
225222
const countAtom = atom(1)
226-
const resolve: (() => void)[] = []
227223
const getAsyncAtom = atomFamily((n: number) =>
228224
atom(async () => {
229-
await new Promise<void>((r) => resolve.push(r))
225+
await new Promise<void>((resolve) => setTimeout(resolve, 100))
230226
return n + 10
231227
}),
232228
)
@@ -243,29 +239,31 @@ it('a derived atom from an async atomFamily (#351)', async () => {
243239
)
244240
}
245241

246-
await act(async () => {
242+
await act(() =>
247243
render(
248244
<StrictMode>
249245
<Suspense fallback="loading">
250246
<Counter />
251247
</Suspense>
252248
</StrictMode>,
253-
)
254-
})
255-
256-
expect(await screen.findByText('loading')).toBeInTheDocument()
257-
resolve.splice(0).forEach((fn) => fn())
258-
expect(await screen.findByText('derived: 11')).toBeInTheDocument()
259-
260-
await userEvent.click(screen.getByText('button'))
261-
expect(await screen.findByText('loading')).toBeInTheDocument()
262-
resolve.splice(0).forEach((fn) => fn())
263-
expect(await screen.findByText('derived: 12')).toBeInTheDocument()
249+
),
250+
)
264251

265-
await userEvent.click(screen.getByText('button'))
266-
expect(await screen.findByText('loading')).toBeInTheDocument()
267-
resolve.splice(0).forEach((fn) => fn())
268-
expect(await screen.findByText('derived: 13')).toBeInTheDocument()
252+
expect(screen.getByText('loading')).toBeInTheDocument()
253+
await act(() => vi.advanceTimersByTimeAsync(100))
254+
expect(screen.getByText('derived: 11')).toBeInTheDocument()
255+
256+
await act(() => fireEvent.click(screen.getByText('button')))
257+
await act(() => vi.advanceTimersByTimeAsync(0))
258+
expect(screen.getByText('loading')).toBeInTheDocument()
259+
await act(() => vi.advanceTimersByTimeAsync(100))
260+
expect(screen.getByText('derived: 12')).toBeInTheDocument()
261+
262+
await act(() => fireEvent.click(screen.getByText('button')))
263+
await act(() => vi.advanceTimersByTimeAsync(0))
264+
expect(screen.getByText('loading')).toBeInTheDocument()
265+
await act(() => vi.advanceTimersByTimeAsync(100))
266+
expect(screen.getByText('derived: 13')).toBeInTheDocument()
269267
})
270268

271269
it('setShouldRemove with custom equality function', async () => {

0 commit comments

Comments
 (0)