Skip to content

Commit 964cc2d

Browse files
committed
chore: add one more test case for pagination buttons
1 parent 718c9bf commit 964cc2d

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

site/src/components/PaginationWidget/PaginationWidgetBase.test.tsx

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,42 @@ import { renderWithAuth } from "testHelpers/renderHelpers";
77
import userEvent from "@testing-library/user-event";
88

99
type SampleProps = Omit<PaginationWidgetBaseProps, "onPageChange">;
10-
const sampleProps: SampleProps[] = [
11-
{ currentPage: 1, pageSize: 5, totalRecords: 6 },
12-
{ currentPage: 1, pageSize: 50, totalRecords: 200 },
13-
{ currentPage: 2, pageSize: 20, totalRecords: 3000 },
14-
];
1510

1611
describe(PaginationWidgetBase.name, () => {
1712
it("Should have its previous button be aria-disabled while on page 1", async () => {
13+
const sampleProps: SampleProps[] = [
14+
{ currentPage: 1, pageSize: 5, totalRecords: 6 },
15+
{ currentPage: 1, pageSize: 50, totalRecords: 200 },
16+
{ currentPage: 1, pageSize: 20, totalRecords: 3000 },
17+
];
18+
1819
for (const props of sampleProps) {
1920
const onPageChange = jest.fn();
20-
2121
const { unmount } = renderWithAuth(
22-
<PaginationWidgetBase
23-
{...props}
24-
currentPage={1}
25-
onPageChange={onPageChange}
26-
/>,
22+
<PaginationWidgetBase {...props} onPageChange={onPageChange} />,
2723
);
2824

29-
const button = await screen.findByLabelText("Previous page");
30-
expect(button).not.toBeDisabled();
31-
expect(button).toHaveAttribute("aria-disabled", "true");
25+
const prevButton = await screen.findByLabelText("Previous page");
26+
expect(prevButton).not.toBeDisabled();
27+
expect(prevButton).toHaveAttribute("aria-disabled", "true");
3228

33-
await userEvent.click(button);
29+
await userEvent.click(prevButton);
3430
expect(onPageChange).not.toHaveBeenCalled();
3531
unmount();
3632
}
3733
});
3834

3935
it("Should have its next button be aria-disabled while on last page", async () => {
36+
const sampleProps: SampleProps[] = [
37+
{ currentPage: 2, pageSize: 5, totalRecords: 6 },
38+
{ currentPage: 4, pageSize: 50, totalRecords: 200 },
39+
{ currentPage: 10, pageSize: 100, totalRecords: 1000 },
40+
];
41+
4042
for (const props of sampleProps) {
4143
const onPageChange = jest.fn();
42-
const lastPage = Math.ceil(props.totalRecords / props.pageSize);
43-
4444
const { unmount } = renderWithAuth(
45-
<PaginationWidgetBase
46-
{...props}
47-
currentPage={lastPage}
48-
onPageChange={onPageChange}
49-
/>,
45+
<PaginationWidgetBase {...props} onPageChange={onPageChange} />,
5046
);
5147

5248
const button = await screen.findByLabelText("Next page");
@@ -58,4 +54,33 @@ describe(PaginationWidgetBase.name, () => {
5854
unmount();
5955
}
6056
});
57+
58+
it("Should have neither button be disabled for all other pages", async () => {
59+
const sampleProps: SampleProps[] = [
60+
{ currentPage: 11, pageSize: 5, totalRecords: 60 },
61+
{ currentPage: 2, pageSize: 50, totalRecords: 200 },
62+
{ currentPage: 3, pageSize: 20, totalRecords: 100 },
63+
];
64+
65+
for (const props of sampleProps) {
66+
const onPageChange = jest.fn();
67+
const { unmount } = renderWithAuth(
68+
<PaginationWidgetBase {...props} onPageChange={onPageChange} />,
69+
);
70+
71+
const prevButton = await screen.findByLabelText("Previous page");
72+
const nextButton = await screen.findByLabelText("Next page");
73+
74+
expect(prevButton).not.toBeDisabled();
75+
expect(prevButton).toHaveAttribute("aria-disabled", "false");
76+
77+
expect(nextButton).not.toBeDisabled();
78+
expect(nextButton).toHaveAttribute("aria-disabled", "false");
79+
80+
await userEvent.click(prevButton);
81+
await userEvent.click(nextButton);
82+
expect(onPageChange).toHaveBeenCalledTimes(2);
83+
unmount();
84+
}
85+
});
6186
});

0 commit comments

Comments
 (0)