Skip to content

Commit 446adc2

Browse files
committed
chore: add more tests and catch bugs
1 parent 5a6b889 commit 446adc2

File tree

3 files changed

+97
-58
lines changed

3 files changed

+97
-58
lines changed

site/src/components/PaginationWidget/PaginationNavButtons.tsx renamed to site/src/components/PaginationWidget/PaginationNavButton.tsx

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,27 @@
1-
import {
2-
type PropsWithChildren,
3-
type ReactNode,
4-
useEffect,
5-
useState,
6-
} from "react";
1+
import { type ReactNode, useEffect, useState } from "react";
72

8-
import KeyboardArrowLeft from "@mui/icons-material/KeyboardArrowLeft";
9-
import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight";
103
import Button from "@mui/material/Button";
114
import Tooltip from "@mui/material/Tooltip";
125
import { useTheme } from "@emotion/react";
136

14-
type NavProps = {
15-
currentPage: number;
16-
onPageChange: (newPage: number) => void;
17-
};
18-
19-
export function LeftNavButton({ currentPage, onPageChange }: NavProps) {
20-
const isFirstPage = currentPage <= 1;
21-
22-
return (
23-
<BaseNavButton
24-
disabledMessage="You are already on the first page"
25-
disabled={isFirstPage}
26-
aria-label="Previous page"
27-
onClick={() => {
28-
if (!isFirstPage) {
29-
onPageChange(currentPage - 1);
30-
}
31-
}}
32-
>
33-
<KeyboardArrowLeft />
34-
</BaseNavButton>
35-
);
36-
}
37-
38-
export function RightNavButton({ currentPage, onPageChange }: NavProps) {
39-
const isLastPage = currentPage <= 1;
40-
41-
return (
42-
<BaseNavButton
43-
disabledMessage="You're already on the last page"
44-
disabled={isLastPage}
45-
aria-label="Previous page"
46-
onClick={() => {
47-
if (!isLastPage) {
48-
onPageChange(currentPage + 1);
49-
}
50-
}}
51-
>
52-
<KeyboardArrowRight />
53-
</BaseNavButton>
54-
);
55-
}
56-
57-
type BaseButtonProps = PropsWithChildren<{
7+
type PaginationNavButtonProps = {
8+
children: ReactNode;
589
disabled: boolean;
5910
disabledMessage: ReactNode;
6011
onClick: () => void;
6112
"aria-label": string;
6213

6314
disabledMessageTimeout?: number;
64-
}>;
15+
};
6516

66-
function BaseNavButton({
17+
export function PaginationNavButton({
6718
children,
6819
onClick,
6920
disabled,
7021
disabledMessage,
7122
"aria-label": ariaLabel,
7223
disabledMessageTimeout = 3000,
73-
}: BaseButtonProps) {
24+
}: PaginationNavButtonProps) {
7425
const theme = useTheme();
7526
const [showDisabledMessage, setShowDisabledMessage] = useState(false);
7627

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { screen } from "@testing-library/react";
2+
import {
3+
PaginationWidgetBase,
4+
PaginationWidgetBaseProps,
5+
} from "./PaginationWidgetBase";
6+
import { renderWithAuth } from "testHelpers/renderHelpers";
7+
import userEvent from "@testing-library/user-event";
8+
9+
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+
];
15+
16+
describe(PaginationWidgetBase.name, () => {
17+
it("Should have its previous button be aria-disabled while on page 1", async () => {
18+
for (const props of sampleProps) {
19+
const onPageChange = jest.fn();
20+
21+
const { unmount } = renderWithAuth(
22+
<PaginationWidgetBase
23+
{...props}
24+
currentPage={1}
25+
onPageChange={onPageChange}
26+
/>,
27+
);
28+
29+
const button = await screen.findByLabelText("Previous page");
30+
expect(button).not.toBeDisabled();
31+
expect(button).toHaveAttribute("aria-disabled", "true");
32+
33+
await userEvent.click(button);
34+
expect(onPageChange).not.toHaveBeenCalled();
35+
unmount();
36+
}
37+
});
38+
39+
it("Should have its next button be aria-disabled while on last page", async () => {
40+
for (const props of sampleProps) {
41+
const onPageChange = jest.fn();
42+
const lastPage = Math.ceil(props.totalRecords / props.pageSize);
43+
44+
const { unmount } = renderWithAuth(
45+
<PaginationWidgetBase
46+
{...props}
47+
currentPage={lastPage}
48+
onPageChange={onPageChange}
49+
/>,
50+
);
51+
52+
const button = await screen.findByLabelText("Next page");
53+
expect(button).not.toBeDisabled();
54+
expect(button).toHaveAttribute("aria-disabled", "true");
55+
56+
await userEvent.click(button);
57+
expect(onPageChange).not.toHaveBeenCalled();
58+
unmount();
59+
}
60+
});
61+
});

site/src/components/PaginationWidget/PaginationWidgetBase.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { useTheme } from "@emotion/react";
33

44
import { PlaceholderPageButton, NumberedPageButton } from "./PageButtons";
55
import { buildPagedList } from "./utils";
6-
import { LeftNavButton, RightNavButton } from "./PaginationNavButtons";
6+
import { PaginationNavButton } from "./PaginationNavButton";
7+
import KeyboardArrowLeft from "@mui/icons-material/KeyboardArrowLeft";
8+
import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight";
79

810
export type PaginationWidgetBaseProps = {
911
currentPage: number;
@@ -26,6 +28,9 @@ export const PaginationWidgetBase = ({
2628
return null;
2729
}
2830

31+
const onFirstPage = currentPage <= 1;
32+
const onLastPage = currentPage >= totalPages;
33+
2934
return (
3035
<div
3136
css={{
@@ -37,7 +42,18 @@ export const PaginationWidgetBase = ({
3742
columnGap: "6px",
3843
}}
3944
>
40-
<LeftNavButton currentPage={currentPage} onPageChange={onPageChange} />
45+
<PaginationNavButton
46+
disabledMessage="You are already on the first page"
47+
disabled={onFirstPage}
48+
aria-label="Previous page"
49+
onClick={() => {
50+
if (!onFirstPage) {
51+
onPageChange(currentPage - 1);
52+
}
53+
}}
54+
>
55+
<KeyboardArrowLeft />
56+
</PaginationNavButton>
4157

4258
{isMobile ? (
4359
<NumberedPageButton
@@ -53,7 +69,18 @@ export const PaginationWidgetBase = ({
5369
/>
5470
)}
5571

56-
<RightNavButton currentPage={currentPage} onPageChange={onPageChange} />
72+
<PaginationNavButton
73+
disabledMessage="You're already on the last page"
74+
disabled={onLastPage}
75+
aria-label="Next page"
76+
onClick={() => {
77+
if (!onLastPage) {
78+
onPageChange(currentPage + 1);
79+
}
80+
}}
81+
>
82+
<KeyboardArrowRight />
83+
</PaginationNavButton>
5784
</div>
5885
);
5986
};

0 commit comments

Comments
 (0)