1- import type { SerializedError , UnknownAction } from '@reduxjs/toolkit' ;
1+ import type { SerializedError , UnknownAction } from '@reduxjs/toolkit'
22import {
33 configureStore ,
44 createListenerMiddleware ,
55 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'
88import type {
99 UseMutation ,
1010 UseQuery ,
11- } from '@reduxjs/toolkit/dist/query/react/buildHooks' ;
11+ } from '@reduxjs/toolkit/dist/query/react/buildHooks'
1212import {
1313 QueryStatus ,
1414 createApi ,
1515 fetchBaseQuery ,
1616 skipToken ,
17- } from '@reduxjs/toolkit/query/react' ;
17+ } from '@reduxjs/toolkit/query/react'
1818import {
1919 act ,
2020 fireEvent ,
2121 render ,
2222 renderHook ,
2323 screen ,
2424 waitFor ,
25- renderHook ,
2625} from '@testing-library/react'
2726import 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'
2930import {
3031 actionsReducer ,
3132 setupApiStore ,
3233 useRenderCounter ,
3334 waitMs ,
3435 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'
4041
4142// Just setup a temporary in-memory counter for tests that `getIncrementedAmount`.
4243// This can be used to test how many renders happen due to data changes or
@@ -188,7 +189,7 @@ describe('hooks tests', () => {
188189
189190 test ( 'useQuery hook sets isFetching=true whenever a request is in flight' , async ( ) => {
190191 function User ( ) {
191- const [ value , setValue ] = React . useState ( 0 )
192+ const [ value , setValue ] = useState ( 0 )
192193
193194 const { isFetching } = api . endpoints . getUser . useQuery ( 1 , {
194195 skip : value < 1 ,
@@ -229,7 +230,7 @@ describe('hooks tests', () => {
229230 test ( 'useQuery hook sets isLoading=true only on initial request' , async ( ) => {
230231 let refetch : any , isLoading : boolean , isFetching : boolean
231232 function User ( ) {
232- const [ value , setValue ] = React . useState ( 0 )
233+ const [ value , setValue ] = useState ( 0 )
233234
234235 ; ( { isLoading, isFetching, refetch } = api . endpoints . getUser . useQuery (
235236 2 ,
@@ -278,7 +279,7 @@ describe('hooks tests', () => {
278279 test ( 'useQuery hook sets isLoading and isFetching to the correct states' , async ( ) => {
279280 let refetchMe : ( ) => void = ( ) => { }
280281 function User ( ) {
281- const [ value , setValue ] = React . useState ( 0 )
282+ const [ value , setValue ] = useState ( 0 )
282283 getRenderCount = useRenderCounter ( )
283284
284285 const { isLoading, isFetching, refetch } =
@@ -344,10 +345,10 @@ describe('hooks tests', () => {
344345 const { isLoading, isFetching, status } =
345346 api . endpoints . getUser . useQuery ( id )
346347
347- React . useEffect ( ( ) => {
348+ useEffect ( ( ) => {
348349 loadingHist . push ( isLoading )
349350 } , [ isLoading ] )
350- React . useEffect ( ( ) => {
351+ useEffect ( ( ) => {
351352 fetchingHist . push ( isFetching )
352353 } , [ isFetching ] )
353354 return (
@@ -509,7 +510,7 @@ describe('hooks tests', () => {
509510 test ( 'refetchOnMountOrArgChange works as expected when changing skip from false->true' , async ( ) => {
510511 let data , isLoading , isFetching
511512 function User ( ) {
512- const [ skip , setSkip ] = React . useState ( true )
513+ const [ skip , setSkip ] = useState ( true )
513514 ; ( { data, isLoading, isFetching } =
514515 api . endpoints . getIncrementedAmount . useQuery ( undefined , {
515516 refetchOnMountOrArgChange : 0.5 ,
@@ -555,7 +556,7 @@ describe('hooks tests', () => {
555556
556557 let data , isLoading , isFetching
557558 function User ( ) {
558- const [ skip , setSkip ] = React . useState ( true )
559+ const [ skip , setSkip ] = useState ( true )
559560 ; ( { data, isLoading, isFetching } =
560561 api . endpoints . getIncrementedAmount . useQuery ( undefined , {
561562 skip,
@@ -633,7 +634,7 @@ describe('hooks tests', () => {
633634
634635 test ( `useQuery refetches when query args object changes even if serialized args don't change` , async ( ) => {
635636 function ItemList ( ) {
636- const [ pageNumber , setPageNumber ] = React . useState ( 0 )
637+ const [ pageNumber , setPageNumber ] = useState ( 0 )
637638 const { data = [ ] } = api . useListItemsQuery ( { pageNumber } )
638639
639640 const renderedItems = data . map ( ( item ) => (
@@ -908,7 +909,7 @@ describe('hooks tests', () => {
908909 test ( 'useLazyQuery accepts updated subscription options and only dispatches updateSubscriptionOptions when values are updated' , async ( ) => {
909910 let interval = 1000
910911 function User ( ) {
911- const [ options , setOptions ] = React . useState < SubscriptionOptions > ( )
912+ const [ options , setOptions ] = useState < SubscriptionOptions > ( )
912913 const [ fetchUser , { data : hookData , isFetching, isUninitialized } ] =
913914 api . endpoints . getUser . useLazyQuery ( options )
914915 getRenderCount = useRenderCounter ( )
@@ -1066,7 +1067,7 @@ describe('hooks tests', () => {
10661067 test ( 'useLazyQuery hook callback returns various properties to handle the result' , async ( ) => {
10671068 function User ( ) {
10681069 const [ getUser ] = api . endpoints . getUser . useLazyQuery ( )
1069- const [ { successMsg, errMsg, isAborted } , setValues ] = React . useState ( {
1070+ const [ { successMsg, errMsg, isAborted } , setValues ] = useState ( {
10701071 successMsg : '' ,
10711072 errMsg : '' ,
10721073 isAborted : false ,
@@ -1160,7 +1161,7 @@ describe('hooks tests', () => {
11601161 const [ getUser , { data, error } ] =
11611162 api . endpoints . getUserAndForceError . useLazyQuery ( )
11621163
1163- const [ unwrappedError , setUnwrappedError ] = React . useState < any > ( )
1164+ const [ unwrappedError , setUnwrappedError ] = useState < any > ( )
11641165
11651166 const handleClick = async ( ) => {
11661167 const res = getUser ( 1 )
@@ -1213,7 +1214,7 @@ describe('hooks tests', () => {
12131214 function User ( ) {
12141215 const [ getUser , { data, error } ] = api . endpoints . getUser . useLazyQuery ( )
12151216
1216- const [ unwrappedResult , setUnwrappedResult ] = React . useState <
1217+ const [ unwrappedResult , setUnwrappedResult ] = useState <
12171218 undefined | { name : string }
12181219 > ( )
12191220
@@ -1320,9 +1321,9 @@ describe('hooks tests', () => {
13201321 test ( 'useMutation hook callback returns various properties to handle the result' , async ( ) => {
13211322 function User ( ) {
13221323 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 )
13261327
13271328 const handleClick = async ( ) => {
13281329 const res = updateUser ( { name : 'Banana' } )
@@ -1750,14 +1751,18 @@ describe('hooks tests', () => {
17501751 test ( 'initially failed useQueries that provide an tag will refetch after a mutation invalidates it' , async ( ) => {
17511752 const checkSessionData = { name : 'matt' }
17521753 server . use (
1753- http . get ( 'https://example.com/me' , ( ) => {
1754+ http . get (
1755+ 'https://example.com/me' ,
1756+ ( ) => {
17541757 return HttpResponse . json ( null , { status : 500 } )
1755- } , { once : true } ) ,
1758+ } ,
1759+ { once : true }
1760+ ) ,
17561761 http . get ( 'https://example.com/me' , ( ) => {
1757- return HttpResponse . json ( checkSessionData )
1762+ return HttpResponse . json ( checkSessionData )
17581763 } ) ,
17591764 http . post ( 'https://example.com/login' , ( ) => {
1760- return HttpResponse . json ( null , { status : 200 } )
1765+ return HttpResponse . json ( null , { status : 200 } )
17611766 } )
17621767 )
17631768 let data , isLoading , isError
@@ -1976,12 +1981,12 @@ describe('hooks with createApi defaults set', () => {
19761981
19771982 const handlers = [
19781983 http . get ( 'https://example.com/posts' , ( ) => {
1979- return HttpResponse . json ( posts )
1984+ return HttpResponse . json ( posts )
19801985 } ) ,
19811986 http . put < { id : string } , Partial < Post > > (
19821987 'https://example.com/post/:id' ,
19831988 async ( { request, params } ) => {
1984- const body = await request . json ( ) ;
1989+ const body = await request . json ( )
19851990 const id = Number ( params . id )
19861991 const idx = posts . findIndex ( ( post ) => post . id === id )
19871992
@@ -1997,20 +2002,23 @@ describe('hooks with createApi defaults set', () => {
19972002 )
19982003 posts = [ ...newPosts ]
19992004
2000- return HttpResponse . json ( posts )
2005+ return HttpResponse . json ( posts )
20012006 }
20022007 ) ,
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+ } )
20122019 return HttpResponse . json ( posts )
2013- } ) ,
2020+ }
2021+ ) ,
20142022 ]
20152023
20162024 server . use ( ...handlers )
@@ -2292,7 +2300,7 @@ describe('hooks with createApi defaults set', () => {
22922300 } )
22932301 getRenderCount = useRenderCounter ( )
22942302
2295- React . useEffect ( ( ) => {
2303+ useEffect ( ( ) => {
22962304 expectablePost = post
22972305 } , [ post ] )
22982306
@@ -2379,7 +2387,6 @@ describe('hooks with createApi defaults set', () => {
23792387
23802388 test ( 'useQuery with selectFromResult option has a type error if the result is not an object' , async ( ) => {
23812389 function SelectedPost ( ) {
2382-
23832390 const res2 = api . endpoints . getPosts . useQuery ( undefined , {
23842391 // selectFromResult must always return an object
23852392 selectFromResult : ( { data } ) => ( { size : data ?. length ?? 0 } ) ,
0 commit comments