Skip to content

Commit

Permalink
Fix tests after upgrade @testing-library/react
Browse files Browse the repository at this point in the history
  • Loading branch information
isqua authored and markerikson committed Nov 28, 2024
1 parent a531f50 commit 97f96ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
30 changes: 21 additions & 9 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
actionsReducer,
setupApiStore,
useRenderCounter,
waitForFakeTimer,
waitMs,
withProvider,
} from '@internal/tests/utils/helpers'
Expand Down Expand Up @@ -47,7 +48,7 @@ interface Item {

const api = createApi({
baseQuery: async (arg: any) => {
await waitMs(150)
await waitForFakeTimer(150)
if (arg?.body && 'amount' in arg.body) {
amount += 1
}
Expand Down Expand Up @@ -915,7 +916,7 @@ describe('hooks tests', () => {
resPromise = refetch()
})
expect(resPromise).toBeInstanceOf(Promise)
const res = await resPromise
const res = await act(() => resPromise)
expect(res.data!.amount).toBeGreaterThan(originalAmount)
})

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

await act(async () => {
await waitMs(1)
await waitForFakeTimer(150)
})

await act(async () => {
await waitMs(1)
await waitForFakeTimer(150)
})

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

userEvent.hover(screen.getByTestId('highPriority'))
await userEvent.hover(screen.getByTestId('highPriority'))

expect(
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
).toEqual({
Expand Down Expand Up @@ -2001,7 +2003,7 @@ describe('hooks tests', () => {
await waitMs(400)

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

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

userEvent.hover(screen.getByTestId('lowPriority'))
await userEvent.hover(screen.getByTestId('lowPriority'))

expect(
api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
Expand Down Expand Up @@ -2993,6 +2995,11 @@ describe('skip behavior', () => {
await act(async () => {
rerender([1])
})

await act(async () => {
await waitForFakeTimer(150)
})

expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
await waitMs(1)
expect(getSubscriptionCount('getUser(1)')).toBe(1)
Expand Down Expand Up @@ -3030,6 +3037,11 @@ describe('skip behavior', () => {
await act(async () => {
rerender([1])
})

await act(async () => {
await waitForFakeTimer(150)
})

expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
await waitMs(1)
expect(getSubscriptionCount('getUser(1)')).toBe(1)
Expand Down Expand Up @@ -3059,7 +3071,7 @@ describe('skip behavior', () => {
)

await act(async () => {
await waitMs(1)
await waitForFakeTimer(150)
})

// Normal fulfilled result, with both `data` and `currentData`
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/cleanup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function UsingAB() {
}

beforeAll(() => {
vi.useFakeTimers()
vi.useFakeTimers({ shouldAdvanceTime: true })
})

test('data stays in store when component stays rendered', async () => {
Expand Down
18 changes: 6 additions & 12 deletions packages/toolkit/src/query/tests/refetchingBehaviors.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ describe('refetchOnFocus tests', () => {
expect(screen.getByTestId('amount').textContent).toBe('1'),
)

fireEvent.focus(window)

await act(async () => {
fireEvent.focus(window)
await delay(150)
})

await delay(150)

await waitFor(() =>
expect(screen.getByTestId('amount').textContent).toBe('2'),
)
Expand Down Expand Up @@ -111,9 +111,7 @@ describe('refetchOnFocus tests', () => {
expect(screen.getByTestId('amount').textContent).toBe('1'),
)

act(() => {
fireEvent.focus(window)
})
fireEvent.focus(window)

await delay(150)

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

act(() => {
fireEvent.focus(window)
})
fireEvent.focus(window)
expect(screen.getByTestId('isLoading').textContent).toBe('false')
await waitFor(() =>
expect(screen.getByTestId('isFetching').textContent).toBe('true'),
Expand Down Expand Up @@ -213,9 +209,7 @@ describe('refetchOnFocus tests', () => {

expect(getIncrementedAmountState()).not.toBeUndefined()

await act(async () => {
fireEvent.focus(window)
})
fireEvent.focus(window)

await delay(1)
expect(getIncrementedAmountState()).toBeUndefined()
Expand Down

0 comments on commit 97f96ad

Please sign in to comment.