Skip to content

Commit a08a149

Browse files
committed
chore: beef up isNonInitialPage and add tests
1 parent 98e43be commit a08a149

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

site/src/components/PaginationWidget/utils.test.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { buildPagedList, getOffset } from "./utils";
1+
import { buildPagedList, getOffset, isNonInitialPage } from "./utils";
22

3-
describe("buildPagedList", () => {
3+
describe(buildPagedList.name, () => {
44
it("has no placeholder entries when there are seven or fewer pages", () => {
55
for (let i = 1; i <= 7; i++) {
66
const expectedResult: number[] = [];
@@ -55,7 +55,7 @@ describe("buildPagedList", () => {
5555
});
5656
});
5757

58-
describe("getOffset", () => {
58+
describe(getOffset.name, () => {
5959
it("returns 0 on page 1", () => {
6060
const page = 1;
6161
const limit = 10;
@@ -70,9 +70,37 @@ describe("getOffset", () => {
7070
}
7171
});
7272

73-
it("Returns offset based on the current page for all pages after 1", () => {
73+
it("Returns offset based on the current page for all valid pages after 1", () => {
7474
expect(getOffset(2, 10)).toEqual(10);
7575
expect(getOffset(3, 10)).toEqual(20);
7676
expect(getOffset(4, 45)).toEqual(135);
7777
});
7878
});
79+
80+
describe(isNonInitialPage.name, () => {
81+
it("Should detect your page correctly if input is set and valid", () => {
82+
const params1 = new URLSearchParams({ page: String(1) });
83+
expect(isNonInitialPage(params1)).toBe(false);
84+
85+
const inputs = [2, 50, 500, 3722];
86+
87+
for (const input of inputs) {
88+
const params = new URLSearchParams({ page: String(input) });
89+
expect(isNonInitialPage(params)).toBe(true);
90+
}
91+
});
92+
93+
it("Should act as if you are on page 1 if input is set but invalid", () => {
94+
const inputs = ["", Infinity, -Infinity, NaN, 3.74, -3];
95+
96+
for (const input of inputs) {
97+
const params = new URLSearchParams({ page: String(input) });
98+
expect(isNonInitialPage(params)).toBe(false);
99+
}
100+
});
101+
102+
it("Should act as if you are on page 1 if input does not exist", () => {
103+
const params = new URLSearchParams();
104+
expect(isNonInitialPage(params)).toBe(false);
105+
});
106+
});

site/src/components/PaginationWidget/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ export const getOffset = (page: number, limit: number): number => {
6464
return (pageToUse - 1) * limit;
6565
};
6666

67-
export const nonInitialPage = (searchParams: URLSearchParams): boolean => {
67+
export const isNonInitialPage = (searchParams: URLSearchParams): boolean => {
6868
const page = searchParams.get("page");
69-
const numberPage = page ? Number(page) : 1;
70-
return numberPage > 1;
69+
const conversion = Number(page);
70+
const inputIsValid = Number.isInteger(conversion) && conversion >= 1;
71+
72+
return inputIsValid ? conversion > 1 : false;
7173
};

site/src/pages/AuditPage/AuditPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
DEFAULT_RECORDS_PER_PAGE,
3-
nonInitialPage,
3+
isNonInitialPage,
44
} from "components/PaginationWidget/utils";
55
import { useFeatureVisibility } from "hooks/useFeatureVisibility";
66
import { FC } from "react";
@@ -73,7 +73,7 @@ const AuditPage: FC = () => {
7373
page={pagination.page}
7474
limit={pagination.limit}
7575
onPageChange={pagination.goToPage}
76-
isNonInitialPage={nonInitialPage(searchParamsResult[0])}
76+
isNonInitialPage={isNonInitialPage(searchParamsResult[0])}
7777
isAuditLogVisible={isAuditLogVisible}
7878
error={error}
7979
filterProps={{

site/src/pages/UsersPage/UsersPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { prepareQuery } from "utils/filters";
2828

2929
import { Helmet } from "react-helmet-async";
3030
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog";
31-
import { nonInitialPage } from "components/PaginationWidget/utils";
31+
import { isNonInitialPage } from "components/PaginationWidget/utils";
3232
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog";
3333
import { ResetPasswordDialog } from "./ResetPasswordDialog";
3434
import { pageTitle } from "utils/page";
@@ -157,7 +157,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
157157
isLoading={isLoading}
158158
canEditUsers={canEditUsers}
159159
canViewActivity={entitlements.features.audit_log.enabled}
160-
isNonInitialPage={nonInitialPage(searchParams)}
160+
isNonInitialPage={isNonInitialPage(searchParams)}
161161
actorID={me.id}
162162
filterProps={{
163163
filter: useFilterResult,

0 commit comments

Comments
 (0)