diff --git a/packages/wrangler/src/__tests__/cfetch-utils.test.ts b/packages/wrangler/src/__tests__/cfetch-utils.test.ts new file mode 100644 index 000000000000..ffa2b6c277a2 --- /dev/null +++ b/packages/wrangler/src/__tests__/cfetch-utils.test.ts @@ -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); + }); +}); diff --git a/packages/wrangler/src/cfetch/index.ts b/packages/wrangler/src/cfetch/index.ts index 133dce63dba8..c148f212b04c 100644 --- a/packages/wrangler/src/cfetch/index.ts +++ b/packages/wrangler/src/cfetch/index.ts @@ -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;