Skip to content

Commit 9f4a7c4

Browse files
committed
Fix tests after upgrade @testing-library/react
1 parent bed2d60 commit 9f4a7c4

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

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

Lines changed: 21 additions & 9 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'
@@ -46,7 +47,7 @@ interface Item {
4647

4748
const api = createApi({
4849
baseQuery: async (arg: any) => {
49-
await waitMs(150)
50+
await waitForFakeTimer(150)
5051
if (arg?.body && 'amount' in arg.body) {
5152
amount += 1
5253
}
@@ -774,7 +775,7 @@ describe('hooks tests', () => {
774775
resPromise = refetch()
775776
})
776777
expect(resPromise).toBeInstanceOf(Promise)
777-
const res = await resPromise
778+
const res = await act(() => resPromise)
778779
expect(res.data!.amount).toBeGreaterThan(originalAmount)
779780
})
780781

@@ -954,15 +955,15 @@ describe('hooks tests', () => {
954955
// Allow at least three state effects to hit.
955956
// Trying to see if any [true, false, true] occurs.
956957
await act(async () => {
957-
await waitMs(1)
958+
await waitForFakeTimer(150)
958959
})
959960

960961
await act(async () => {
961-
await waitMs(1)
962+
await waitForFakeTimer(150)
962963
})
963964

964965
await act(async () => {
965-
await waitMs(1)
966+
await waitForFakeTimer(150)
966967
})
967968

968969
// Find if at any time the isLoading state has reverted
@@ -1723,7 +1724,8 @@ describe('hooks tests', () => {
17231724
expect(screen.getByTestId('isFetching').textContent).toBe('false'),
17241725
)
17251726

1726-
userEvent.hover(screen.getByTestId('highPriority'))
1727+
await userEvent.hover(screen.getByTestId('highPriority'))
1728+
17271729
expect(
17281730
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
17291731
).toEqual({
@@ -1860,7 +1862,7 @@ describe('hooks tests', () => {
18601862
await waitMs(400)
18611863

18621864
// This should run the query being that we're past the threshold
1863-
userEvent.hover(screen.getByTestId('lowPriority'))
1865+
await userEvent.hover(screen.getByTestId('lowPriority'))
18641866
expect(
18651867
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
18661868
).toEqual({
@@ -1960,7 +1962,7 @@ describe('hooks tests', () => {
19601962

19611963
render(<User />, { wrapper: storeRef.wrapper })
19621964

1963-
userEvent.hover(screen.getByTestId('lowPriority'))
1965+
await userEvent.hover(screen.getByTestId('lowPriority'))
19641966

19651967
expect(
19661968
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
@@ -2852,6 +2854,11 @@ describe('skip behavior', () => {
28522854
await act(async () => {
28532855
rerender([1])
28542856
})
2857+
2858+
await act(async () => {
2859+
await waitForFakeTimer(150)
2860+
})
2861+
28552862
expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
28562863
await waitMs(1)
28572864
expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -2888,6 +2895,11 @@ describe('skip behavior', () => {
28882895
await act(async () => {
28892896
rerender([1])
28902897
})
2898+
2899+
await act(async () => {
2900+
await waitForFakeTimer(150)
2901+
})
2902+
28912903
expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
28922904
await waitMs(1)
28932905
expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -2916,7 +2928,7 @@ describe('skip behavior', () => {
29162928
)
29172929

29182930
await act(async () => {
2919-
await waitMs(1)
2931+
await waitForFakeTimer(150)
29202932
})
29212933

29222934
// 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/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)