Skip to content

Commit e848a55

Browse files
authored
Merge pull request #4686 from isqua/act
Update @testing-library/react from 13.3.0 to 16.0.1; Fixes #4685
2 parents 3a6ec28 + 23a1c3f commit e848a55

File tree

11 files changed

+133
-76
lines changed

11 files changed

+133
-76
lines changed

examples/query/react/basic/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"react-scripts": "5.0.1"
1414
},
1515
"devDependencies": {
16-
"@testing-library/react": "^13.3.0",
16+
"@testing-library/dom": "^10.4.0",
17+
"@testing-library/react": "^16.0.1",
1718
"@types/jest": "^26.0.23",
1819
"@types/react": "^18.0.5",
1920
"@types/react-dom": "^18.0.5",

examples/query/react/kitchen-sink/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
"react-scripts": "5.0.1"
1616
},
1717
"devDependencies": {
18+
"@testing-library/dom": "^10.4.0",
1819
"@testing-library/jest-dom": "^5.11.5",
19-
"@testing-library/react": "^13.3.0",
20+
"@testing-library/react": "^16.0.1",
2021
"@types/jest": "^26.0.23",
2122
"@types/node": "^14.14.6",
2223
"@types/react": "^18.0.5",

packages/toolkit/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@
5656
"@phryneas/ts-version": "^1.0.2",
5757
"@size-limit/file": "^11.0.1",
5858
"@size-limit/webpack": "^11.0.1",
59-
"@testing-library/react": "^13.3.0",
60-
"@testing-library/user-event": "^13.1.5",
59+
"@testing-library/dom": "^10.4.0",
60+
"@testing-library/react": "^16.0.1",
61+
"@testing-library/user-event": "^14.5.2",
6162
"@types/babel__core": "^7.20.5",
6263
"@types/babel__helper-module-imports": "^7.18.3",
6364
"@types/json-stringify-safe": "^5.0.0",

packages/toolkit/src/query/tests/apiProvider.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const api = createApi({
3232
}),
3333
})
3434

