Skip to content

Commit

Permalink
Merge pull request #4686 from isqua/act
Browse files Browse the repository at this point in the history
Update @testing-library/react from 13.3.0 to 16.0.1; Fixes #4685
  • Loading branch information
markerikson authored Nov 28, 2024
2 parents 3a6ec28 + 23a1c3f commit e848a55
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 76 deletions.
3 changes: 2 additions & 1 deletion examples/query/react/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"react-scripts": "5.0.1"
},
"devDependencies": {
"@testing-library/react": "^13.3.0",
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.0.1",
"@types/jest": "^26.0.23",
"@types/react": "^18.0.5",
"@types/react-dom": "^18.0.5",
Expand Down
3 changes: 2 additions & 1 deletion examples/query/react/kitchen-sink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"react-scripts": "5.0.1"
},
"devDependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^5.11.5",
"@testing-library/react": "^13.3.0",
"@testing-library/react": "^16.0.1",
"@types/jest": "^26.0.23",
"@types/node": "^14.14.6",
"@types/react": "^18.0.5",
Expand Down
5 changes: 3 additions & 2 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@
"@phryneas/ts-version": "^1.0.2",
"@size-limit/file": "^11.0.1",
"@size-limit/webpack": "^11.0.1",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.1.5",
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@types/babel__core": "^7.20.5",
"@types/babel__helper-module-imports": "^7.18.3",
"@types/json-stringify-safe": "^5.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/toolkit/src/query/tests/apiProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const api = createApi({
}),
})

afterEach(() => {
vi.resetAllMocks()
})

describe('ApiProvider', () => {
test('ApiProvider allows a user to make queries without a traditional Redux setup', async () => {
function User() {
Expand Down Expand Up @@ -72,6 +76,8 @@ describe('ApiProvider', () => {
expect(getByTestId('isFetching').textContent).toBe('false')
})
test('ApiProvider throws if nested inside a Redux context', () => {
// Intentionally swallow the "unhandled error" message
vi.spyOn(console, 'error').mockImplementation(() => {})
expect(() =>
render(
<Provider store={configureStore({ reducer: () => null })}>
Expand Down
31 changes: 21 additions & 10 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 @@ -465,7 +466,6 @@ describe('hooks tests', () => {
<div>
<button
onClick={() => {
console.log('Refetching')
refetch()
}}
>
Expand Down Expand Up @@ -915,7 +915,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 +1095,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 +1864,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 +2002,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 +2102,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 +2994,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 +3036,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 +3070,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
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ describe('fetchBaseQuery', () => {
prepare.apply(undefined, arguments as unknown as any[])
},
})
baseQuery('http://example.com', commonBaseQueryApi, {
baseQuery('https://example.com', commonBaseQueryApi, {
foo: 'baz',
bar: 5,
})
Expand Down
6 changes: 6 additions & 0 deletions packages/toolkit/src/query/tests/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export const posts: Record<string, Post> = {
}

export const handlers = [
http.get(
'https://example.com',
async ({ request, params, cookies, requestId }) => {
HttpResponse.json({ value: 'success' })
},
),
http.get(
'https://example.com/echo',
async ({ request, params, cookies, requestId }) => {
Expand Down
37 changes: 22 additions & 15 deletions packages/toolkit/src/query/tests/optimisticUpserts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,27 +399,34 @@ describe('upsertQueryEntries', () => {
storeRef.store.dispatch(entriesAction)

// Tricky timing. The cache data promises will be resolved
// in microtasks, so we just need any async delay here.
await delay(1)
// in microtasks. We need to wait for them. Best to do this
// in a loop just to avoid a hardcoded delay, but also this
// needs to complete before `keepUnusedDataFor` expires them.
await waitFor(
() => {
const state = storeRef.store.getState()

// onCacheEntryAdded should have run for each post,
// including cache data being resolved
for (const post of posts) {
const matchingSideEffectAction = state.actions.find(
(action) =>
postAddedAction.match(action) && action.payload === post.id,
)
expect(matchingSideEffectAction).toBeTruthy()
}

const state = storeRef.store.getState()
const selectedData =
api.endpoints.postWithSideEffect.select('1')(state).data

// onCacheEntryAdded should have run for each post,
// including cache data being resolved
for (const post of posts) {
const matchingSideEffectAction = state.actions.find(
(action) => postAddedAction.match(action) && action.payload === post.id,
)
expect(matchingSideEffectAction).toBeTruthy()
}

expect(api.endpoints.postWithSideEffect.select('1')(state).data).toBe(
posts[0],
expect(selectedData).toBe(posts[0])
},
{ timeout: 50, interval: 5 },
)

// The cache data should be removed after the keepUnusedDataFor time,
// so wait longer than that
await delay(20)
await delay(100)

const stateAfter = storeRef.store.getState()

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
Loading

0 comments on commit e848a55

Please sign in to comment.