Skip to content

Commit b921668

Browse files
committed
add unit tests
1 parent 76f115a commit b921668

File tree

3 files changed

+169
-5
lines changed

3 files changed

+169
-5
lines changed

packages/core/src/pagination/createPaginator.spec.ts

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ describe(createPaginator.name, () => {
3232
}
3333
}
3434

35+
class ClientStringToken {
36+
private pages = 5;
37+
async send(command: any) {
38+
if (--this.pages > 0) {
39+
return {
40+
outToken: command.input.inToken,
41+
};
42+
}
43+
return {};
44+
}
45+
}
3546
class CommandStringToken {
3647
public constructor(public input: any) {
3748
expect(input).toEqual({
@@ -79,6 +90,158 @@ describe(createPaginator.name, () => {
7990
expect(pages).toEqual(5);
8091
});
8192

93+
it("should prioritize token set in paginator config, fallback to token set in input parameters", async () => {
94+
class CommandExpectPaginatorConfigToken {
95+
public constructor(public input: any) {
96+
expect(input).toMatchObject({
97+
inToken: "abc",
98+
});
99+
}
100+
}
101+
class CommandExpectOperationInputToken {
102+
public constructor(public input: any) {
103+
expect(input).toMatchObject({
104+
inToken: "xyz",
105+
});
106+
}
107+
}
108+
{
109+
const paginate = createPaginator<
110+
PaginationConfiguration,
111+
{ inToken?: string; sizeToken?: number },
112+
{ outToken: string }
113+
>(ClientStringToken, CommandExpectPaginatorConfigToken, "inToken", "outToken", "sizeToken");
114+
115+
let pages = 0;
116+
const client = new ClientStringToken() as any;
117+
118+
for await (const page of paginate(
119+
{
120+
client,
121+
startingToken: "abc",
122+
},
123+
{
124+
inToken: "xyz",
125+
}
126+
)) {
127+
pages += 1;
128+
expect(page).toBeDefined();
129+
}
130+
131+
expect(pages).toEqual(5);
132+
}
133+
{
134+
const paginate = createPaginator<
135+
PaginationConfiguration,
136+
{ inToken?: string; sizeToken?: number },
137+
{ outToken: string }
138+
>(ClientStringToken, CommandExpectOperationInputToken, "inToken", "outToken", "sizeToken");
139+
140+
let pages = 0;
141+
const client = new ClientStringToken() as any;
142+
143+
for await (const page of paginate(
144+
{
145+
client,
146+
},
147+
{
148+
inToken: "xyz",
149+
}
150+
)) {
151+
pages += 1;
152+
expect(page).toBeDefined();
153+
}
154+
155+
expect(pages).toEqual(5);
156+
}
157+
});
158+
159+
it("should prioritize page size set in operation input, fallback to page size set in paginator config (inverted from token priority)", async () => {
160+
class CommandExpectPaginatorPageSize {
161+
public constructor(public input: any) {
162+
expect(input).toMatchObject({
163+
sizeToken: 100,
164+
});
165+
}
166+
}
167+
class CommandExpectOperationInputPageSize {
168+
public constructor(public input: any) {
169+
expect(input).toMatchObject({
170+
sizeToken: 99,
171+
});
172+
}
173+
}
174+
{
175+
const paginate = createPaginator<
176+
PaginationConfiguration,
177+
{ inToken?: string; sizeToken?: number },
178+
{ outToken: string }
179+
>(ClientStringToken, CommandExpectPaginatorPageSize, "inToken", "outToken", "sizeToken");
180+
181+
let pages = 0;
182+
const client = new ClientStringToken() as any;
183+
184+
for await (const page of paginate(
185+
{
186+
client,
187+
pageSize: 100,
188+
},
189+
{
190+
inToken: "abc",
191+
}
192+
)) {
193+
pages += 1;
194+
expect(page).toBeDefined();
195+
}
196+
197+
expect(pages).toEqual(5);
198+
}
199+
{
200+
const paginate = createPaginator<
201+
PaginationConfiguration,
202+
{ inToken?: string; sizeToken?: number },
203+
{ outToken: string }
204+
>(ClientStringToken, CommandExpectOperationInputPageSize, "inToken", "outToken", "sizeToken");
205+
206+
let pages = 0;
207+
const client = new ClientStringToken() as any;
208+
209+
for await (const page of paginate(
210+
{
211+
client,
212+
pageSize: 100,
213+
},
214+
{
215+
sizeToken: 99,
216+
inToken: "abc",
217+
}
218+
)) {
219+
pages += 1;
220+
expect(page).toBeDefined();
221+
}
222+
223+
expect(pages).toEqual(5);
224+
}
225+
});
226+
227+
it("should have the correct AsyncGenerator.TNext type", async () => {
228+
const paginate = createPaginator<
229+
PaginationConfiguration,
230+
{ inToken?: string; sizeToken: number },
231+
{
232+
outToken: string;
233+
}
234+
>(ClientStringToken, CommandStringToken, "inToken", "outToken.outToken2.outToken3", "sizeToken");
235+
const asyncGenerator = paginate(
236+
{ client: new ClientStringToken() as any },
237+
{ inToken: "TOKEN_VALUE", sizeToken: 100 }
238+
);
239+
240+
const { value, done } = await asyncGenerator.next();
241+
expect(value?.outToken).toBeTypeOf("string");
242+
expect(done).toBe(false);
243+
});
244+
82245
it("should handle deep paths", async () => {
83246
const paginate = createPaginator<
84247
PaginationConfiguration,

packages/core/src/pagination/createPaginator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ export function createPaginator<
3333
input: InputType,
3434
...additionalArguments: any[]
3535
): Paginator<OutputType> {
36-
let token: any = config.startingToken || (input as any)[inputTokenName];
36+
const _input = input as any;
37+
// for legacy reasons this coalescing order is inverted from that of pageSize.
38+
let token: any = config.startingToken ?? _input[inputTokenName];
3739
let hasNext = true;
3840
let page: OutputType;
3941

4042
while (hasNext) {
41-
(input as any)[inputTokenName] = token;
43+
_input[inputTokenName] = token;
4244
if (pageSizeTokenName) {
43-
(input as any)[pageSizeTokenName] = (input as any)[pageSizeTokenName] ?? config.pageSize;
45+
_input[pageSizeTokenName] = _input[pageSizeTokenName] ?? config.pageSize;
4446
}
4547
if (config.client instanceof ClientCtor) {
4648
page = await makePagedClientRequest(CommandCtor, config.client, input, ...additionalArguments);
@@ -52,7 +54,6 @@ export function createPaginator<
5254
token = get(page, outputTokenName);
5355
hasNext = !!(token && (!config.stopOnSameToken || token !== prevToken));
5456
}
55-
// @ts-ignore
5657
return undefined;
5758
};
5859
}

packages/types/src/pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Client } from "./client";
55
*
66
* Expected type definition of a paginator.
77
*/
8-
export type Paginator<T> = AsyncGenerator<T, undefined, unknown>;
8+
export type Paginator<T> = AsyncGenerator<T, undefined, Promise<T | undefined>>;
99

1010
/**
1111
* @public

0 commit comments

Comments
 (0)