Skip to content

Commit

Permalink
test: basic tests for static pages [1393]
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-s-nava committed Sep 23, 2024
1 parent 811c2e9 commit a4ad0cf
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
38 changes: 38 additions & 0 deletions frontend/tests/pages/not-found.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render, screen, waitFor } from "@testing-library/react";
import { axe } from "jest-axe";
import { identity } from "lodash";
import PageNotFound from "src/app/not-found";
import { useTranslationsMock } from "tests/utils/intlMocks";

jest.mock("next-intl/server", () => ({
getTranslations: () => identity,
unstable_setRequestLocale: identity,
}));

jest.mock("next-intl", () => ({
useTranslations: () => useTranslationsMock(),
}));

describe("PageNotFound", () => {
it("renders alert with grants.gov link", () => {
render(<PageNotFound />);

const alert = screen.queryByTestId("alert");

expect(alert).toBeInTheDocument();
});

it("links back to the home page", () => {
render(<PageNotFound />);
const link = screen.getByRole("link", { name: "visit_homepage_button" });

expect(link).toBeInTheDocument();
});

it("passes accessibility scan", async () => {
const { container } = render(<PageNotFound />);
const results = await waitFor(() => axe(container));

expect(results).toHaveNoViolations();
});
});
32 changes: 32 additions & 0 deletions frontend/tests/pages/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { render, screen, waitFor } from "@testing-library/react";
import { axe } from "jest-axe";
import { identity } from "lodash";
import Home from "src/app/[locale]/page";
import { mockMessages, useTranslationsMock } from "tests/utils/intlMocks";

jest.mock("next-intl/server", () => ({
getTranslations: () => identity,
unstable_setRequestLocale: identity,
}));

jest.mock("next-intl", () => ({
useTranslations: () => useTranslationsMock(),
useMessages: () => mockMessages,
}));

describe("Home", () => {
it("renders intro text", () => {
render(<Home />);

const content = screen.getByText("goal.paragraph_1");

expect(content).toBeInTheDocument();
});

it("passes accessibility scan", async () => {
const { container } = render(<Home />);
const results = await waitFor(() => axe(container));

expect(results).toHaveNoViolations();
});
});
32 changes: 32 additions & 0 deletions frontend/tests/pages/process/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { render, screen, waitFor } from "@testing-library/react";
import { axe } from "jest-axe";
import { identity } from "lodash";
import Process from "src/app/[locale]/process/page";
import { mockMessages, useTranslationsMock } from "tests/utils/intlMocks";

jest.mock("next-intl/server", () => ({
getTranslations: () => identity,
unstable_setRequestLocale: identity,
}));

jest.mock("next-intl", () => ({
useTranslations: () => useTranslationsMock(),
useMessages: () => mockMessages,
}));

describe("Process", () => {
it("renders intro text", () => {
render(<Process />);

const content = screen.getByText("intro.content");

expect(content).toBeInTheDocument();
});

it("passes accessibility scan", async () => {
const { container } = render(<Process />);
const results = await waitFor(() => axe(container));

expect(results).toHaveNoViolations();
});
});
32 changes: 32 additions & 0 deletions frontend/tests/pages/research/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { render, screen, waitFor } from "@testing-library/react";
import { axe } from "jest-axe";
import { identity } from "lodash";
import Research from "src/app/[locale]/research/page";
import { mockMessages, useTranslationsMock } from "tests/utils/intlMocks";

jest.mock("next-intl/server", () => ({
getTranslations: () => identity,
unstable_setRequestLocale: identity,
}));

jest.mock("next-intl", () => ({
useTranslations: () => useTranslationsMock(),
useMessages: () => mockMessages,
}));

describe("Research", () => {
it("renders intro text", () => {
render(<Research />);

const content = screen.getByText("intro.content");

expect(content).toBeInTheDocument();
});

it("passes accessibility scan", async () => {
const { container } = render(<Research />);
const results = await waitFor(() => axe(container));

expect(results).toHaveNoViolations();
});
});
44 changes: 44 additions & 0 deletions frontend/tests/utils/intlMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function mockUseTranslations(translationKey: string) {
return translationKey;
}

mockUseTranslations.rich = (translationKey: string) => translationKey;

// // test without i18n functionality, pass through translation key as text
// export function generateNextIntlServerMock() {
// return {
// getTranslations: () => (translationKey: string) => translationKey,
// unstable_setRequestLocale: () => {},
// };
// }

export function useTranslationsMock() {
return mockUseTranslations;
}

// mocking all types of messages, could split by message type in the future
export const mockMessages = {
Process: {
intro: {
boxes: ["firstKey"],
},
milestones: {
icon_list: ["firstIcon"],
},
},
Research: {
impact: {
boxes: ["firstKey"],
},
},
};

// export function useMessagesMock() {
// return {
// Process: {
// intro: {
// boxes: ["firstKey"],
// },
// },
// };
// }

0 comments on commit a4ad0cf

Please sign in to comment.