@@ -98,6 +98,90 @@ describe("Replicate client", () => {
9898 } ) ;
9999 } ) ;
100100
101+ describe ( "paginate" , ( ) => {
102+ test ( "pages through results" , async ( ) => {
103+ nock ( BASE_URL )
104+ . get ( "/collections" )
105+ . reply ( 200 , {
106+ results : [
107+ {
108+ name : "Super resolution" ,
109+ slug : "super-resolution" ,
110+ description :
111+ "Upscaling models that create high-quality images from low-quality images." ,
112+ } ,
113+ ] ,
114+ next : `${ BASE_URL } /collections?page=2` ,
115+ previous : null ,
116+ } ) ;
117+ nock ( BASE_URL )
118+ . get ( "/collections?page=2" )
119+ . reply ( 200 , {
120+ results : [
121+ {
122+ name : "Image classification" ,
123+ slug : "image-classification" ,
124+ description : "Models that classify images." ,
125+ } ,
126+ ] ,
127+ next : null ,
128+ previous : null ,
129+ } ) ;
130+
131+ const iterator = client . paginate ( client . collections . list ) ;
132+
133+ const firstPage = ( await iterator . next ( ) ) . value ;
134+ expect ( firstPage . length ) . toBe ( 1 ) ;
135+
136+ const secondPage = ( await iterator . next ( ) ) . value ;
137+ expect ( secondPage . length ) . toBe ( 1 ) ;
138+ } ) ;
139+
140+ test ( "accepts an abort signal" , async ( ) => {
141+ nock ( BASE_URL )
142+ . get ( "/collections" )
143+ . reply ( 200 , {
144+ results : [
145+ {
146+ name : "Super resolution" ,
147+ slug : "super-resolution" ,
148+ description :
149+ "Upscaling models that create high-quality images from low-quality images." ,
150+ } ,
151+ ] ,
152+ next : `${ BASE_URL } /collections?page=2` ,
153+ previous : null ,
154+ } ) ;
155+ nock ( BASE_URL )
156+ . get ( "/collections?page=2" )
157+ . reply ( 200 , {
158+ results : [
159+ {
160+ name : "Image classification" ,
161+ slug : "image-classification" ,
162+ description : "Models that classify images." ,
163+ } ,
164+ ] ,
165+ next : null ,
166+ previous : null ,
167+ } ) ;
168+
169+ const controller = new AbortController ( ) ;
170+ const iterator = client . paginate ( client . collections . list , {
171+ signal : controller . signal ,
172+ } ) ;
173+
174+ const firstIteration = await iterator . next ( ) ;
175+ expect ( firstIteration . value . length ) . toBe ( 1 ) ;
176+
177+ controller . abort ( ) ;
178+
179+ const secondIteration = await iterator . next ( ) ;
180+ expect ( secondIteration . value ) . toBeUndefined ( ) ;
181+ expect ( secondIteration . done ) . toBe ( true ) ;
182+ } ) ;
183+ } ) ;
184+
101185 describe ( "account.get" , ( ) => {
102186 test ( "Calls the correct API route" , async ( ) => {
103187 nock ( BASE_URL ) . get ( "/account" ) . reply ( 200 , {
0 commit comments