Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: search page component test [1393] #2189

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe this change occurred automatically when I started up a local server, not entirely sure why.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I get the same. It is a doc link update so I think we can just add it in this PR.

79 changes: 79 additions & 0 deletions frontend/tests/pages/search/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { render, screen } from "@testing-library/react";
import { identity } from "lodash";
import Search from "src/app/[locale]/search/page";
import { MockSearchFetcher } from "src/services/search/searchfetcher/MockSearchFetcher";
import { useTranslationsMock } from "tests/utils/intlMocks";

// test without feature flag functionality
jest.mock("src/hoc/search/withFeatureFlag", () =>
jest.fn((Component: React.Component) => Component),
);

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

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

// mock API interactions
jest.mock("src/services/search/searchfetcher/SearchFetcherUtil", () => ({
getSearchFetcher: () => new MockSearchFetcher(),
}));

jest.mock("next/navigation", () => ({
...jest.requireActual<typeof import("next/navigation")>("next/navigation"),
useRouter: () => ({
// eslint-disable-next-line @typescript-eslint/no-empty-function
push: () => {},
}),
}));

/*
nested async server components (< ...Fetcher />) are currently breaking the render.
stated workarounds are not working. to get testing minimally working, overriding
Suspense to force display of fallback UI.

for more see https://github.com/testing-library/react-testing-library/issues/1209
*/
jest.mock("react", () => ({
...jest.requireActual<typeof import("react")>("react"),
Suspense: ({ fallback }: { fallback: React.Component }) => fallback,
}));

describe("Search Route", () => {
it("renders the search page with expected checkboxes checked", async () => {
const mockSearchParams = {
status: "forecasted,posted",
};

render(<Search searchParams={mockSearchParams} />);

// translation service is mocked, so the expected label here is the translation key rather than the label text
const forecastedCheckbox = await screen.findByLabelText(
"opportunityStatus.label.forecasted",
);
expect(forecastedCheckbox).toBeInTheDocument();
expect(forecastedCheckbox).toBeChecked();

const postedCheckbox = await screen.findByLabelText(
"opportunityStatus.label.posted",
);
expect(postedCheckbox).toBeInTheDocument();
expect(postedCheckbox).toBeChecked();

const closedCheckbox = await screen.findByLabelText(
"opportunityStatus.label.closed",
);
expect(closedCheckbox).toBeInTheDocument();
expect(closedCheckbox).not.toBeChecked();

const archivedCheckbox = await screen.findByLabelText(
"opportunityStatus.label.archived",
);
expect(archivedCheckbox).toBeInTheDocument();
expect(archivedCheckbox).not.toBeChecked();
});
});
Loading