Skip to content

Commit 5def501

Browse files
committed
Use the delay function recommended by MSW
1 parent 4212a97 commit 5def501

16 files changed

+142
-123
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as React from 'react'
2-
import { createApi, ApiProvider } from '@reduxjs/toolkit/query/react'
1+
import { configureStore } from '@reduxjs/toolkit'
2+
import { ApiProvider, createApi } from '@reduxjs/toolkit/query/react'
33
import { fireEvent, render, waitFor } from '@testing-library/react'
4-
import { waitMs } from './helpers'
4+
import { delay } from 'msw'
5+
import * as React from 'react'
56
import { Provider } from 'react-redux'
6-
import { configureStore } from '@reduxjs/toolkit'
77

88
const api = createApi({
99
baseQuery: async (arg: any) => {
10-
await waitMs()
10+
await delay(150)
1111
return { data: arg?.body ? arg.body : null }
1212
},
1313
endpoints: (build) => ({

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import {
55
reactHooksModule,
66
} from '@reduxjs/toolkit/query/react'
77
import { render, screen, waitFor } from '@testing-library/react'
8+
import { delay } from 'msw'
89
import * as React from 'react'
910
import type { ReactReduxContextValue } from 'react-redux'
1011
import {
12+
Provider,
1113
createDispatchHook,
1214
createSelectorHook,
1315
createStoreHook,
14-
Provider,
1516
} from 'react-redux'
16-
import { setupApiStore, useRenderCounter, waitMs } from './helpers'
17+
import { setupApiStore, useRenderCounter } from './helpers'
1718

1819
const MyContext = React.createContext<ReactReduxContextValue>(null as any)
1920

@@ -32,7 +33,7 @@ describe('buildCreateApi', () => {
3233

3334
const api = customCreateApi({
3435
baseQuery: async (arg: any) => {
35-
await waitMs()
36+
await delay(150)
3637

3738
return {
3839
data: arg?.body ? { ...arg.body } : {},
@@ -112,7 +113,7 @@ describe('buildCreateApi', () => {
112113
)
113114
const api = createApi({
114115
baseQuery: async (arg: any) => {
115-
await waitMs()
116+
await delay(150)
116117

117118
return {
118119
data: arg?.body ? { ...arg.body } : {},

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApi } from '@reduxjs/toolkit/query'
2-
import { actionsReducer, setupApiStore, waitMs } from './helpers'
2+
import { delay } from 'msw'
3+
import { actionsReducer, setupApiStore } from './helpers'
34

45
const baseQuery = (args?: any) => ({ data: args })
56
const api = createApi({
@@ -43,7 +44,7 @@ it('invalidates the specified tags', async () => {
4344
await storeRef.store.dispatch(api.util.invalidateTags(['Banana', 'Bread']))
4445

4546
// Slight pause to let the middleware run and such
46-
await waitMs(20)
47+
await delay(20)
4748

4849
const firstSequence = [
4950
api.internalActions.middlewareRegistered.match,
@@ -58,7 +59,7 @@ it('invalidates the specified tags', async () => {
5859
await storeRef.store.dispatch(getBread.initiate(1))
5960
await storeRef.store.dispatch(api.util.invalidateTags([{ type: 'Bread' }]))
6061

61-
await waitMs(20)
62+
await delay(20)
6263

6364
expect(storeRef.store.getState().actions).toMatchSequence(
6465
...firstSequence,

packages/toolkit/src/query/tests/buildSlice.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createSlice } from '@reduxjs/toolkit'
22
import { createApi } from '@reduxjs/toolkit/query'
3+
import { delay } from 'msw'
34
import { setupApiStore } from './helpers'
4-
import { delay } from '../../utils'
55

66
let shouldApiResponseSuccess = true
77

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ import type {
1010
QueryDefinition,
1111
} from '@reduxjs/toolkit/query'
1212
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
13-
import type { SpyInstance } from 'vitest'
14-
import { vi } from 'vitest'
13+
import type { MockInstance } from 'vitest'
1514

1615
import type {
1716
DefinitionsFromApi,
1817
OverrideResultType,
1918
TagTypesFromApi,
2019
} from '@reduxjs/toolkit/dist/query/endpointDefinitions'
21-
import { HttpResponse, http } from 'msw'
20+
import { HttpResponse, delay, http } from 'msw'
2221
import nodeFetch from 'node-fetch'
23-
import type { SerializeQueryArgs } from '../defaultSerializeQueryArgs'
2422
import {
2523
ANY,
26-
expectExactType,
27-
expectType,
2824
getSerializedHeaders,
2925
setupApiStore,
30-
waitMs,
26+
expectExactType,
27+
expectType,
3128
} from './helpers'
29+
import type { SerializeQueryArgs } from '../defaultSerializeQueryArgs'
3230
import { server } from './mocks/server'
3331

34-
const originalEnv = process.env.NODE_ENV
35-
beforeAll(() => void ((process.env as any).NODE_ENV = 'development'))
36-
afterAll(() => void ((process.env as any).NODE_ENV = originalEnv))
32+
beforeAll(() => {
33+
vi.stubEnv('NODE_ENV', 'development')
34+
35+
return vi.unstubAllEnvs
36+
})
3737

38-
let spy: SpyInstance
38+
let spy: MockInstance
3939
beforeAll(() => {
4040
spy = vi.spyOn(console, 'error').mockImplementation(() => {})
4141
})
@@ -185,7 +185,7 @@ describe('wrong tagTypes log errors', () => {
185185
store.dispatch(api.endpoints[endpoint].initiate())
186186
let result: { status: string }
187187
do {
188-
await waitMs(5)
188+
await delay(5)
189189
// @ts-ignore
190190
result = api.endpoints[endpoint].select()(store.getState())
191191
} while (result.status === 'pending')
@@ -460,11 +460,11 @@ describe('endpoint definition typings', () => {
460460
})
461461

462462
storeRef.store.dispatch(api.endpoints.query1.initiate('in1'))
463-
await waitMs(1)
463+
await delay(1)
464464
expect(spy).not.toHaveBeenCalled()
465465

466466
storeRef.store.dispatch(api.endpoints.query2.initiate('in2'))
467-
await waitMs(1)
467+
await delay(1)
468468
expect(spy).toHaveBeenCalledWith(
469469
"Tag type 'missing' was used, but not specified in `tagTypes`!"
470470
)
@@ -805,15 +805,15 @@ describe('query endpoint lifecycles - onStart, onSuccess, onError', () => {
805805
const failAttempt = storeRef.store.dispatch(api.endpoints.query.initiate())
806806
expect(storeRef.store.getState().testReducer.count).toBe(0)
807807
await failAttempt
808-
await waitMs(10)
808+
await delay(10)
809809
expect(storeRef.store.getState().testReducer.count).toBe(-1)
810810

811811
const successAttempt = storeRef.store.dispatch(
812812
api.endpoints.query.initiate()
813813
)
814814
expect(storeRef.store.getState().testReducer.count).toBe(0)
815815
await successAttempt
816-
await waitMs(10)
816+
await delay(10)
817817
expect(storeRef.store.getState().testReducer.count).toBe(1)
818818
})
819819

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ If you have multiple apis, you *have* to specify the reducerPath option when usi
270270
* It would be great to support this case as well, but for now:
271271
* "It is what it is."
272272
*/
273-
test.skip('common: two apis, only second middleware', async () => {
273+
test.todo('common: two apis, only second middleware', async () => {
274274
const store = configureStore({
275275
reducer: {
276276
// @ts-ignore

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import * as React from 'react'
1+
import type { ThunkDispatch, UnknownAction } from '@reduxjs/toolkit'
22
import type { BaseQueryFn } from '@reduxjs/toolkit/query/react'
33
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
4-
import { http, HttpResponse } from 'msw'
5-
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
6-
import axios from 'axios'
7-
import { expectExactType, hookWaitFor, setupApiStore } from './helpers'
8-
import { server } from './mocks/server'
94
import {
5+
act,
106
fireEvent,
117
render,
12-
waitFor,
13-
screen,
14-
act,
158
renderHook,
9+
screen,
10+
waitFor,
1611
} from '@testing-library/react'
12+
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
13+
import axios from 'axios'
14+
import { HttpResponse, http } from 'msw'
15+
import * as React from 'react'
1716
import { useDispatch } from 'react-redux'
18-
import type { UnknownAction, ThunkDispatch } from '@reduxjs/toolkit'
17+
import { expectExactType, hookWaitFor, setupApiStore } from './helpers'
1918
import type { BaseQueryApi } from '../baseQueryTypes'
19+
import { server } from './mocks/server'
2020

2121
const baseQuery = fetchBaseQuery({ baseUrl: 'https://example.com' })
2222

@@ -34,8 +34,10 @@ const api = createApi({
3434

3535
const storeRef = setupApiStore(api)
3636

37-
const failQueryOnce = http.get('/query', () =>
38-
HttpResponse.json({ value: 'failed' }, { status: 500 }), { once: true }
37+
const failQueryOnce = http.get(
38+
'/query',
39+
() => HttpResponse.json({ value: 'failed' }, { status: 500 }),
40+
{ once: true }
3941
)
4042

4143
describe('fetchBaseQuery', () => {
@@ -86,7 +88,7 @@ describe('query error handling', () => {
8688
test('success', async () => {
8789
server.use(
8890
http.get('https://example.com/query', () =>
89-
HttpResponse.json({ value: 'success' })
91+
HttpResponse.json({ value: 'success' })
9092
)
9193
)
9294
const { result } = renderHook(() => api.endpoints.query.useQuery({}), {
@@ -107,7 +109,7 @@ describe('query error handling', () => {
107109
test('error', async () => {
108110
server.use(
109111
http.get('https://example.com/query', () =>
110-
HttpResponse.json({ value: 'error' }, { status: 500 })
112+
HttpResponse.json({ value: 'error' }, { status: 500 })
111113
)
112114
)
113115
const { result } = renderHook(() => api.endpoints.query.useQuery({}), {
@@ -131,7 +133,7 @@ describe('query error handling', () => {
131133
test('success -> error', async () => {
132134
server.use(
133135
http.get('https://example.com/query', () =>
134-
HttpResponse.json({ value: 'success' })
136+
HttpResponse.json({ value: 'success' })
135137
)
136138
)
137139
const { result } = renderHook(() => api.endpoints.query.useQuery({}), {
@@ -149,8 +151,10 @@ describe('query error handling', () => {
149151
)
150152

151153
server.use(
152-
http.get('https://example.com/query', () =>
153-
HttpResponse.json({ value: 'error' }, { status: 500 }), { once: true }
154+
http.get(
155+
'https://example.com/query',
156+
() => HttpResponse.json({ value: 'error' }, { status: 500 }),
157+
{ once: true }
154158
)
155159
)
156160

@@ -175,12 +179,14 @@ describe('query error handling', () => {
175179
test('error -> success', async () => {
176180
server.use(
177181
http.get('https://example.com/query', () =>
178-
HttpResponse.json({ value: 'success' })
182+
HttpResponse.json({ value: 'success' })
179183
)
180184
)
181185
server.use(
182-
http.get('https://example.com/query', () =>
183-
HttpResponse.json({ value: 'error' }, { status: 500 }), { once: true }
186+
http.get(
187+
'https://example.com/query',
188+
() => HttpResponse.json({ value: 'error' }, { status: 500 }),
189+
{ once: true }
184190
)
185191
)
186192
const { result } = renderHook(() => api.endpoints.query.useQuery({}), {
@@ -218,7 +224,7 @@ describe('mutation error handling', () => {
218224
test('success', async () => {
219225
server.use(
220226
http.post('https://example.com/mutation', () =>
221-
HttpResponse.json({ value: 'success' })
227+
HttpResponse.json({ value: 'success' })
222228
)
223229
)
224230
const { result } = renderHook(() => api.endpoints.mutation.useMutation(), {
@@ -243,7 +249,7 @@ describe('mutation error handling', () => {
243249
test('error', async () => {
244250
server.use(
245251
http.post('https://example.com/mutation', () =>
246-
HttpResponse.json({ value: 'error' }, { status: 500 })
252+
HttpResponse.json({ value: 'error' }, { status: 500 })
247253
)
248254
)
249255
const { result } = renderHook(() => api.endpoints.mutation.useMutation(), {
@@ -271,7 +277,7 @@ describe('mutation error handling', () => {
271277
test('success -> error', async () => {
272278
server.use(
273279
http.post('https://example.com/mutation', () =>
274-
HttpResponse.json({ value: 'success' })
280+
HttpResponse.json({ value: 'success' })
275281
)
276282
)
277283
const { result } = renderHook(() => api.endpoints.mutation.useMutation(), {
@@ -295,8 +301,10 @@ describe('mutation error handling', () => {
295301
}
296302

297303
server.use(
298-
http.post('https://example.com/mutation', () =>
299-
HttpResponse.json({ value: 'error' }, { status: 500 }), { once: true }
304+
http.post(
305+
'https://example.com/mutation',
306+
() => HttpResponse.json({ value: 'error' }, { status: 500 }),
307+
{ once: true }
300308
)
301309
)
302310

@@ -324,12 +332,14 @@ describe('mutation error handling', () => {
324332
test('error -> success', async () => {
325333
server.use(
326334
http.post('https://example.com/mutation', () =>
327-
HttpResponse.json({ value: 'success' })
335+
HttpResponse.json({ value: 'success' })
328336
)
329337
)
330338
server.use(
331-
http.post('https://example.com/mutation', () =>
332-
HttpResponse.json({ value: 'error' }, { status: 500 }), { once: true }
339+
http.post(
340+
'https://example.com/mutation',
341+
() => HttpResponse.json({ value: 'error' }, { status: 500 }),
342+
{ once: true }
333343
)
334344
)
335345

@@ -443,7 +453,7 @@ describe('custom axios baseQuery', () => {
443453
test('axios errors behave as expected', async () => {
444454
server.use(
445455
http.get('https://example.com/success', () =>
446-
HttpResponse.json({ value: 'error' }, { status: 500 })
456+
HttpResponse.json({ value: 'error' }, { status: 500 })
447457
)
448458
)
449459
const { result } = renderHook(() => api.endpoints.query.useQuery(), {
@@ -481,8 +491,10 @@ describe('error handling in a component', () => {
481491

482492
test('a mutation is unwrappable and has the correct types', async () => {
483493
server.use(
484-
http.get('https://example.com/success', () =>
485-
HttpResponse.json(mockErrorResponse, { status: 500 }), { once: true }
494+
http.get(
495+
'https://example.com/success',
496+
() => HttpResponse.json(mockErrorResponse, { status: 500 }),
497+
{ once: true }
486498
)
487499
)
488500

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { createApi, fakeBaseQuery } from '@reduxjs/toolkit/query'
2-
import { setupApiStore, waitMs } from './helpers'
31
import type { TagDescription } from '@reduxjs/toolkit/dist/query/endpointDefinitions'
2+
import { createApi, fakeBaseQuery } from '@reduxjs/toolkit/query'
43
import { waitFor } from '@testing-library/react'
4+
import { delay } from 'msw'
5+
import { setupApiStore } from './helpers'
56

67
const tagTypes = [
78
'apple',
@@ -135,7 +136,7 @@ test.each(caseMatrix)(
135136

136137
store.dispatch(invalidating.initiate())
137138
expect(queryCount).toBe(1)
138-
await waitMs(2)
139+
await delay(2)
139140
expect(queryCount).toBe(shouldInvalidate ? 2 : 1)
140141
}
141142
)

0 commit comments

Comments
 (0)