@@ -876,8 +876,13 @@ describe('custom serializeQueryArgs per endpoint', () => {
876
876
} ,
877
877
}
878
878
879
+ const TagTypes = {
880
+ ListItemsTag : 'ListItemsTag' ,
881
+ } as const ;
882
+
879
883
const api = createApi ( {
880
884
baseQuery : fetchBaseQuery ( { baseUrl : 'https://example.com' } ) ,
885
+ tagTypes : Object . values ( TagTypes ) ,
881
886
serializeQueryArgs : ( { endpointName, queryArgs } ) =>
882
887
`base-${ endpointName } -${ queryArgs } ` ,
883
888
endpoints : ( build ) => ( {
@@ -944,6 +949,16 @@ describe('custom serializeQueryArgs per endpoint', () => {
944
949
return currentArg !== previousArg
945
950
} ,
946
951
} ) ,
952
+ listItems3 : build . query < string [ ] , number > ( {
953
+ query : ( pageNumber ) => {
954
+ console . log ( pageNumber ) ;
955
+ return `/listItems3?page=${ pageNumber } `
956
+ } ,
957
+ serializeQueryArgs : ( { endpointName } ) => {
958
+ return endpointName
959
+ } ,
960
+ providesTags : [ TagTypes . ListItemsTag ] ,
961
+ } ) ,
947
962
} ) ,
948
963
} )
949
964
@@ -1083,4 +1098,35 @@ describe('custom serializeQueryArgs per endpoint', () => {
1083
1098
baseQueryMeta : expect . any ( Object ) ,
1084
1099
} )
1085
1100
} )
1101
+
1102
+ test ( 'serializeQueryArgs allows refetching as args change with same cache key if invalidated' , async ( ) => {
1103
+ const allItems = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'i' ]
1104
+ const PAGE_SIZE = 3
1105
+
1106
+ server . use (
1107
+ rest . get ( 'https://example.com/listItems3' , ( req , res , ctx ) => {
1108
+ const pageString = req . url . searchParams . get ( 'page' )
1109
+ const pageNum = parseInt ( pageString || '0' )
1110
+
1111
+ const results = paginate ( allItems , PAGE_SIZE , pageNum )
1112
+ return res ( ctx . json ( results ) )
1113
+ } )
1114
+ )
1115
+
1116
+ // Page number shouldn't matter here, because the cache key ignores that.
1117
+ // We just need to select the only cache entry.
1118
+ const selectListItems = api . endpoints . listItems3 . select ( 0 )
1119
+
1120
+ await storeRef . store . dispatch ( api . endpoints . listItems3 . initiate ( 1 ) )
1121
+
1122
+ const initialEntry = selectListItems ( storeRef . store . getState ( ) )
1123
+ expect ( initialEntry . data ) . toEqual ( [ 'a' , 'b' , 'c' ] )
1124
+
1125
+ await storeRef . store . dispatch ( api . util . invalidateTags ( [ TagTypes . ListItemsTag ] ) ) ;
1126
+
1127
+ // TODO: due to bug, the call to listItems3 still uses `1` instead of `2` as expected
1128
+ await storeRef . store . dispatch ( api . endpoints . listItems3 . initiate ( 2 ) )
1129
+ const updatedEntry = selectListItems ( storeRef . store . getState ( ) )
1130
+ expect ( updatedEntry . data ) . toEqual ( [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] )
1131
+ } )
1086
1132
} )
0 commit comments