@@ -822,6 +822,7 @@ describe("client", () => {
822
822
} ) ;
823
823
} ) ;
824
824
} ) ;
825
+ // >: {"tests": ["infiniteQuery"]}
825
826
describe ( "useInfiniteQuery" , ( ) => {
826
827
it ( "should fetch data correctly with pagination and include cursor" , async ( ) => {
827
828
const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
@@ -990,5 +991,92 @@ describe("client", () => {
990
991
const allItems = result . current . data ?. pages . flatMap ( ( page ) => page . items ) ;
991
992
expect ( allItems ) . toEqual ( [ 4 , 5 , 6 , 1 , 2 , 3 ] ) ;
992
993
} ) ;
994
+ it ( "should use custom cursor params" , async ( ) => {
995
+ const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
996
+ const client = createClient ( fetchClient ) ;
997
+
998
+ // First page request handler
999
+ const firstRequestHandler = useMockRequestHandler ( {
1000
+ baseUrl,
1001
+ method : "get" ,
1002
+ path : "/paginated-data" ,
1003
+ status : 200 ,
1004
+ body : { items : [ 1 , 2 , 3 ] , nextPage : 1 } ,
1005
+ } ) ;
1006
+
1007
+ const { result, rerender } = renderHook (
1008
+ ( ) =>
1009
+ client . useInfiniteQuery (
1010
+ "get" ,
1011
+ "/paginated-data" ,
1012
+ {
1013
+ params : {
1014
+ query : {
1015
+ limit : 3 ,
1016
+ } ,
1017
+ } ,
1018
+ } ,
1019
+ {
1020
+ getNextPageParam : ( lastPage ) => lastPage . nextPage ,
1021
+ initialPageParam : 0 ,
1022
+ pageParamName : "follow_cursor" ,
1023
+ } ,
1024
+ ) ,
1025
+ { wrapper } ,
1026
+ ) ;
1027
+
1028
+ // Wait for initial query to complete
1029
+ await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) ) ;
1030
+
1031
+ // Verify first request
1032
+ const firstRequestUrl = firstRequestHandler . getRequestUrl ( ) ;
1033
+ expect ( firstRequestUrl ?. searchParams . get ( "limit" ) ) . toBe ( "3" ) ;
1034
+ expect ( firstRequestUrl ?. searchParams . get ( "follow_cursor" ) ) . toBe ( "0" ) ;
1035
+
1036
+ // Set up mock for second page before triggering next page fetch
1037
+ const secondRequestHandler = useMockRequestHandler ( {
1038
+ baseUrl,
1039
+ method : "get" ,
1040
+ path : "/paginated-data" ,
1041
+ status : 200 ,
1042
+ body : { items : [ 4 , 5 , 6 ] , nextPage : 2 } ,
1043
+ } ) ;
1044
+
1045
+ // Fetch next page
1046
+ await act ( async ( ) => {
1047
+ await result . current . fetchNextPage ( ) ;
1048
+ // Force a rerender to ensure state is updated
1049
+ rerender ( ) ;
1050
+ } ) ;
1051
+
1052
+ // Wait for second page to be fetched and verify loading states
1053
+ await waitFor ( ( ) => {
1054
+ expect ( result . current . isFetching ) . toBe ( false ) ;
1055
+ expect ( result . current . hasNextPage ) . toBe ( true ) ;
1056
+ expect ( result . current . data ?. pages ) . toHaveLength ( 2 ) ;
1057
+ } ) ;
1058
+
1059
+ // Verify second request
1060
+ const secondRequestUrl = secondRequestHandler . getRequestUrl ( ) ;
1061
+ expect ( secondRequestUrl ?. searchParams . get ( "limit" ) ) . toBe ( "3" ) ;
1062
+ expect ( secondRequestUrl ?. searchParams . get ( "follow_cursor" ) ) . toBe ( "1" ) ;
1063
+
1064
+ expect ( result . current . data ) . toBeDefined ( ) ;
1065
+ expect ( result . current . data ?. pages [ 0 ] . nextPage ) . toBe ( 1 ) ;
1066
+
1067
+ expect ( result . current . data ) . toBeDefined ( ) ;
1068
+ expect ( result . current . data ?. pages [ 1 ] . nextPage ) . toBe ( 2 ) ;
1069
+
1070
+ // Verify the complete data structure
1071
+ expect ( result . current . data ?. pages ) . toEqual ( [
1072
+ { items : [ 1 , 2 , 3 ] , nextPage : 1 } ,
1073
+ { items : [ 4 , 5 , 6 ] , nextPage : 2 } ,
1074
+ ] ) ;
1075
+
1076
+ // Verify we can access all items through pages
1077
+ const allItems = result . current . data ?. pages . flatMap ( ( page ) => page . items ) ;
1078
+ expect ( allItems ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
1079
+ } ) ;
993
1080
} ) ;
1081
+ // <: {"tests": ["infiniteQuery"]}
994
1082
} ) ;
0 commit comments