@@ -3,7 +3,7 @@ import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.j
3
3
import type { paths } from "./fixtures/api.js" ;
4
4
import createClient from "../src/index.js" ;
5
5
import createFetchClient from "openapi-fetch" ;
6
- import { act , fireEvent , render , renderHook , screen , waitFor } from "@testing-library/react" ;
6
+ import { fireEvent , render , renderHook , screen , waitFor , act } from "@testing-library/react" ;
7
7
import {
8
8
QueryClient ,
9
9
QueryClientProvider ,
@@ -823,64 +823,91 @@ describe("client", () => {
823
823
} ) ;
824
824
} ) ;
825
825
describe ( "useInfiniteQuery" , ( ) => {
826
- it ( "should fetch data correctly with pagination" , async ( ) => {
826
+ it ( "should fetch data correctly with pagination and include cursor " , async ( ) => {
827
827
const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
828
828
const client = createClient ( fetchClient ) ;
829
829
830
- useMockRequestHandler ( {
830
+ // Track request URLs using the mock handler
831
+ let firstRequestUrl : URL | undefined ;
832
+ let secondRequestUrl : URL | undefined ;
833
+
834
+ // First page request handler
835
+ const firstRequestHandler = useMockRequestHandler ( {
831
836
baseUrl,
832
837
method : "get" ,
833
838
path : "/paginated-data" ,
834
839
status : 200 ,
835
840
body : { items : [ 1 , 2 , 3 ] , nextPage : 1 } ,
836
841
} ) ;
837
842
838
- const { result } = renderHook (
839
- ( ) => client . useInfiniteQuery ( "get" , "/paginated-data" , { params : { query : { limit : 3 } } } ) ,
843
+ const { result, rerender } = renderHook (
844
+ ( ) => client . useInfiniteQuery ( "get" , "/paginated-data" ,
845
+ {
846
+ params : {
847
+ query : {
848
+ limit : 3
849
+ }
850
+ }
851
+ } ,
852
+ {
853
+ getNextPageParam : ( lastPage ) => lastPage . nextPage ,
854
+ initialPageParam : 0 ,
855
+ } ) ,
840
856
{ wrapper } ,
841
857
) ;
842
858
859
+ // Wait for initial query to complete
843
860
await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) ) ;
844
861
845
- expect ( ( result . current . data as any ) . pages [ 0 ] ) . toEqual ( { items : [ 1 , 2 , 3 ] , nextPage : 1 } ) ;
862
+ // Verify first request
863
+ firstRequestUrl = firstRequestHandler . getRequestUrl ( ) ;
864
+ expect ( firstRequestUrl ?. searchParams . get ( 'limit' ) ) . toBe ( '3' ) ;
865
+ expect ( firstRequestUrl ?. searchParams . get ( 'cursor' ) ) . toBe ( '0' ) ;
846
866
847
- // Set up mock for second page
848
- useMockRequestHandler ( {
867
+ // Set up mock for second page before triggering next page fetch
868
+ const secondRequestHandler = useMockRequestHandler ( {
849
869
baseUrl,
850
870
method : "get" ,
851
871
path : "/paginated-data" ,
852
872
status : 200 ,
853
873
body : { items : [ 4 , 5 , 6 ] , nextPage : 2 } ,
854
874
} ) ;
855
875
856
- await result . current . fetchNextPage ( ) ;
876
+ // Fetch next page
877
+ await act ( async ( ) => {
878
+ await result . current . fetchNextPage ( ) ;
879
+ // Force a rerender to ensure state is updated
880
+ rerender ( ) ;
881
+ } ) ;
857
882
858
- await waitFor ( ( ) => expect ( result . current . isFetching ) . toBe ( false ) ) ;
883
+ // Wait for second page to be fetched and verify loading states
884
+ await waitFor ( ( ) => {
885
+ expect ( result . current . isFetching ) . toBe ( false ) ;
886
+ expect ( result . current . hasNextPage ) . toBe ( true ) ;
887
+ expect ( result . current . data ?. pages ) . toHaveLength ( 2 ) ;
888
+ } ) ;
859
889
860
- expect ( ( result . current . data as any ) . pages ) . toHaveLength ( 2 ) ;
861
- expect ( ( result . current . data as any ) . pages [ 1 ] ) . toEqual ( { items : [ 4 , 5 , 6 ] , nextPage : 2 } ) ;
862
- } ) ;
890
+ // Verify second request
891
+ secondRequestUrl = secondRequestHandler . getRequestUrl ( ) ;
892
+ expect ( secondRequestUrl ?. searchParams . get ( 'limit' ) ) . toBe ( '3' ) ;
893
+ expect ( secondRequestUrl ?. searchParams . get ( 'cursor' ) ) . toBe ( '1' ) ;
863
894
864
- it ( "should handle errors correctly" , async ( ) => {
865
- const fetchClient = createFetchClient < paths > ( { baseUrl } ) ;
866
- const client = createClient ( fetchClient ) ;
895
+ expect ( result . current . data ) . toBeDefined ( ) ;
896
+ expect ( result . current . data ! . pages [ 0 ] . nextPage ) . toBe ( 1 ) ;
867
897
868
- useMockRequestHandler ( {
869
- baseUrl,
870
- method : "get" ,
871
- path : "/paginated-data" ,
872
- status : 500 ,
873
- body : { code : 500 , message : "Internal Server Error" } ,
874
- } ) ;
875
898
876
- const { result } = renderHook (
877
- ( ) => client . useInfiniteQuery ( "get" , "/paginated-data" , { params : { query : { limit : 3 } } } ) ,
878
- { wrapper } ,
879
- ) ;
899
+ expect ( result . current . data ) . toBeDefined ( ) ;
900
+ expect ( result . current . data ! . pages [ 1 ] . nextPage ) . toBe ( 2 ) ;
880
901
881
- await waitFor ( ( ) => expect ( result . current . isError ) . toBe ( true ) ) ;
902
+ // Verify the complete data structure
903
+ expect ( result . current . data ?. pages ) . toEqual ( [
904
+ { items : [ 1 , 2 , 3 ] , nextPage : 1 } ,
905
+ { items : [ 4 , 5 , 6 ] , nextPage : 2 }
906
+ ] ) ;
882
907
883
- expect ( result . current . error ) . toEqual ( { code : 500 , message : "Internal Server Error" } ) ;
908
+ // Verify we can access all items through pages
909
+ const allItems = result . current . data ?. pages . flatMap ( page => page . items ) ;
910
+ expect ( allItems ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
884
911
} ) ;
885
- } ) ;
912
+ } )
886
913
} ) ;
0 commit comments