Skip to content

Commit

Permalink
fixes for pagination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Jan 17, 2025
1 parent 415b4e9 commit 3a1060d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/components/__tests__/Dashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ describe("Dashboard", () => {
await waitFor(() => {
expect(
within(screen.getByTestId("alerts-table")).getAllByRole("row"),
).toHaveLength(15);
).toHaveLength(16);
});
});

Expand All @@ -333,13 +333,16 @@ describe("Dashboard", () => {

render(<Dashboard />);

await waitFor(async () => {
await userEvent.click(screen.getByRole("button", { name: /next/i }));
await waitFor(
async () => {
await userEvent.click(screen.getByRole("button", { name: /next/i }));

expect(
within(screen.getByTestId("alerts-table")).getAllByRole("row").length,
).toBeLessThan(15);
});
expect(
within(screen.getByTestId("alerts-table")).getAllByRole("row").length,
).toBeLessThan(16);
},
{ timeout: 5000 },
);

// on the last page, we cannot go further
expect(screen.getByRole("button", { name: /next/i })).toBeDisabled();
Expand All @@ -357,7 +360,7 @@ describe("Dashboard", () => {

expect(
within(screen.getByTestId("alerts-table")).getAllByRole("row").length,
).toEqual(15);
).toEqual(16);
});
});
});
4 changes: 2 additions & 2 deletions src/hooks/useClientSidePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export function useClientSidePagination<T>(
pageSize: number,
) {
const pageStart = page * pageSize;
const pageEnd = page * pageSize + pageSize - 1;
const pageEnd = page * pageSize + pageSize;

const dataView = data.slice(pageStart, pageEnd);

const hasPreviousPage = page > 0;
const hasNextPage = pageEnd + 1 < data.length;
const hasNextPage = pageEnd < data.length;

return { pageStart, pageEnd, dataView, hasPreviousPage, hasNextPage };
}

0 comments on commit 3a1060d

Please sign in to comment.