Skip to content

Commit

Permalink
fix: add test for cursor pagination
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pilar <[email protected]>
  • Loading branch information
pilartomas committed Jan 6, 2025
1 parent 676cac6 commit d54eb22
Showing 1 changed file with 56 additions and 17 deletions.
73 changes: 56 additions & 17 deletions src/internals/helpers/paginate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
* limitations under the License.
*/

import { paginate, PaginateInput } from "@/internals/helpers/paginate.js";
import {
paginate,
PaginateInput,
paginateWithCursor,
PaginateWithCursorInput,
} from "@/internals/helpers/paginate.js";

describe("paginate", () => {
it.each([
const mockSetup = [
{
size: 1,
chunkSize: 1,
Expand All @@ -38,23 +43,57 @@ describe("paginate", () => {
chunkSize: 1,
items: Array(20).fill(1),
},
])("Works %#", async ({ size, items, chunkSize }) => {
const fn: PaginateInput<number>["handler"] = vi.fn().mockImplementation(async ({ offset }) => {
const chunk = items.slice(offset, offset + chunkSize);
return { done: offset + chunk.length >= items.length, data: chunk };
});
] as const;

describe("paginate", () => {
it.each(mockSetup)("Works %#", async ({ size, items, chunkSize }) => {
const fn: PaginateInput<number>["handler"] = vi
.fn()
.mockImplementation(async ({ offset }) => {
const chunk = items.slice(offset, offset + chunkSize);
return { done: offset + chunk.length >= items.length, data: chunk };
});

const results = await paginate({
size,
handler: fn,
const results = await paginate({
size,
handler: fn,
});

const maxItemsToBeRetrieved = Math.min(size, items.length);
let expectedCalls = Math.ceil(maxItemsToBeRetrieved / chunkSize);
if (expectedCalls === 0 && size > 0) {
expectedCalls = 1;
}
expect(fn).toBeCalledTimes(expectedCalls);
expect(results).toHaveLength(maxItemsToBeRetrieved);
});
});

const maxItemsToBeRetrieved = Math.min(size, items.length);
let expectedCalls = Math.ceil(maxItemsToBeRetrieved / chunkSize);
if (expectedCalls === 0 && size > 0) {
expectedCalls = 1;
}
expect(fn).toBeCalledTimes(expectedCalls);
expect(results).toHaveLength(maxItemsToBeRetrieved);
describe("paginateWithCursor", () => {
it.each(mockSetup)("Works %#", async ({ size, items, chunkSize }) => {
const fn = vi
.fn<PaginateWithCursorInput<number, number>["handler"]>()
.mockImplementation(async ({ cursor = 0 }) => {
const chunk = items.slice(cursor, cursor + chunkSize);
return {
done: cursor + chunk.length >= items.length,
data: chunk,
nextCursor: cursor + chunk.length,
};
});

const results = await paginateWithCursor({
size,
handler: fn,
});

const maxItemsToBeRetrieved = Math.min(size, items.length);
let expectedCalls = Math.ceil(maxItemsToBeRetrieved / chunkSize);
if (expectedCalls === 0 && size > 0) {
expectedCalls = 1;
}
expect(fn).toBeCalledTimes(expectedCalls);
expect(results).toHaveLength(maxItemsToBeRetrieved);
});
});
});

0 comments on commit d54eb22

Please sign in to comment.