1
- import type { SerializedError , UnknownAction } from '@reduxjs/toolkit' ;
1
+ import type { SerializedError , UnknownAction } from '@reduxjs/toolkit'
2
2
import {
3
3
configureStore ,
4
4
createListenerMiddleware ,
5
5
createSlice ,
6
- } from '@reduxjs/toolkit' ;
7
- import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState' ;
6
+ } from '@reduxjs/toolkit'
7
+ import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState'
8
8
import type {
9
9
UseMutation ,
10
10
UseQuery ,
11
- } from '@reduxjs/toolkit/dist/query/react/buildHooks' ;
11
+ } from '@reduxjs/toolkit/dist/query/react/buildHooks'
12
12
import {
13
13
QueryStatus ,
14
14
createApi ,
15
15
fetchBaseQuery ,
16
16
skipToken ,
17
- } from '@reduxjs/toolkit/query/react' ;
17
+ } from '@reduxjs/toolkit/query/react'
18
18
import {
19
19
act ,
20
20
fireEvent ,
21
21
render ,
22
22
renderHook ,
23
23
screen ,
24
24
waitFor ,
25
- renderHook ,
26
25
} from '@testing-library/react'
27
26
import userEvent from '@testing-library/user-event'
28
- import { http , HttpResponse } from 'msw'
27
+ import { HttpResponse , http } from 'msw'
28
+ import { useEffect , useState } from 'react'
29
+ import type { MockInstance } from 'vitest'
29
30
import {
30
31
actionsReducer ,
31
32
setupApiStore ,
32
33
useRenderCounter ,
33
34
waitMs ,
34
35
withProvider ,
35
- } from '../../tests/utils/helpers' ;
36
- import { expectExactType , expectType } from '../../tests/utils/typeTestHelpers' ;
37
- import type { SubscriptionSelectors } from '../core/buildMiddleware/types' ;
38
- import { countObjectKeys } from '../utils/countObjectKeys' ;
39
- import { server } from './mocks/server' ;
36
+ } from '../../tests/utils/helpers'
37
+ import { expectExactType , expectType } from '../../tests/utils/typeTestHelpers'
38
+ import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
39
+ import { countObjectKeys } from '../utils/countObjectKeys'
40
+ import { server } from './mocks/server'
40
41
41
42
// Just setup a temporary in-memory counter for tests that `getIncrementedAmount`.
42
43
// This can be used to test how many renders happen due to data changes or
@@ -188,7 +189,7 @@ describe('hooks tests', () => {
188
189
189
190
test ( 'useQuery hook sets isFetching=true whenever a request is in flight' , async ( ) => {
190
191
function User ( ) {
191
- const [ value , setValue ] = React . useState ( 0 )
192
+ const [ value , setValue ] = useState ( 0 )
192
193
193
194
const { isFetching } = api . endpoints . getUser . useQuery ( 1 , {
194
195
skip : value < 1 ,
@@ -229,7 +230,7 @@ describe('hooks tests', () => {
229
230
test ( 'useQuery hook sets isLoading=true only on initial request' , async ( ) => {
230
231
let refetch : any , isLoading : boolean , isFetching : boolean
231
232
function User ( ) {
232
- const [ value , setValue ] = React . useState ( 0 )
233
+ const [ value , setValue ] = useState ( 0 )
233
234
234
235
; ( { isLoading, isFetching, refetch } = api . endpoints . getUser . useQuery (
235
236
2 ,
@@ -278,7 +279,7 @@ describe('hooks tests', () => {
278
279
test ( 'useQuery hook sets isLoading and isFetching to the correct states' , async ( ) => {
279
280
let refetchMe : ( ) => void = ( ) => { }
280
281
function User ( ) {
281
- const [ value , setValue ] = React . useState ( 0 )
282
+ const [ value , setValue ] = useState ( 0 )
282
283
getRenderCount = useRenderCounter ( )
283
284
284
285
const { isLoading, isFetching, refetch } =
@@ -344,10 +345,10 @@ describe('hooks tests', () => {
344
345
const { isLoading, isFetching, status } =
345
346
api . endpoints . getUser . useQuery ( id )
346
347
347
- React . useEffect ( ( ) => {
348
+ useEffect ( ( ) => {
348
349
loadingHist . push ( isLoading )
349
350
} , [ isLoading ] )
350
- React . useEffect ( ( ) => {
351
+ useEffect ( ( ) => {
351
352
fetchingHist . push ( isFetching )
352
353
} , [ isFetching ] )
353
354
return (
@@ -509,7 +510,7 @@ describe('hooks tests', () => {
509
510
test ( 'refetchOnMountOrArgChange works as expected when changing skip from false->true' , async ( ) => {
510
511
let data , isLoading , isFetching
511
512
function User ( ) {
512
- const [ skip , setSkip ] = React . useState ( true )
513
+ const [ skip , setSkip ] = useState ( true )
513
514
; ( { data, isLoading, isFetching } =
514
515
api . endpoints . getIncrementedAmount . useQuery ( undefined , {
515
516
refetchOnMountOrArgChange : 0.5 ,
@@ -555,7 +556,7 @@ describe('hooks tests', () => {
555
556
556
557
let data , isLoading , isFetching
557
558
function User ( ) {
558
- const [ skip , setSkip ] = React . useState ( true )
559
+ const [ skip , setSkip ] = useState ( true )
559
560
; ( { data, isLoading, isFetching } =
560
561
api . endpoints . getIncrementedAmount . useQuery ( undefined , {
561
562
skip,
@@ -633,7 +634,7 @@ describe('hooks tests', () => {
633
634
634
635
test ( `useQuery refetches when query args object changes even if serialized args don't change` , async ( ) => {
635
636
function ItemList ( ) {
636
- const [ pageNumber , setPageNumber ] = React . useState ( 0 )
637
+ const [ pageNumber , setPageNumber ] = useState ( 0 )
637
638
const { data = [ ] } = api . useListItemsQuery ( { pageNumber } )
638
639
639
640
const renderedItems = data . map ( ( item ) => (
@@ -908,7 +909,7 @@ describe('hooks tests', () => {
908
909
test ( 'useLazyQuery accepts updated subscription options and only dispatches updateSubscriptionOptions when values are updated' , async ( ) => {
909
910
let interval = 1000
910
911
function User ( ) {
911
- const [ options , setOptions ] = React . useState < SubscriptionOptions > ( )
912
+ const [ options , setOptions ] = useState < SubscriptionOptions > ( )
912
913
const [ fetchUser , { data : hookData , isFetching, isUninitialized } ] =
913
914
api . endpoints . getUser . useLazyQuery ( options )
914
915
getRenderCount = useRenderCounter ( )
@@ -1066,7 +1067,7 @@ describe('hooks tests', () => {
1066
1067
test ( 'useLazyQuery hook callback returns various properties to handle the result' , async ( ) => {
1067
1068
function User ( ) {
1068
1069
const [ getUser ] = api . endpoints . getUser . useLazyQuery ( )
1069
- const [ { successMsg, errMsg, isAborted } , setValues ] = React . useState ( {
1070
+ const [ { successMsg, errMsg, isAborted } , setValues ] = useState ( {
1070
1071
successMsg : '' ,
1071
1072
errMsg : '' ,
1072
1073
isAborted : false ,
@@ -1160,7 +1161,7 @@ describe('hooks tests', () => {
1160
1161
const [ getUser , { data, error } ] =
1161
1162
api . endpoints . getUserAndForceError . useLazyQuery ( )
1162
1163
1163
- const [ unwrappedError , setUnwrappedError ] = React . useState < any > ( )
1164
+ const [ unwrappedError , setUnwrappedError ] = useState < any > ( )
1164
1165
1165
1166
const handleClick = async ( ) => {
1166
1167
const res = getUser ( 1 )
@@ -1213,7 +1214,7 @@ describe('hooks tests', () => {
1213
1214
function User ( ) {
1214
1215
const [ getUser , { data, error } ] = api . endpoints . getUser . useLazyQuery ( )
1215
1216
1216
- const [ unwrappedResult , setUnwrappedResult ] = React . useState <
1217
+ const [ unwrappedResult , setUnwrappedResult ] = useState <
1217
1218
undefined | { name : string }
1218
1219
> ( )
1219
1220
@@ -1320,9 +1321,9 @@ describe('hooks tests', () => {
1320
1321
test ( 'useMutation hook callback returns various properties to handle the result' , async ( ) => {
1321
1322
function User ( ) {
1322
1323
const [ updateUser ] = api . endpoints . updateUser . useMutation ( )
1323
- const [ successMsg , setSuccessMsg ] = React . useState ( '' )
1324
- const [ errMsg , setErrMsg ] = React . useState ( '' )
1325
- const [ isAborted , setIsAborted ] = React . useState ( false )
1324
+ const [ successMsg , setSuccessMsg ] = useState ( '' )
1325
+ const [ errMsg , setErrMsg ] = useState ( '' )
1326
+ const [ isAborted , setIsAborted ] = useState ( false )
1326
1327
1327
1328
const handleClick = async ( ) => {
1328
1329
const res = updateUser ( { name : 'Banana' } )
@@ -1750,14 +1751,18 @@ describe('hooks tests', () => {
1750
1751
test ( 'initially failed useQueries that provide an tag will refetch after a mutation invalidates it' , async ( ) => {
1751
1752
const checkSessionData = { name : 'matt' }
1752
1753
server . use (
1753
- http . get ( 'https://example.com/me' , ( ) => {
1754
+ http . get (
1755
+ 'https://example.com/me' ,
1756
+ ( ) => {
1754
1757
return HttpResponse . json ( null , { status : 500 } )
1755
- } , { once : true } ) ,
1758
+ } ,
1759
+ { once : true }
1760
+ ) ,
1756
1761
http . get ( 'https://example.com/me' , ( ) => {
1757
- return HttpResponse . json ( checkSessionData )
1762
+ return HttpResponse . json ( checkSessionData )
1758
1763
} ) ,
1759
1764
http . post ( 'https://example.com/login' , ( ) => {
1760
- return HttpResponse . json ( null , { status : 200 } )
1765
+ return HttpResponse . json ( null , { status : 200 } )
1761
1766
} )
1762
1767
)
1763
1768
let data , isLoading , isError
@@ -1976,12 +1981,12 @@ describe('hooks with createApi defaults set', () => {
1976
1981
1977
1982
const handlers = [
1978
1983
http . get ( 'https://example.com/posts' , ( ) => {
1979
- return HttpResponse . json ( posts )
1984
+ return HttpResponse . json ( posts )
1980
1985
} ) ,
1981
1986
http . put < { id : string } , Partial < Post > > (
1982
1987
'https://example.com/post/:id' ,
1983
1988
async ( { request, params } ) => {
1984
- const body = await request . json ( ) ;
1989
+ const body = await request . json ( )
1985
1990
const id = Number ( params . id )
1986
1991
const idx = posts . findIndex ( ( post ) => post . id === id )
1987
1992
@@ -1997,20 +2002,23 @@ describe('hooks with createApi defaults set', () => {
1997
2002
)
1998
2003
posts = [ ...newPosts ]
1999
2004
2000
- return HttpResponse . json ( posts )
2005
+ return HttpResponse . json ( posts )
2001
2006
}
2002
2007
) ,
2003
- http . post < any , Omit < Post , 'id' > > ( 'https://example.com/post' , async ( { request } ) => {
2004
- const body = await request . json ( ) ;
2005
- let post = body
2006
- startingId += 1
2007
- posts . concat ( {
2008
- ...post ,
2009
- fetched_at : new Date ( ) . toISOString ( ) ,
2010
- id : startingId ,
2011
- } )
2008
+ http . post < any , Omit < Post , 'id' > > (
2009
+ 'https://example.com/post' ,
2010
+ async ( { request } ) => {
2011
+ const body = await request . json ( )
2012
+ let post = body
2013
+ startingId += 1
2014
+ posts . concat ( {
2015
+ ...post ,
2016
+ fetched_at : new Date ( ) . toISOString ( ) ,
2017
+ id : startingId ,
2018
+ } )
2012
2019
return HttpResponse . json ( posts )
2013
- } ) ,
2020
+ }
2021
+ ) ,
2014
2022
]
2015
2023
2016
2024
server . use ( ...handlers )
@@ -2292,7 +2300,7 @@ describe('hooks with createApi defaults set', () => {
2292
2300
} )
2293
2301
getRenderCount = useRenderCounter ( )
2294
2302
2295
- React . useEffect ( ( ) => {
2303
+ useEffect ( ( ) => {
2296
2304
expectablePost = post
2297
2305
} , [ post ] )
2298
2306
@@ -2379,7 +2387,6 @@ describe('hooks with createApi defaults set', () => {
2379
2387
2380
2388
test ( 'useQuery with selectFromResult option has a type error if the result is not an object' , async ( ) => {
2381
2389
function SelectedPost ( ) {
2382
-
2383
2390
const res2 = api . endpoints . getPosts . useQuery ( undefined , {
2384
2391
// selectFromResult must always return an object
2385
2392
selectFromResult : ( { data } ) => ( { size : data ?. length ?? 0 } ) ,
0 commit comments