@@ -17,24 +17,22 @@ function createMockHttpClient(responses: unknown[]): HttpClient {
1717}
1818
1919describe ( "pagination" , ( ) => {
20- it ( "yields pages of items using offset-based pagination" , async ( ) => {
20+ it ( "yields individual items using offset-based pagination" , async ( ) => {
2121 const httpClient = createMockHttpClient ( [
2222 { data : [ { id : 1 } , { id : 2 } ] } ,
2323 { data : [ { id : 3 } ] } ,
2424 ] ) ;
2525
26- const pages : unknown [ ] [ ] = [ ] ;
27- for await ( const page of paginate ( {
26+ const items : unknown [ ] = [ ] ;
27+ for await ( const item of paginate ( {
2828 httpClient,
2929 path : "/ssot/test" ,
3030 batchSize : 2 ,
3131 } ) ) {
32- pages . push ( page ) ;
32+ items . push ( item ) ;
3333 }
3434
35- expect ( pages ) . toHaveLength ( 2 ) ;
36- expect ( pages [ 0 ] ) . toEqual ( [ { id : 1 } , { id : 2 } ] ) ;
37- expect ( pages [ 1 ] ) . toEqual ( [ { id : 3 } ] ) ;
35+ expect ( items ) . toEqual ( [ { id : 1 } , { id : 2 } , { id : 3 } ] ) ;
3836 } ) ;
3937
4038 it ( "stops when empty page is returned" , async ( ) => {
@@ -43,16 +41,16 @@ describe("pagination", () => {
4341 { data : [ ] } ,
4442 ] ) ;
4543
46- const pages : unknown [ ] [ ] = [ ] ;
47- for await ( const page of paginate ( {
44+ const items : unknown [ ] = [ ] ;
45+ for await ( const item of paginate ( {
4846 httpClient,
4947 path : "/ssot/test" ,
5048 batchSize : 10 ,
5149 } ) ) {
52- pages . push ( page ) ;
50+ items . push ( item ) ;
5351 }
5452
55- expect ( pages ) . toHaveLength ( 1 ) ;
53+ expect ( items ) . toEqual ( [ { id : 1 } ] ) ;
5654 } ) ;
5755
5856 it ( "collectAll gathers all items" , async ( ) => {
@@ -120,14 +118,14 @@ describe("pagination", () => {
120118 { data : [ { id : 1 } ] } ,
121119 ] ) ;
122120
123- const pages : unknown [ ] [ ] = [ ] ;
124- for await ( const page of paginate ( {
121+ const items : unknown [ ] = [ ] ;
122+ for await ( const item of paginate ( {
125123 httpClient,
126124 path : "/ssot/test" ,
127125 batchSize : 5 ,
128126 pageSizeParam : "limit" ,
129127 } ) ) {
130- pages . push ( page ) ;
128+ items . push ( item ) ;
131129 }
132130
133131 expect ( httpClient . get ) . toHaveBeenCalledWith ( "/ssot/test" , {
@@ -140,13 +138,13 @@ describe("pagination", () => {
140138 { data : [ { id : 1 } ] } ,
141139 ] ) ;
142140
143- const pages : unknown [ ] [ ] = [ ] ;
144- for await ( const page of paginate ( {
141+ const items : unknown [ ] = [ ] ;
142+ for await ( const item of paginate ( {
145143 httpClient,
146144 path : "/ssot/test" ,
147145 batchSize : 5 ,
148146 } ) ) {
149- pages . push ( page ) ;
147+ items . push ( item ) ;
150148 }
151149
152150 expect ( httpClient . get ) . toHaveBeenCalledWith ( "/ssot/test" , {
@@ -169,4 +167,55 @@ describe("pagination", () => {
169167
170168 expect ( all ) . toEqual ( [ { name : "a" } ] ) ;
171169 } ) ;
170+
171+ it ( "strips API base prefix from absolute nextPageUrl" , async ( ) => {
172+ const httpClient = createMockHttpClient ( [
173+ {
174+ data : [ { id : 1 } ] ,
175+ nextPageUrl : "https://instance.my.salesforce.com/services/data/v66.0/ssot/test?offset=1&batchSize=1" ,
176+ } ,
177+ { data : [ { id : 2 } ] } ,
178+ ] ) ;
179+
180+ const all = await collectAll ( {
181+ httpClient,
182+ path : "/ssot/test" ,
183+ batchSize : 1 ,
184+ } ) ;
185+
186+ expect ( all ) . toEqual ( [ { id : 1 } , { id : 2 } ] ) ;
187+ expect ( httpClient . get ) . toHaveBeenNthCalledWith ( 2 , "/ssot/test?offset=1&batchSize=1" , {
188+ query : undefined ,
189+ } ) ;
190+ } ) ;
191+
192+ it ( "uses totalSize to avoid extra offset-based request" , async ( ) => {
193+ const httpClient = createMockHttpClient ( [
194+ { data : [ { id : 1 } , { id : 2 } ] , totalSize : 2 } ,
195+ ] ) ;
196+
197+ const all = await collectAll ( {
198+ httpClient,
199+ path : "/ssot/test" ,
200+ batchSize : 2 ,
201+ } ) ;
202+
203+ expect ( all ) . toEqual ( [ { id : 1 } , { id : 2 } ] ) ;
204+ expect ( httpClient . get ) . toHaveBeenCalledTimes ( 1 ) ;
205+ } ) ;
206+
207+ it ( "uses totalSize guard even when nextPageUrl is present" , async ( ) => {
208+ const httpClient = createMockHttpClient ( [
209+ { data : [ { id : 1 } ] , totalSize : 1 , nextPageUrl : "/ssot/test?offset=1&batchSize=1" } ,
210+ ] ) ;
211+
212+ const all = await collectAll ( {
213+ httpClient,
214+ path : "/ssot/test" ,
215+ batchSize : 1 ,
216+ } ) ;
217+
218+ expect ( all ) . toEqual ( [ { id : 1 } ] ) ;
219+ expect ( httpClient . get ) . toHaveBeenCalledTimes ( 1 ) ;
220+ } ) ;
172221} ) ;
0 commit comments