@@ -20,10 +20,9 @@ import {
20
20
renderHook ,
21
21
} from '@testing-library/react'
22
22
import userEvent from '@testing-library/user-event'
23
- import { rest } from 'msw'
23
+ import { http , HttpResponse } from 'msw'
24
24
import {
25
25
actionsReducer ,
26
- ANY ,
27
26
expectExactType ,
28
27
expectType ,
29
28
setupApiStore ,
@@ -104,7 +103,7 @@ const api = createApi({
104
103
query : ( update ) => ( { body : update } ) ,
105
104
} ) ,
106
105
getError : build . query ( {
107
- query : ( query ) => '/error' ,
106
+ query : ( ) => '/error' ,
108
107
} ) ,
109
108
listItems : build . query < Item [ ] , { pageNumber : number } > ( {
110
109
serializeQueryArgs : ( { endpointName } ) => {
@@ -119,7 +118,7 @@ const api = createApi({
119
118
merge : ( currentCache , newItems ) => {
120
119
currentCache . push ( ...newItems )
121
120
} ,
122
- forceRefetch : ( { currentArg , previousArg } ) => {
121
+ forceRefetch : ( ) => {
123
122
return true
124
123
} ,
125
124
} ) ,
@@ -757,7 +756,7 @@ describe('hooks tests', () => {
757
756
}
758
757
759
758
// 1) Initial state: an active subscription
760
- const { result , rerender, unmount } = renderHook (
759
+ const { rerender, unmount } = renderHook (
761
760
( [ arg , options ] : Parameters <
762
761
typeof pokemonApi . useGetPokemonByNameQuery
763
762
> ) => pokemonApi . useGetPokemonByNameQuery ( arg , options ) ,
@@ -1752,14 +1751,14 @@ describe('hooks tests', () => {
1752
1751
test ( 'initially failed useQueries that provide an tag will refetch after a mutation invalidates it' , async ( ) => {
1753
1752
const checkSessionData = { name : 'matt' }
1754
1753
server . use (
1755
- rest . get ( 'https://example.com/me' , ( req , res , ctx ) => {
1756
- return res . once ( ctx . status ( 500 ) )
1754
+ http . get ( 'https://example.com/me' , ( ) => {
1755
+ return HttpResponse . json ( null , { status : 500 } )
1756
+ } , { once : true } ) ,
1757
+ http . get ( 'https://example.com/me' , ( ) => {
1758
+ return HttpResponse . json ( checkSessionData )
1757
1759
} ) ,
1758
- rest . get ( 'https://example.com/me' , ( req , res , ctx ) => {
1759
- return res ( ctx . json ( checkSessionData ) )
1760
- } ) ,
1761
- rest . post ( 'https://example.com/login' , ( req , res , ctx ) => {
1762
- return res ( ctx . status ( 200 ) )
1760
+ http . post ( 'https://example.com/login' , ( ) => {
1761
+ return HttpResponse . json ( null , { status : 200 } )
1763
1762
} )
1764
1763
)
1765
1764
let data , isLoading , isError
@@ -1977,47 +1976,49 @@ describe('hooks with createApi defaults set', () => {
1977
1976
posts = [ ...initialPosts ]
1978
1977
1979
1978
const handlers = [
1980
- rest . get ( 'https://example.com/posts' , ( req , res , ctx ) => {
1981
- return res ( ctx . json ( posts ) )
1979
+ http . get ( 'https://example.com/posts' , ( ) => {
1980
+ return HttpResponse . json ( posts )
1982
1981
} ) ,
1983
- rest . put < Partial < Post > > (
1982
+ http . put < Post , Partial < Post > > (
1984
1983
'https://example.com/post/:id' ,
1985
- ( req , res , ctx ) => {
1986
- const id = Number ( req . params . id )
1984
+ async ( { request, params } ) => {
1985
+ const body = await request . json ( ) ;
1986
+ const id = Number ( params . id )
1987
1987
const idx = posts . findIndex ( ( post ) => post . id === id )
1988
1988
1989
1989
const newPosts = posts . map ( ( post , index ) =>
1990
1990
index !== idx
1991
1991
? post
1992
1992
: {
1993
- ...req . body ,
1993
+ ...body ,
1994
1994
id,
1995
- name : req . body . name || post . name ,
1995
+ name : body ? .name || post . name ,
1996
1996
fetched_at : new Date ( ) . toUTCString ( ) ,
1997
1997
}
1998
1998
)
1999
1999
posts = [ ...newPosts ]
2000
2000
2001
- return res ( ctx . json ( posts ) )
2001
+ return HttpResponse . json ( posts )
2002
2002
}
2003
2003
) ,
2004
- rest . post ( 'https://example.com/post' , ( req , res , ctx ) => {
2005
- let post = req . body as Omit < Post , 'id' >
2004
+ http . post < any , Omit < Post , 'id' > > ( 'https://example.com/post' , async ( { request } ) => {
2005
+ const body = await request . json ( ) ;
2006
+ let post = body
2006
2007
startingId += 1
2007
2008
posts . concat ( {
2008
2009
...post ,
2009
2010
fetched_at : new Date ( ) . toISOString ( ) ,
2010
2011
id : startingId ,
2011
2012
} )
2012
- return res ( ctx . json ( posts ) )
2013
+ return HttpResponse . json ( posts )
2013
2014
} ) ,
2014
2015
]
2015
2016
2016
2017
server . use ( ...handlers )
2017
2018
} )
2018
2019
2019
2020
interface Post {
2020
- id : number
2021
+ id : string
2021
2022
name : string
2022
2023
fetched_at : string
2023
2024
}
@@ -2091,7 +2092,7 @@ describe('hooks with createApi defaults set', () => {
2091
2092
function SelectedPost ( ) {
2092
2093
const { post } = api . endpoints . getPosts . useQueryState ( undefined , {
2093
2094
selectFromResult : ( { data } ) => ( {
2094
- post : data ?. find ( ( post ) => post . id === 1 ) ,
2095
+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
2095
2096
} ) ,
2096
2097
} )
2097
2098
getRenderCount = useRenderCounter ( )
@@ -2170,7 +2171,7 @@ describe('hooks with createApi defaults set', () => {
2170
2171
isSuccess,
2171
2172
isError,
2172
2173
} ) => ( {
2173
- post : data ?. find ( ( post ) => post . id === 1 ) ,
2174
+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
2174
2175
isUninitialized,
2175
2176
isLoading,
2176
2177
isFetching,
@@ -2227,7 +2228,7 @@ describe('hooks with createApi defaults set', () => {
2227
2228
getRenderCount = useRenderCounter ( )
2228
2229
const { post } = api . endpoints . getPosts . useQuery ( undefined , {
2229
2230
selectFromResult : ( { data } ) => ( {
2230
- post : data ?. find ( ( post ) => post . id === 1 ) ,
2231
+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
2231
2232
} ) ,
2232
2233
} )
2233
2234
@@ -2276,7 +2277,7 @@ describe('hooks with createApi defaults set', () => {
2276
2277
</ button >
2277
2278
< button
2278
2279
data-testid = "updatePost"
2279
- onClick = { ( ) => updatePost ( { id : 1 , name : 'supercoooll!' } ) }
2280
+ onClick = { ( ) => updatePost ( { id : 1 , name : 'supercoooll!' } as any ) }
2280
2281
>
2281
2282
Update post
2282
2283
</ button >
@@ -2287,7 +2288,7 @@ describe('hooks with createApi defaults set', () => {
2287
2288
function SelectedPost ( ) {
2288
2289
const { post } = api . endpoints . getPosts . useQuery ( undefined , {
2289
2290
selectFromResult : ( { data } ) => ( {
2290
- post : data ?. find ( ( post ) => post . id === 1 ) ,
2291
+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
2291
2292
} ) ,
2292
2293
} )
2293
2294
getRenderCount = useRenderCounter ( )
@@ -2377,11 +2378,6 @@ describe('hooks with createApi defaults set', () => {
2377
2378
2378
2379
test ( 'useQuery with selectFromResult option has a type error if the result is not an object' , async ( ) => {
2379
2380
function SelectedPost ( ) {
2380
- const _res1 = api . endpoints . getPosts . useQuery ( undefined , {
2381
- // selectFromResult must always return an object
2382
- // @ts -expect-error
2383
- selectFromResult : ( { data } ) => data ?. length ?? 0 ,
2384
- } )
2385
2381
2386
2382
const res2 = api . endpoints . getPosts . useQuery ( undefined , {
2387
2383
// selectFromResult must always return an object
0 commit comments