35+
afterEach(() => {
36+
vi.resetAllMocks()
37+
})
38+
3539
describe('ApiProvider', () => {
3640
test('ApiProvider allows a user to make queries without a traditional Redux setup', async () => {
3741
function User() {
@@ -72,6 +76,8 @@ describe('ApiProvider', () => {
7276
expect(getByTestId('isFetching').textContent).toBe('false')
7377
})
7478
test('ApiProvider throws if nested inside a Redux context', () => {
79+
// Intentionally swallow the "unhandled error" message
80+
vi.spyOn(console, 'error').mockImplementation(() => {})
7581
expect(() =>
7682
render(
7783
<Provider store={configureStore({ reducer: () => null })}>

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
actionsReducer,
77
setupApiStore,
88
useRenderCounter,
9+
waitForFakeTimer,
910
waitMs,
1011
withProvider,
1112
} from '@internal/tests/utils/helpers'
@@ -47,7 +48,7 @@ interface Item {
4748

4849
const api = createApi({
4950
baseQuery: async (arg: any) => {
50-
await waitMs(150)
51+
await waitForFakeTimer(150)
5152
if (arg?.body && 'amount' in arg.body) {
5253
amount += 1
5354
}
@@ -465,7 +466,6 @@ describe('hooks tests', () => {
465466
<div>
466467
<button
467468
onClick={() => {
468-
console.log('Refetching')
469469
refetch()
470470
}}
471471
>
@@ -915,7 +915,7 @@ describe('hooks tests', () => {
915915
resPromise = refetch()
916916
})
917917
expect(resPromise).toBeInstanceOf(Promise)
918-
const res = await resPromise
918+
const res = await act(() => resPromise)
919919
expect(res.data!.amount).toBeGreaterThan(originalAmount)
920920
})
921921

@@ -1095,15 +1095,15 @@ describe('hooks tests', () => {
10951095
// Allow at least three state effects to hit.
10961096
// Trying to see if any [true, false, true] occurs.
10971097
await act(async () => {
1098-
await waitMs(1)
1098+
await waitForFakeTimer(150)
10991099
})
11001100

11011101
await act(async () => {
1102-
await waitMs(1)
1102+
await waitForFakeTimer(150)
11031103
})
11041104

11051105
await act(async () => {
1106-
await waitMs(1)
1106+
await waitForFakeTimer(150)
11071107
})
11081108

11091109
// Find if at any time the isLoading state has reverted
@@ -1864,7 +1864,8 @@ describe('hooks tests', () => {
18641864
expect(screen.getByTestId('isFetching').textContent).toBe('false'),
18651865
)
18661866

1867-
userEvent.hover(screen.getByTestId('highPriority'))
1867+
await userEvent.hover(screen.getByTestId('highPriority'))
1868+
18681869
expect(
18691870
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
18701871
).toEqual({
@@ -2001,7 +2002,7 @@ describe('hooks tests', () => {
20012002
await waitMs(400)
20022003

20032004
// This should run the query being that we're past the threshold
2004-
userEvent.hover(screen.getByTestId('lowPriority'))
2005+
await userEvent.hover(screen.getByTestId('lowPriority'))
20052006
expect(
20062007
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
20072008
).toEqual({
@@ -2101,7 +2102,7 @@ describe('hooks tests', () => {
21012102

21022103
render(<User />, { wrapper: storeRef.wrapper })
21032104

2104-
userEvent.hover(screen.getByTestId('lowPriority'))
2105+
await userEvent.hover(screen.getByTestId('lowPriority'))
21052106

21062107
expect(
21072108
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
@@ -2993,6 +2994,11 @@ describe('skip behavior', () => {
29932994
await act(async () => {
29942995
rerender([1])
29952996
})
2997+
2998+
await act(async () => {
2999+
await waitForFakeTimer(150)
3000+
})
3001+
29963002
expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
29973003
await waitMs(1)
29983004
expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -3030,6 +3036,11 @@ describe('skip behavior', () => {
30303036
await act(async () => {
30313037
rerender([1])
30323038
})
3039+
3040+
await act(async () => {
3041+
await waitForFakeTimer(150)
3042+
})
3043+
30333044
expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
30343045
await waitMs(1)
30353046
expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -3059,7 +3070,7 @@ describe('skip behavior', () => {
30593070
)
30603071

30613072
await act(async () => {
3062-
await waitMs(1)
3073+
await waitForFakeTimer(150)
30633074
})
30643075

30653076
// Normal fulfilled result, with both `data` and `currentData`

packages/toolkit/src/query/tests/cleanup.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function UsingAB() {
4343
}
4444

4545
beforeAll(() => {
46-
vi.useFakeTimers()
46+
vi.useFakeTimers({ shouldAdvanceTime: true })
4747
})
4848

4949
test('data stays in store when component stays rendered', async () => {

packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ describe('fetchBaseQuery', () => {
862862
prepare.apply(undefined, arguments as unknown as any[])
863863
},
864864
})
865-
baseQuery('http://example.com', commonBaseQueryApi, {
865+
baseQuery('https://example.com', commonBaseQueryApi, {
866866
foo: 'baz',
867867
bar: 5,
868868
})

packages/toolkit/src/query/tests/mocks/handlers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export const posts: Record<string, Post> = {
1212
}
1313

1414
export const handlers = [
15+
http.get(
16+
'https://example.com',
17+
async ({ request, params, cookies, requestId }) => {
18+
HttpResponse.json({ value: 'success' })
19+
},
20+
),
1521
http.get(
1622
'https://example.com/echo',
1723
async ({ request, params, cookies, requestId }) => {

packages/toolkit/src/query/tests/optimisticUpserts.test.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -399,27 +399,34 @@ describe('upsertQueryEntries', () => {
399399
storeRef.store.dispatch(entriesAction)
400400

401401
// Tricky timing. The cache data promises will be resolved
402-
// in microtasks, so we just need any async delay here.
403-
await delay(1)
402+
// in microtasks. We need to wait for them. Best to do this
403+
// in a loop just to avoid a hardcoded delay, but also this
404+
// needs to complete before `keepUnusedDataFor` expires them.
405+
await waitFor(
406+
() => {
407+
const state = storeRef.store.getState()
408+
409+
// onCacheEntryAdded should have run for each post,
410+
// including cache data being resolved
411+
for (const post of posts) {
412+
const matchingSideEffectAction = state.actions.find(
413+
(action) =>
414+
postAddedAction.match(action) && action.payload === post.id,
415+
)
416+
expect(matchingSideEffectAction).toBeTruthy()
417+
}
404418

405-
const state = storeRef.store.getState()
419+
const selectedData =
420+
api.endpoints.postWithSideEffect.select('1')(state).data
406421

407-
// onCacheEntryAdded should have run for each post,
408-
// including cache data being resolved
409-
for (const post of posts) {
410-
const matchingSideEffectAction = state.actions.find(
411-
(action) => postAddedAction.match(action) && action.payload === post.id,
412-
)
413-
expect(matchingSideEffectAction).toBeTruthy()
414-
}
415-
416-
expect(api.endpoints.postWithSideEffect.select('1')(state).data).toBe(
417-
posts[0],
422+
expect(selectedData).toBe(posts[0])
423+
},
424+
{ timeout: 50, interval: 5 },
418425
)
419426

420427
// The cache data should be removed after the keepUnusedDataFor time,
421428
// so wait longer than that
422-
await delay(20)
429+
await delay(100)
423430

424431
const stateAfter = storeRef.store.getState()
425432

packages/toolkit/src/query/tests/refetchingBehaviors.test.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ describe('refetchOnFocus tests', () => {
7171
expect(screen.getByTestId('amount').textContent).toBe('1'),
7272
)
7373

74+
fireEvent.focus(window)
75+
7476
await act(async () => {
75-
fireEvent.focus(window)
77+
await delay(150)
7678
})
7779

78-
await delay(150)
79-
8080
await waitFor(() =>
8181
expect(screen.getByTestId('amount').textContent).toBe('2'),
8282
)
@@ -111,9 +111,7 @@ describe('refetchOnFocus tests', () => {
111111
expect(screen.getByTestId('amount').textContent).toBe('1'),
112112
)
113113

114-
act(() => {
115-
fireEvent.focus(window)
116-
})
114+
fireEvent.focus(window)
117115

118116
await delay(150)
119117

@@ -165,9 +163,7 @@ describe('refetchOnFocus tests', () => {
165163
expect(screen.getByTestId('amount').textContent).toBe('1'),
166164
)
167165

168-
act(() => {
169-
fireEvent.focus(window)
170-
})
166+
fireEvent.focus(window)
171167
expect(screen.getByTestId('isLoading').textContent).toBe('false')
172168
await waitFor(() =>
173169
expect(screen.getByTestId('isFetching').textContent).toBe('true'),
@@ -213,9 +209,7 @@ describe('refetchOnFocus tests', () => {
213209

214210
expect(getIncrementedAmountState()).not.toBeUndefined()
215211

216-
await act(async () => {
217-
fireEvent.focus(window)
218-
})
212+
fireEvent.focus(window)
219213

220214
await delay(1)
221215
expect(getIncrementedAmountState()).toBeUndefined()

0 commit comments

Comments
 (0)