@@ -909,5 +909,87 @@ describe("client", () => {
909
909
const allItems = result . current . data ?. pages . flatMap ( page => page . items ) ;
910
910
expect ( allItems ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
911
911
} ) ;
912
+ it ( "should reverse pages and pageParams when using the select option" , async ( ) => {
913
+ const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
914
+ const client = createClient ( fetchClient ) ;
915
+
916
+ // First page request handler
917
+ const firstRequestHandler = useMockRequestHandler ( {
918
+ baseUrl,
919
+ method : "get" ,
920
+ path : "/paginated-data" ,
921
+ status : 200 ,
922
+ body : { items : [ 1 , 2 , 3 ] , nextPage : 1 } ,
923
+ } ) ;
924
+
925
+ const { result, rerender } = renderHook (
926
+ ( ) =>
927
+ client . useInfiniteQuery (
928
+ "get" ,
929
+ "/paginated-data" ,
930
+ {
931
+ params : {
932
+ query : {
933
+ limit : 3 ,
934
+ } ,
935
+ } ,
936
+ } ,
937
+ {
938
+ getNextPageParam : ( lastPage ) => lastPage . nextPage ,
939
+ initialPageParam : 0 ,
940
+ select : ( data ) => ( {
941
+ pages : [ ...data . pages ] . reverse ( ) ,
942
+ pageParams : [ ...data . pageParams ] . reverse ( ) ,
943
+ } ) ,
944
+ }
945
+ ) ,
946
+ { wrapper }
947
+ ) ;
948
+
949
+ // Wait for initial query to complete
950
+ await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) ) ;
951
+
952
+ // Verify first request
953
+ const firstRequestUrl = firstRequestHandler . getRequestUrl ( ) ;
954
+ expect ( firstRequestUrl ?. searchParams . get ( "limit" ) ) . toBe ( "3" ) ;
955
+ expect ( firstRequestUrl ?. searchParams . get ( "cursor" ) ) . toBe ( "0" ) ;
956
+
957
+ // Set up mock for second page before triggering next page fetch
958
+ const secondRequestHandler = useMockRequestHandler ( {
959
+ baseUrl,
960
+ method : "get" ,
961
+ path : "/paginated-data" ,
962
+ status : 200 ,
963
+ body : { items : [ 4 , 5 , 6 ] , nextPage : 2 } ,
964
+ } ) ;
965
+
966
+ // Fetch next page
967
+ await act ( async ( ) => {
968
+ await result . current . fetchNextPage ( ) ;
969
+ rerender ( ) ;
970
+ } ) ;
971
+
972
+ // Wait for second page to complete
973
+ await waitFor ( ( ) => {
974
+ expect ( result . current . isFetching ) . toBe ( false ) ;
975
+ expect ( result . current . hasNextPage ) . toBe ( true ) ;
976
+ } ) ;
977
+
978
+ // Verify reversed pages and pageParams
979
+ expect ( result . current . data ) . toBeDefined ( ) ;
980
+
981
+ // Since pages are reversed, the second page will now come first
982
+ expect ( result . current . data ! . pages ) . toEqual ( [
983
+ { items : [ 4 , 5 , 6 ] , nextPage : 2 } ,
984
+ { items : [ 1 , 2 , 3 ] , nextPage : 1 } ,
985
+ ] ) ;
986
+
987
+ // Verify reversed pageParams
988
+ expect ( result . current . data ! . pageParams ) . toEqual ( [ 1 , 0 ] ) ;
989
+
990
+ // Verify all items from reversed pages
991
+ const allItems = result . current . data ! . pages . flatMap ( ( page ) => page . items ) ;
992
+ expect ( allItems ) . toEqual ( [ 4 , 5 , 6 , 1 , 2 , 3 ] ) ;
993
+ } ) ;
912
994
} )
913
995
} ) ;
0 commit comments