Skip to content

Commit

Permalink
test: basic tests for static pages [1393] (#2197)
Browse files Browse the repository at this point in the history
* adds utils for mocking intl
  • Loading branch information
doug-s-nava authored Sep 24, 2024
1 parent c3590d8 commit 27b6f35
Show file tree
Hide file tree
Showing 5 changed files with 160 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();
});
});
26 changes: 26 additions & 0 deletions frontend/tests/utils/intlMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function mockUseTranslations(translationKey: string) {
return translationKey;
}

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

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"],
},
},
};

1 comment on commit 27b6f35

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for ./frontend

St.
Category Percentage Covered / Total
🟢 Statements 89.67% 842/939
🟡 Branches 71.79% 224/312
🟢 Functions 86.57% 174/201
🟢 Lines 90.09% 791/878

Test suite run success

187 tests passing in 58 suites.

Report generated by 🧪jest coverage report action from 27b6f35

Please sign in to comment.