1
+ import type { SerializedError } from '@reduxjs/toolkit'
1
2
import { configureStore } from '@reduxjs/toolkit'
2
3
import type { BaseQueryFn , FetchBaseQueryError } from '@reduxjs/toolkit/query'
3
4
import { createApi , fetchBaseQuery } from '@reduxjs/toolkit/query'
4
5
import type { Post } from './mocks/server'
5
6
import { posts } from './mocks/server'
6
7
import { actionsReducer , setupApiStore } from './helpers'
8
+ import type { QuerySubState } from '@reduxjs/toolkit/dist/query/core/apiState'
7
9
8
10
describe ( 'queryFn base implementation tests' , ( ) => {
9
11
const baseQuery : BaseQueryFn < string , { wrappedByBaseQuery : string } , string > =
@@ -172,7 +174,15 @@ describe('queryFn base implementation tests', () => {
172
174
[ 'withAsyncThrowingQueryFn' , withAsyncThrowingQueryFn , 'throw' ] ,
173
175
] ) ( '%s1' , async ( endpointName , endpoint , expectedResult ) => {
174
176
const thunk = endpoint . initiate ( endpointName )
175
- const result = await store . dispatch ( thunk )
177
+ let result : undefined | QuerySubState < any > = undefined
178
+ await expect ( async ( ) => {
179
+ result = await store . dispatch ( thunk )
180
+ } ) . toHaveConsoleOutput (
181
+ endpointName . includes ( 'Throw' )
182
+ ? `An unhandled error occured processing a request for the endpoint "${ endpointName } ".
183
+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${ endpointName } )]`
184
+ : ''
185
+ )
176
186
if ( expectedResult === 'data' ) {
177
187
expect ( result ) . toEqual (
178
188
expect . objectContaining ( {
@@ -209,7 +219,19 @@ describe('queryFn base implementation tests', () => {
209
219
] ,
210
220
] ) ( '%s' , async ( endpointName , endpoint , expectedResult ) => {
211
221
const thunk = endpoint . initiate ( endpointName )
212
- const result = await store . dispatch ( thunk )
222
+ let result :
223
+ | undefined
224
+ | { data : string }
225
+ | { error : string | SerializedError } = undefined
226
+ await expect ( async ( ) => {
227
+ result = await store . dispatch ( thunk )
228
+ } ) . toHaveConsoleOutput (
229
+ endpointName . includes ( 'Throw' )
230
+ ? `An unhandled error occured processing a request for the endpoint "${ endpointName } ".
231
+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: resultFrom(${ endpointName } )]`
232
+ : ''
233
+ )
234
+
213
235
if ( expectedResult === 'data' ) {
214
236
expect ( result ) . toEqual (
215
237
expect . objectContaining ( {
@@ -236,17 +258,32 @@ describe('queryFn base implementation tests', () => {
236
258
test ( 'neither provided' , async ( ) => {
237
259
{
238
260
const thunk = withNeither . initiate ( 'withNeither' )
239
- const result = await store . dispatch ( thunk )
240
- expect ( result . error ) . toEqual (
261
+ let result : QuerySubState < any >
262
+ await expect ( async ( ) => {
263
+ result = await store . dispatch ( thunk )
264
+ } ) . toHaveConsoleOutput (
265
+ `An unhandled error occured processing a request for the endpoint "withNeither".
266
+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`
267
+ )
268
+ expect ( result ! . error ) . toEqual (
241
269
expect . objectContaining ( {
242
270
message : 'endpointDefinition.queryFn is not a function' ,
243
271
} )
244
272
)
245
273
}
246
274
{
275
+ let result :
276
+ | undefined
277
+ | { data : string }
278
+ | { error : string | SerializedError } = undefined
247
279
const thunk = mutationWithNeither . initiate ( 'mutationWithNeither' )
248
- const result = await store . dispatch ( thunk )
249
- expect ( 'error' in result && result . error ) . toEqual (
280
+ await expect ( async ( ) => {
281
+ result = await store . dispatch ( thunk )
282
+ } ) . toHaveConsoleOutput (
283
+ `An unhandled error occured processing a request for the endpoint "mutationWithNeither".
284
+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [TypeError: endpointDefinition.queryFn is not a function]`
285
+ )
286
+ expect ( ( result as any ) . error ) . toEqual (
250
287
expect . objectContaining ( {
251
288
message : 'endpointDefinition.queryFn is not a function' ,
252
289
} )
@@ -336,11 +373,17 @@ describe('usage scenario tests', () => {
336
373
} )
337
374
338
375
it ( 'can wrap a service like Firebase and handle errors' , async ( ) => {
339
- const result = await storeRef . store . dispatch (
340
- api . endpoints . getMissingFirebaseUser . initiate ( 1 )
341
- )
342
- expect ( result . data ) . toBeUndefined ( )
343
- expect ( result . error ) . toEqual (
376
+ let result : QuerySubState < any >
377
+ await expect ( async ( ) => {
378
+ result = await storeRef . store . dispatch (
379
+ api . endpoints . getMissingFirebaseUser . initiate ( 1 )
380
+ )
381
+ } )
382
+ . toHaveConsoleOutput ( `An unhandled error occured processing a request for the endpoint "getMissingFirebaseUser".
383
+ In the case of an unhandled error, no tags will be "provided" or "invalidated". [Error: Missing user]` )
384
+
385
+ expect ( result ! . data ) . toBeUndefined ( )
386
+ expect ( result ! . error ) . toEqual (
344
387
expect . objectContaining ( {
345
388
message : 'Missing user' ,
346
389
name : 'Error' ,
0 commit comments