Skip to content

Commit

Permalink
chore(hasMorePages): add tests for hasMorePages
Browse files Browse the repository at this point in the history
  • Loading branch information
rozenmd committed Nov 13, 2023
1 parent c43c087 commit 790d61a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/wrangler/src/__tests__/cfetch-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { hasMorePages } from "../cfetch";

/**
hasMorePages is a function that returns a boolean based on the result_info object returned from the cloudflare v4 API - if the current page is less than the total number of pages, it returns true, otherwise false.
*/

describe("hasMorePages", () => {
it("should handle result_info not having enough results to paginate", () => {
expect(
hasMorePages({
page: 1,
per_page: 10,
count: 5,
total_count: 5,
})
).toBe(false);
});
it("should return true if the current page is less than the total number of pages", () => {
expect(
hasMorePages({
page: 1,
per_page: 10,
count: 10,
total_count: 100,
})
).toBe(true);
});
it("should return false if we are on the last page of results", () => {
expect(
hasMorePages({
page: 10,
per_page: 10,
count: 10,
total_count: 100,
})
).toBe(false);
});
});
4 changes: 3 additions & 1 deletion packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ interface PageResultInfo {
total_count: number;
}

function hasMorePages(result_info: unknown): result_info is PageResultInfo {
export function hasMorePages(
result_info: unknown
): result_info is PageResultInfo {
const page = (result_info as PageResultInfo | undefined)?.page;
const per_page = (result_info as PageResultInfo | undefined)?.per_page;
const total = (result_info as PageResultInfo | undefined)?.total_count;
Expand Down

0 comments on commit 790d61a

Please sign in to comment.