Skip to content

Commit 97f96ad

Browse files
isquamarkerikson
authored andcommitted
Fix tests after upgrade @testing-library/react
1 parent a531f50 commit 97f96ad

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

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

+21-9
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
}
@@ -915,7 +916,7 @@ describe('hooks tests', () => {
915916
resPromise = refetch()
916917
})
917918
expect(resPromise).toBeInstanceOf(Promise)
918-
const res = await resPromise
919+
const res = await act(() => resPromise)
919920
expect(res.data!.amount).toBeGreaterThan(originalAmount)
920921
})
921922

@@ -1095,15 +1096,15 @@ describe('hooks tests', () => {
10951096
// Allow at least three state effects to hit.
10961097
// Trying to see if any [true, false, true] occurs.
10971098
await act(async () => {
1098-
await waitMs(1)
1099+
await waitForFakeTimer(150)
10991100
})
11001101

11011102
await act(async () => {
1102-
await waitMs(1)
1103+
await waitForFakeTimer(150)
11031104
})
11041105

11051106
await act(async () => {
1106-
await waitMs(1)
1107+
await waitForFakeTimer(150)
11071108
})
11081109

11091110
// Find if at any time the isLoading state has reverted
@@ -1864,7 +1865,8 @@ describe('hooks tests', () => {
18641865
expect(screen.getByTestId('isFetching').textContent).toBe('false'),
18651866
)
18661867

1867-
userEvent.hover(screen.getByTestId('highPriority'))
1868+
await userEvent.hover(screen.getByTestId('highPriority'))
1869+
18681870
expect(
18691871
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
18701872
).toEqual({
@@ -2001,7 +2003,7 @@ describe('hooks tests', () => {
20012003
await waitMs(400)
20022004

20032005
// This should run the query being that we're past the threshold
2004-
userEvent.hover(screen.getByTestId('lowPriority'))
2006+
await userEvent.hover(screen.getByTestId('lowPriority'))
20052007
expect(
20062008
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
20072009
).toEqual({
@@ -2101,7 +2103,7 @@ describe('hooks tests', () => {
21012103

21022104
render(<User />, { wrapper: storeRef.wrapper })
21032105

2104-
userEvent.hover(screen.getByTestId('lowPriority'))
2106+
await userEvent.hover(screen.getByTestId('lowPriority'))
21052107

21062108
expect(
21072109
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
@@ -2993,6 +2995,11 @@ describe('skip behavior', () => {
29932995
await act(async () => {
29942996
rerender([1])
29952997
})
2998+
2999+
await act(async () => {
3000+
await waitForFakeTimer(150)
3001+
})
3002+
29963003
expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
29973004
await waitMs(1)
29983005
expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -3030,6 +3037,11 @@ describe('skip behavior', () => {
30303037
await act(async () => {
30313038
rerender([1])
30323039
})
3040+
3041+
await act(async () => {
3042+
await waitForFakeTimer(150)
3043+
})
3044+
30333045
expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
30343046
await waitMs(1)
30353047
expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -3059,7 +3071,7 @@ describe('skip behavior', () => {
30593071
)
30603072

30613073
await act(async () => {
3062-
await waitMs(1)
3074+
await waitForFakeTimer(150)
30633075
})
30643076

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

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

+1-1
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/refetchingBehaviors.test.tsx

+6-12
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)