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 committed Nov 1, 2024
1 parent c7a523f commit fe43800
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 20 additions & 8 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 @@ -46,7 +47,7 @@ interface Item {

const api = createApi({
baseQuery: async (arg: any) => {
await waitMs(150)
await waitForFakeTimer(10)
if (arg?.body && 'amount' in arg.body) {
amount += 1
}
Expand Down Expand Up @@ -942,15 +943,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(10)
})

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

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

// Find if at any time the isLoading state has reverted
Expand Down Expand Up @@ -1669,7 +1670,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({

Check failure on line 1677 in packages/toolkit/src/query/tests/buildHooks.test.tsx

View workflow job for this annotation

GitHub Actions / Test against dist (20.x)

src/query/tests/buildHooks.test.tsx > hooks tests > usePrefetch > usePrefetch respects force arg

AssertionError: expected { status: 'fulfilled', …(10) } to deeply equal { data: { name: 'Timmy' }, …(11) } - Expected + Received Object { "data": Object { "name": "Timmy", }, "endpointName": "getUser", - "error": undefined, "fulfilledTimeStamp": Any<Number>, "isError": false, - "isLoading": true, - "isSuccess": false, + "isLoading": false, + "isSuccess": true, "isUninitialized": false, "originalArgs": 4, "requestId": Any<String>, "startedTimeStamp": Any<Number>, - "status": "pending", + "status": "fulfilled", } ❯ src/query/tests/buildHooks.test.tsx:1677:9
Expand Down Expand Up @@ -1806,7 +1808,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({

Check failure on line 1814 in packages/toolkit/src/query/tests/buildHooks.test.tsx

View workflow job for this annotation

GitHub Actions / Test against dist (20.x)

src/query/tests/buildHooks.test.tsx > hooks tests > usePrefetch > usePrefetch respects ifOlderThan when it evaluates to true

AssertionError: expected { status: 'fulfilled', …(10) } to deeply equal { data: { name: 'Timmy' }, …(10) } - Expected + Received Object { "data": Object { "name": "Timmy", }, "endpointName": "getUser", "fulfilledTimeStamp": Any<Number>, "isError": false, - "isLoading": true, - "isSuccess": false, + "isLoading": false, + "isSuccess": true, "isUninitialized": false, "originalArgs": 47, "requestId": Any<String>, "startedTimeStamp": Any<Number>, - "status": "pending", + "status": "fulfilled", } ❯ src/query/tests/buildHooks.test.tsx:1814:9
Expand Down Expand Up @@ -1906,7 +1908,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 @@ -2798,6 +2800,11 @@ describe('skip behavior', () => {
await act(async () => {
rerender([1])
})

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

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

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

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

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

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

0 comments on commit fe43800

Please sign in to comment.