11import { HttpMockImpl } from "../../__mocks__/index.js" ;
2- import { type HttpRequestParams } from "../../interfaces/index.js" ;
2+ import {
3+ type HttpRequestParams ,
4+ type HttpResponse ,
5+ } from "../../interfaces/index.js" ;
36import { PaginatorHttp } from "./paginator-http.js" ;
47
58describe ( "PaginatorHttp" , ( ) => {
69 const http = new HttpMockImpl ( ) ;
710
811 beforeEach ( ( ) => {
9- http . request . mockReturnValue ( { headers : new Headers ( { } ) } ) ;
12+ http . request . mockResolvedValue ( {
13+ data : { } ,
14+ headers : new Headers ( ) ,
15+ } ) ;
1016 } ) ;
1117
1218 afterEach ( ( ) => {
1319 http . clear ( ) ;
1420 } ) ;
1521
1622 it ( "sends a request" , async ( ) => {
17- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" , {
23+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" , {
1824 foo : "bar" ,
1925 } ) . values ( ) ;
2026 await paginator . next ( ) ;
@@ -26,7 +32,7 @@ describe("PaginatorHttp", () => {
2632 } ) ;
2733
2834 it ( "sends a request with await" , async ( ) => {
29- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" , {
35+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" , {
3036 foo : "bar" ,
3137 } ) ;
3238 await paginator ;
@@ -38,12 +44,16 @@ describe("PaginatorHttp", () => {
3844 } ) ;
3945
4046 it ( "parses the next url" , async ( ) => {
41- http . request . mockReturnValue ( {
47+ http . request . mockResolvedValue ( {
4248 headers : new Headers ( {
4349 link : '<https://mastodon.social/api/v1/timelines/home?max_id=109382006402042919>; rel="next", <https://mastodon.social/api/v1/timelines/home?min_id=109382039876197520>; rel="prev"' ,
4450 } ) ,
4551 } ) ;
46- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) . values ( ) ;
52+ const paginator = new PaginatorHttp (
53+ http ,
54+ false ,
55+ "/v1/api/timelines" ,
56+ ) . values ( ) ;
4757 await paginator . next ( ) ;
4858 await paginator . next ( ) ;
4959 expect ( http . request ) . toBeCalledWith ( {
@@ -54,14 +64,14 @@ describe("PaginatorHttp", () => {
5464 } ) ;
5565
5666 it ( "paginates with opposite direction when prev direction was set" , async ( ) => {
57- http . request . mockReturnValue ( {
67+ http . request . mockResolvedValue ( {
5868 headers : new Headers ( {
5969 link : '<https://mastodon.social/api/v1/timelines/home?max_id=109382006402042919>; rel="next", <https://mastodon.social/api/v1/timelines/home?min_id=109382039876197520>; rel="prev"' ,
6070 } ) ,
6171 data : [ ] ,
6272 } ) ;
6373
64- let paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) ;
74+ let paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" ) ;
6575 expect ( paginator . getDirection ( ) ) . toBe ( "next" ) ;
6676
6777 paginator = paginator . setDirection ( "prev" ) ;
@@ -78,12 +88,16 @@ describe("PaginatorHttp", () => {
7888 } ) ;
7989
8090 it ( "parses the next url regardless of order" , async ( ) => {
81- http . request . mockReturnValue ( {
91+ http . request . mockResolvedValue ( {
8292 headers : new Headers ( {
8393 link : '<https://mastodon.social/api/v1/timelines/home?min_id=109382039876197520>; rel="prev", <https://mastodon.social/api/v1/timelines/home?max_id=109382006402042919>; rel="next"' ,
8494 } ) ,
8595 } ) ;
86- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) . values ( ) ;
96+ const paginator = new PaginatorHttp (
97+ http ,
98+ false ,
99+ "/v1/api/timelines" ,
100+ ) . values ( ) ;
87101 await paginator . next ( ) ;
88102 await paginator . next ( ) ;
89103 expect ( http . request ) . toBeCalledWith ( {
@@ -94,7 +108,11 @@ describe("PaginatorHttp", () => {
94108 } ) ;
95109
96110 it ( "returns done when next link does not exist" , async ( ) => {
97- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) . values ( ) ;
111+ const paginator = new PaginatorHttp (
112+ http ,
113+ false ,
114+ "/v1/api/timelines" ,
115+ ) . values ( ) ;
98116 await paginator . next ( ) ;
99117 const result = await paginator . next ( ) ;
100118 expect ( result ) . toEqual ( {
@@ -103,7 +121,7 @@ describe("PaginatorHttp", () => {
103121 } ) ;
104122
105123 it ( "is AsyncIterable" , async ( ) => {
106- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" , {
124+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" , {
107125 foo : "bar" ,
108126 } ) ;
109127
@@ -119,7 +137,7 @@ describe("PaginatorHttp", () => {
119137 } ) ;
120138
121139 it ( "returns AsyncIterable from values" , async ( ) => {
122- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" , {
140+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" , {
123141 foo : "bar" ,
124142 } ) ;
125143
@@ -137,12 +155,12 @@ describe("PaginatorHttp", () => {
137155 } ) ;
138156
139157 it ( "parse array in url query string correctly" , async ( ) => {
140- http . request . mockReturnValue ( {
158+ http . request . mockResolvedValue ( {
141159 headers : new Headers ( {
142160 link : '<https://mastodon.social/api/v1/notifications?types[]=mention&max_id=123456>; rel="next", <https://mastodon.social/api/v1/notifications?types[]=mention>; rel="prev"' ,
143161 } ) ,
144162 } ) ;
145- const paginator = new PaginatorHttp ( http , "/v1/api/notifications" , {
163+ const paginator = new PaginatorHttp ( http , false , "/v1/api/notifications" , {
146164 types : [ "mention" ] ,
147165 } ) . values ( ) ;
148166 await paginator . next ( ) ;
@@ -155,19 +173,19 @@ describe("PaginatorHttp", () => {
155173 } ) ;
156174
157175 it ( "is thenable" , ( ) => {
158- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) ;
176+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" ) ;
159177 const onFulfilled = vi . fn ( ) ;
160178 paginator . then ( onFulfilled ) ;
161179 expect ( onFulfilled ) . toBeCalledTimes ( 0 ) ;
162180 } ) ;
163181
164182 it ( "can be converted to promise" , async ( ) => {
165- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) ;
183+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" ) ;
166184 expect ( paginator . then ( ) ) . toBeInstanceOf ( Promise ) ;
167185 } ) ;
168186
169187 it ( "is thenable (error)" , ( ) => {
170- const paginator = new PaginatorHttp ( http , "/v1/api/timelines" ) ;
188+ const paginator = new PaginatorHttp ( http , false , "/v1/api/timelines" ) ;
171189 http . request . mockImplementation ( ( ) => {
172190 throw new Error ( "mock error" ) ;
173191 } ) ;
@@ -178,4 +196,14 @@ describe("PaginatorHttp", () => {
178196 expect ( onFulfilled ) . not . toBeCalled ( ) ;
179197 expect ( onRejected ) . toBeCalledTimes ( 0 ) ;
180198 } ) ;
199+
200+ it ( "respects the raw parameter" , async ( ) => {
201+ const paginator = new PaginatorHttp < HttpResponse < unknown > > (
202+ http ,
203+ true ,
204+ "/v1/api/timelines" ,
205+ ) ;
206+ const result = await paginator ;
207+ expect ( result . headers ) . toBeInstanceOf ( Headers ) ;
208+ } ) ;
181209} ) ;
0 commit comments