1
- import * as React from 'react '
1
+ import type { ThunkDispatch , UnknownAction } from '@reduxjs/toolkit '
2
2
import type { BaseQueryFn } from '@reduxjs/toolkit/query/react'
3
3
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'
9
4
import {
5
+ act ,
10
6
fireEvent ,
11
7
render ,
12
- waitFor ,
13
- screen ,
14
- act ,
15
8
renderHook ,
9
+ screen ,
10
+ waitFor ,
16
11
} 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'
17
16
import { useDispatch } from 'react-redux'
18
- import type { UnknownAction , ThunkDispatch } from '@reduxjs/toolkit '
17
+ import { expectExactType , hookWaitFor , setupApiStore } from './helpers '
19
18
import type { BaseQueryApi } from '../baseQueryTypes'
19
+ import { server } from './mocks/server'
20
20
21
21
const baseQuery = fetchBaseQuery ( { baseUrl : 'https://example.com' } )
22
22
@@ -34,8 +34,10 @@ const api = createApi({
34
34
35
35
const storeRef = setupApiStore ( api )
36
36
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 }
39
41
)
40
42
41
43
describe ( 'fetchBaseQuery' , ( ) => {
@@ -86,7 +88,7 @@ describe('query error handling', () => {
86
88
test ( 'success' , async ( ) => {
87
89
server . use (
88
90
http . get ( 'https://example.com/query' , ( ) =>
89
- HttpResponse . json ( { value : 'success' } )
91
+ HttpResponse . json ( { value : 'success' } )
90
92
)
91
93
)
92
94
const { result } = renderHook ( ( ) => api . endpoints . query . useQuery ( { } ) , {
@@ -107,7 +109,7 @@ describe('query error handling', () => {
107
109
test ( 'error' , async ( ) => {
108
110
server . use (
109
111
http . get ( 'https://example.com/query' , ( ) =>
110
- HttpResponse . json ( { value : 'error' } , { status : 500 } )
112
+ HttpResponse . json ( { value : 'error' } , { status : 500 } )
111
113
)
112
114
)
113
115
const { result } = renderHook ( ( ) => api . endpoints . query . useQuery ( { } ) , {
@@ -131,7 +133,7 @@ describe('query error handling', () => {
131
133
test ( 'success -> error' , async ( ) => {
132
134
server . use (
133
135
http . get ( 'https://example.com/query' , ( ) =>
134
- HttpResponse . json ( { value : 'success' } )
136
+ HttpResponse . json ( { value : 'success' } )
135
137
)
136
138
)
137
139
const { result } = renderHook ( ( ) => api . endpoints . query . useQuery ( { } ) , {
@@ -149,8 +151,10 @@ describe('query error handling', () => {
149
151
)
150
152
151
153
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 }
154
158
)
155
159
)
156
160
@@ -175,12 +179,14 @@ describe('query error handling', () => {
175
179
test ( 'error -> success' , async ( ) => {
176
180
server . use (
177
181
http . get ( 'https://example.com/query' , ( ) =>
178
- HttpResponse . json ( { value : 'success' } )
182
+ HttpResponse . json ( { value : 'success' } )
179
183
)
180
184
)
181
185
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 }
184
190
)
185
191
)
186
192
const { result } = renderHook ( ( ) => api . endpoints . query . useQuery ( { } ) , {
@@ -218,7 +224,7 @@ describe('mutation error handling', () => {
218
224
test ( 'success' , async ( ) => {
219
225
server . use (
220
226
http . post ( 'https://example.com/mutation' , ( ) =>
221
- HttpResponse . json ( { value : 'success' } )
227
+ HttpResponse . json ( { value : 'success' } )
222
228
)
223
229
)
224
230
const { result } = renderHook ( ( ) => api . endpoints . mutation . useMutation ( ) , {
@@ -243,7 +249,7 @@ describe('mutation error handling', () => {
243
249
test ( 'error' , async ( ) => {
244
250
server . use (
245
251
http . post ( 'https://example.com/mutation' , ( ) =>
246
- HttpResponse . json ( { value : 'error' } , { status : 500 } )
252
+ HttpResponse . json ( { value : 'error' } , { status : 500 } )
247
253
)
248
254
)
249
255
const { result } = renderHook ( ( ) => api . endpoints . mutation . useMutation ( ) , {
@@ -271,7 +277,7 @@ describe('mutation error handling', () => {
271
277
test ( 'success -> error' , async ( ) => {
272
278
server . use (
273
279
http . post ( 'https://example.com/mutation' , ( ) =>
274
- HttpResponse . json ( { value : 'success' } )
280
+ HttpResponse . json ( { value : 'success' } )
275
281
)
276
282
)
277
283
const { result } = renderHook ( ( ) => api . endpoints . mutation . useMutation ( ) , {
@@ -295,8 +301,10 @@ describe('mutation error handling', () => {
295
301
}
296
302
297
303
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 }
300
308
)
301
309
)
302
310
@@ -324,12 +332,14 @@ describe('mutation error handling', () => {
324
332
test ( 'error -> success' , async ( ) => {
325
333
server . use (
326
334
http . post ( 'https://example.com/mutation' , ( ) =>
327
- HttpResponse . json ( { value : 'success' } )
335
+ HttpResponse . json ( { value : 'success' } )
328
336
)
329
337
)
330
338
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 }
333
343
)
334
344
)
335
345
@@ -443,7 +453,7 @@ describe('custom axios baseQuery', () => {
443
453
test ( 'axios errors behave as expected' , async ( ) => {
444
454
server . use (
445
455
http . get ( 'https://example.com/success' , ( ) =>
446
- HttpResponse . json ( { value : 'error' } , { status : 500 } )
456
+ HttpResponse . json ( { value : 'error' } , { status : 500 } )
447
457
)
448
458
)
449
459
const { result } = renderHook ( ( ) => api . endpoints . query . useQuery ( ) , {
@@ -481,8 +491,10 @@ describe('error handling in a component', () => {
481
491
482
492
test ( 'a mutation is unwrappable and has the correct types' , async ( ) => {
483
493
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 }
486
498
)
487
499
)
488
500
0 commit comments