From 923030340e04b2bbf9d68d3167a52153220149c5 Mon Sep 17 00:00:00 2001 From: Nathan Arthur Date: Mon, 6 May 2024 11:35:38 -0400 Subject: [PATCH] fix getAll bugs --- src/factory.spec.ts | 46 ++++++++++++++++++++++++++++++++------------- src/factory.ts | 14 +++++++------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/factory.spec.ts b/src/factory.spec.ts index f52ee95..13b0565 100644 --- a/src/factory.spec.ts +++ b/src/factory.spec.ts @@ -7,23 +7,43 @@ const factory = new Factory(); describe("Factory", () => { beforeEach(() => { - vi.mocked(client.get) - .mockResolvedValue({ - data: { - results: [{}], - next: null, - }, - }) - .mockResolvedValueOnce({ - data: { - results: [{}], - next: "the_next_url", - }, - }); + vi.mocked(client.get).mockResolvedValue({ + data: { + results: [{}], + next: null, + }, + }); }); it("gets all", async () => { + vi.mocked(client.get).mockResolvedValueOnce({ + data: { + results: [{}], + next: "the_next_url", + }, + }); await factory.getMany(1, Row); expect(client.get).toBeCalledTimes(2); }); + + it("treats page as one indexed", async () => { + await factory.getMany(1, Row); + expect(client.get).not.toBeCalledWith( + expect.anything(), + expect.objectContaining({ params: expect.objectContaining({ page: 0 }) }), + ); + }); + + it("includes page with all requests", async () => { + await factory.getMany(1, Row); + expect(client.get).toBeCalledWith( + expect.anything(), + expect.objectContaining({ params: expect.objectContaining({ page: 1 }) }), + ); + }); + + it("returns results", async () => { + const results = await factory.getMany(1, Row); + expect(results).toHaveLength(1); + }); }); diff --git a/src/factory.ts b/src/factory.ts index 6b41923..a19bd3c 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -34,16 +34,16 @@ export class Factory { options: Record & { page?: number } = {}, accumulator: R[] = [], ): Promise { - const { results, next } = await this.sdk.listRows(tableId, options); + const { page = 1 } = options; + const { results, next } = await this.sdk.listRows(tableId, { + ...options, + page, + }); + accumulator.push(...results); if (!next) { return accumulator; } - accumulator.push(...results); - return this.getAll( - tableId, - { ...options, page: (options.page ?? 0) + 1 }, - accumulator, - ); + return this.getAll(tableId, { ...options, page: page + 1 }, accumulator); } private createRows(