From 790d61aaeee0792bd5856db695c620b197998793 Mon Sep 17 00:00:00 2001 From: Max Rozen <3822106+rozenmd@users.noreply.github.com> Date: Mon, 13 Nov 2023 13:00:46 +0100 Subject: [PATCH] chore(hasMorePages): add tests for hasMorePages --- .../src/__tests__/cfetch-utils.test.ts | 38 +++++++++++++++++++ packages/wrangler/src/cfetch/index.ts | 4 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 packages/wrangler/src/__tests__/cfetch-utils.test.ts 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;