@@ -20,10 +20,9 @@ import {
2020 renderHook ,
2121} from '@testing-library/react'
2222import userEvent from '@testing-library/user-event'
23- import { rest } from 'msw'
23+ import { http , HttpResponse } from 'msw'
2424import {
2525 actionsReducer ,
26- ANY ,
2726 expectExactType ,
2827 expectType ,
2928 setupApiStore ,
@@ -104,7 +103,7 @@ const api = createApi({
104103 query : ( update ) => ( { body : update } ) ,
105104 } ) ,
106105 getError : build . query ( {
107- query : ( query ) => '/error' ,
106+ query : ( ) => '/error' ,
108107 } ) ,
109108 listItems : build . query < Item [ ] , { pageNumber : number } > ( {
110109 serializeQueryArgs : ( { endpointName } ) => {
@@ -119,7 +118,7 @@ const api = createApi({
119118 merge : ( currentCache , newItems ) => {
120119 currentCache . push ( ...newItems )
121120 } ,
122- forceRefetch : ( { currentArg , previousArg } ) => {
121+ forceRefetch : ( ) => {
123122 return true
124123 } ,
125124 } ) ,
@@ -757,7 +756,7 @@ describe('hooks tests', () => {
757756 }
758757
759758 // 1) Initial state: an active subscription
760- const { result , rerender, unmount } = renderHook (
759+ const { rerender, unmount } = renderHook (
761760 ( [ arg , options ] : Parameters <
762761 typeof pokemonApi . useGetPokemonByNameQuery
763762 > ) => pokemonApi . useGetPokemonByNameQuery ( arg , options ) ,
@@ -1752,14 +1751,14 @@ describe('hooks tests', () => {
17521751 test ( 'initially failed useQueries that provide an tag will refetch after a mutation invalidates it' , async ( ) => {
17531752 const checkSessionData = { name : 'matt' }
17541753 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 )
17571759 } ) ,
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 } )
17631762 } )
17641763 )
17651764 let data , isLoading , isError
@@ -1977,47 +1976,49 @@ describe('hooks with createApi defaults set', () => {
19771976 posts = [ ...initialPosts ]
19781977
19791978 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 )
19821981 } ) ,
1983- rest . put < Partial < Post > > (
1982+ http . put < Post , Partial < Post > > (
19841983 '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 )
19871987 const idx = posts . findIndex ( ( post ) => post . id === id )
19881988
19891989 const newPosts = posts . map ( ( post , index ) =>
19901990 index !== idx
19911991 ? post
19921992 : {
1993- ...req . body ,
1993+ ...body ,
19941994 id,
1995- name : req . body . name || post . name ,
1995+ name : body ? .name || post . name ,
19961996 fetched_at : new Date ( ) . toUTCString ( ) ,
19971997 }
19981998 )
19991999 posts = [ ...newPosts ]
20002000
2001- return res ( ctx . json ( posts ) )
2001+ return HttpResponse . json ( posts )
20022002 }
20032003 ) ,
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
20062007 startingId += 1
20072008 posts . concat ( {
20082009 ...post ,
20092010 fetched_at : new Date ( ) . toISOString ( ) ,
20102011 id : startingId ,
20112012 } )
2012- return res ( ctx . json ( posts ) )
2013+ return HttpResponse . json ( posts )
20132014 } ) ,
20142015 ]
20152016
20162017 server . use ( ...handlers )
20172018 } )
20182019
20192020 interface Post {
2020- id : number
2021+ id : string
20212022 name : string
20222023 fetched_at : string
20232024 }
@@ -2091,7 +2092,7 @@ describe('hooks with createApi defaults set', () => {
20912092 function SelectedPost ( ) {
20922093 const { post } = api . endpoints . getPosts . useQueryState ( undefined , {
20932094 selectFromResult : ( { data } ) => ( {
2094- post : data ?. find ( ( post ) => post . id === 1 ) ,
2095+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
20952096 } ) ,
20962097 } )
20972098 getRenderCount = useRenderCounter ( )
@@ -2170,7 +2171,7 @@ describe('hooks with createApi defaults set', () => {
21702171 isSuccess,
21712172 isError,
21722173 } ) => ( {
2173- post : data ?. find ( ( post ) => post . id === 1 ) ,
2174+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
21742175 isUninitialized,
21752176 isLoading,
21762177 isFetching,
@@ -2227,7 +2228,7 @@ describe('hooks with createApi defaults set', () => {
22272228 getRenderCount = useRenderCounter ( )
22282229 const { post } = api . endpoints . getPosts . useQuery ( undefined , {
22292230 selectFromResult : ( { data } ) => ( {
2230- post : data ?. find ( ( post ) => post . id === 1 ) ,
2231+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
22312232 } ) ,
22322233 } )
22332234
@@ -2276,7 +2277,7 @@ describe('hooks with createApi defaults set', () => {
22762277 </ button >
22772278 < button
22782279 data-testid = "updatePost"
2279- onClick = { ( ) => updatePost ( { id : 1 , name : 'supercoooll!' } ) }
2280+ onClick = { ( ) => updatePost ( { id : 1 , name : 'supercoooll!' } as any ) }
22802281 >
22812282 Update post
22822283 </ button >
@@ -2287,7 +2288,7 @@ describe('hooks with createApi defaults set', () => {
22872288 function SelectedPost ( ) {
22882289 const { post } = api . endpoints . getPosts . useQuery ( undefined , {
22892290 selectFromResult : ( { data } ) => ( {
2290- post : data ?. find ( ( post ) => post . id === 1 ) ,
2291+ post : data ?. find ( ( post ) => post . id === 1 as any ) ,
22912292 } ) ,
22922293 } )
22932294 getRenderCount = useRenderCounter ( )
@@ -2377,11 +2378,6 @@ describe('hooks with createApi defaults set', () => {
23772378
23782379 test ( 'useQuery with selectFromResult option has a type error if the result is not an object' , async ( ) => {
23792380 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- } )
23852381
23862382 const res2 = api . endpoints . getPosts . useQuery ( undefined , {
23872383 // selectFromResult must always return an object
0 commit comments