Skip to content

Commit

Permalink
[web] Better react-router-dom mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Mar 13, 2023
1 parent f0e75d3 commit 0413912
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 108 deletions.
13 changes: 4 additions & 9 deletions web/src/App.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) [2022] SUSE LLC
* Copyright (c) [2022-2023] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -28,11 +28,6 @@ import { STARTUP, CONFIG, INSTALL } from "~/client/phase";
import { IDLE, BUSY } from "~/client/status";

jest.mock("~/client");

jest.mock('react-router-dom', () => ({
Outlet: mockComponent("Content"),
}));

jest.mock("~/components/layout/Layout", () => mockLayout());

// Mock some components,
Expand Down Expand Up @@ -147,7 +142,7 @@ describe("App", () => {

it("renders the application content", async () => {
installerRender(<App />);
await screen.findByText("Content");
await screen.findByText(/Outlet Content/);
});
});

Expand All @@ -169,7 +164,7 @@ describe("App", () => {

it("renders the Installation component on the INSTALL phase", async () => {
installerRender(<App />);
await screen.findByText("Content");
await screen.findByText(/Outlet Content/);
changePhaseTo(INSTALL);
await screen.findByText("Installation Mock");
});
Expand All @@ -183,7 +178,7 @@ describe("App", () => {

it("renders the application's content", async () => {
installerRender(<App />);
await screen.findByText("Content");
await screen.findByText(/Outlet Content/);
});
});
});
12 changes: 5 additions & 7 deletions web/src/Main.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) [2022] SUSE LLC
* Copyright (c) [2022-2023] SUSE LLC
*
* All Rights Reserved.
*
Expand All @@ -21,18 +21,16 @@

import React from "react";
import { screen } from "@testing-library/react";
import { installerRender, mockComponent } from "~/test-utils";
import { plainRender, mockComponent } from "~/test-utils";

import Main from "~/Main";

jest.mock("~/components/questions/Questions", () => mockComponent("Questions Mock"));
jest.mock('react-router-dom', () => ({
Outlet: mockComponent("Content"),
}));

it("renders the Questions component and the content", async () => {
installerRender(<Main />);
plainRender(<Main />);

await screen.findByText("Questions Mock");
await screen.findByText("Content");
// react-router-dom Outlet is mocked. See test-utils for more details
await screen.findByText("Outlet Content");
});
6 changes: 1 addition & 5 deletions web/src/components/core/ChangeProductButton.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@

import React from "react";
import { screen, waitFor } from "@testing-library/react";
import { plainRender } from "~/test-utils";
import { plainRender, mockNavigateFn } from "~/test-utils";
import { createClient } from "~/client";
import { ChangeProductButton } from "~/components/core";

let mockProducts;
const mockNavigateFn = jest.fn();

jest.mock("~/client");
jest.mock("~/context/software", () => ({
Expand All @@ -37,9 +36,6 @@ jest.mock("~/context/software", () => ({
};
}
}));
jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
}));

beforeEach(() => {
createClient.mockImplementation(() => {
Expand Down
5 changes: 0 additions & 5 deletions web/src/components/core/Page.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ import { screen } from "@testing-library/react";
import { installerRender, mockLayout } from "~/test-utils";
import { Page } from "~/components/core";

const mockNavigateFn = jest.fn();

jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
}));
jest.mock("~/components/layout/Layout", () => mockLayout());

describe("Page", () => {
Expand Down
19 changes: 4 additions & 15 deletions web/src/components/core/Section.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@

import React from "react";
import { screen, within } from "@testing-library/react";
import { plainRender } from "~/test-utils";
import { plainRender, installerRender } from "~/test-utils";
import { Section } from "~/components/core";

jest.mock('react-router-dom', () => ({
Link: ({ to, children }) => <a href={to}>{children}</a>
}));

describe("Section", () => {
it("renders given title", () => {
plainRender(<Section title="settings" />);
Expand Down Expand Up @@ -74,10 +70,9 @@ describe("Section", () => {

describe("when path is given", () => {
it("renders a link for navigating to it", async () => {
plainRender(<Section title="Settings" path="/settings" />);
installerRender(<Section title="Settings" path="/settings" />);
const heading = screen.getByRole("heading", { name: "Settings" });
const link = within(heading).getByRole("link", { name: "Settings" });
// NOTE: ReactRouter#Link is mocked at the top of file.
expect(link).toHaveAttribute("href", "/settings");
});
});
Expand All @@ -86,7 +81,7 @@ describe("Section", () => {
describe("and path is not present", () => {
it("triggers it when the user click on the section title", async () => {
const openDialog = jest.fn();
const { user } = plainRender(
const { user } = installerRender(
<Section title="Settings" openDialog={openDialog} />
);
const button = screen.getByRole("button", { name: "Settings" });
Expand All @@ -96,15 +91,9 @@ describe("Section", () => {
});

describe("but path is present too", () => {
// Silence "Error: Not Implemented: navigation..." from jsdom when clicking a link
// https://github.com/jsdom/jsdom/issues/2112
const eventListener = (e) => e.preventDefault();
beforeEach(() => window.addEventListener("click", eventListener));
afterEach(() => window.removeEventListener("click", eventListener, true));

it("does not triggers it when the user click on the section title", async () => {
const openDialog = jest.fn();
const { user } = plainRender(
const { user } = installerRender(
<Section path="/settings" title="Settings" openDialog={openDialog} />
);
const link = screen.getByRole("link", { name: "Settings" });
Expand Down
14 changes: 4 additions & 10 deletions web/src/components/l10n/L10nPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,18 @@

import React from "react";
import { screen } from "@testing-library/react";
import { installerRender, mockLayout } from "~/test-utils";
import { installerRender, mockLayout, mockNavigateFn } from "~/test-utils";
import { L10nPage } from "~/components/l10n";
import { createClient } from "~/client";

const mockNavigateFn = jest.fn();

jest.mock("~/client");
jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
}));
jest.mock("~/components/layout/Layout", () => mockLayout());

const setLanguagesFn = jest.fn();
const languages = [
{ id: "en_US", name: "English" },
{ id: "de_DE", name: "German" }
];

const setLanguagesFn = jest.fn();
jest.mock("~/client");
jest.mock("~/components/layout/Layout", () => mockLayout());

beforeEach(() => {
// if defined outside, the mock is cleared automatically
Expand Down
5 changes: 0 additions & 5 deletions web/src/components/network/NetworkPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ import NetworkPage from "~/components/network/NetworkPage";
import { ConnectionTypes } from "~/client/network";
import { createClient } from "~/client";

const mockNavigateFn = jest.fn();

jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
}));
jest.mock("~/client");

const wiredConnection = {
Expand Down
8 changes: 1 addition & 7 deletions web/src/components/overview/L10nSection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@

import React from "react";
import { act, screen } from "@testing-library/react";
import { installerRender, createCallbackMock, mockComponent } from "~/test-utils";
import { installerRender, createCallbackMock } from "~/test-utils";
import { L10nSection } from "~/components/overview";
import { createClient } from "~/client";

const mockNavigateFn = jest.fn();

jest.mock("~/client");
jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
Link: mockComponent("Link")
}));

const languages = [
{ id: "en_US", name: "English" },
Expand Down
6 changes: 0 additions & 6 deletions web/src/components/overview/NetworkSection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ import { NetworkSection } from "~/components/overview";
import { ConnectionTypes, NetworkEventTypes } from "~/client/network";
import { createClient } from "~/client";

const mockNavigateFn = jest.fn();

jest.mock("~/client");
jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
Link: mockComponent("Link")
}));

jest.mock('~/components/core/SectionSkeleton', () => mockComponent("Section Skeleton"));

Expand Down
8 changes: 2 additions & 6 deletions web/src/components/overview/Overview.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ jest.mock("~/context/software", () => ({
}
}));

jest.mock('react-router-dom', () => ({
Navigate: mockComponent("Navigate"),
useNavigate: () => jest.fn()
}));

jest.mock("~/components/layout/Layout", () => mockLayout());
jest.mock("~/components/overview/L10nSection", () => mockComponent("Localization Section"));
jest.mock("~/components/overview/StorageSection", () => mockComponent("Storage Section"));
Expand Down Expand Up @@ -92,7 +87,8 @@ describe("when no product is selected", () => {
it("redirects to the product selection page", async () => {
installerRender(<Overview />);

await screen.findByText("Navigate");
// react-router-dom Navigate is mocked. See test-utils for more details.
await screen.findByText("Navigating to /products");
});
});

Expand Down
6 changes: 0 additions & 6 deletions web/src/components/overview/StorageSection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ import { createClient } from "~/client";
import { BUSY, IDLE } from "~/client/status";
import { StorageSection } from "~/components/overview";

const mockUseNavigate = jest.fn();
jest.mock("~/client");
jest.mock("~/components/core/SectionSkeleton", () => mockComponent("Loading storage"));
jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => mockUseNavigate,
Link: mockComponent("Link")
}));

let status = IDLE;
let proposal = {
Expand Down
8 changes: 1 addition & 7 deletions web/src/components/overview/UsersSection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@

import React from "react";
import { screen } from "@testing-library/react";
import { installerRender, mockComponent } from "~/test-utils";
import { installerRender } from "~/test-utils";
import { UsersSection } from "~/components/overview";
import { createClient } from "~/client";

const mockNavigateFn = jest.fn();

jest.mock("~/client");
jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigateFn,
Link: mockComponent("Link")
}));

const user = {
fullName: "Jane Doe",
Expand Down
14 changes: 4 additions & 10 deletions web/src/components/software/ProductSelectionPage.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) [2022] SUSE LLC
* Copyright (c) [2022-2023] SUSE LLC
*
* All Rights Reserved.
*
Expand All @@ -21,7 +21,7 @@

import React from "react";
import { screen } from "@testing-library/react";
import { installerRender, mockLayout } from "~/test-utils";
import { installerRender, mockLayout, mockNavigateFn } from "~/test-utils";
import { ProductSelectionPage } from "~/components/software";
import { createClient } from "~/client";

Expand All @@ -39,12 +39,6 @@ const products = [
];
jest.mock("~/client");

const mockUseNavigate = jest.fn();
jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => mockUseNavigate
}));

jest.mock("~/context/software", () => ({
...jest.requireActual("~/context/software"),
useSoftware: () => {
Expand Down Expand Up @@ -78,7 +72,7 @@ describe("when the user chooses a product", () => {
const button = await screen.findByRole("button", { name: "Select" });
await user.click(button);
expect(softwareMock.selectProduct).toHaveBeenCalledWith("MicroOS");
expect(mockUseNavigate).toHaveBeenCalledWith("/");
expect(mockNavigateFn).toHaveBeenCalledWith("/");
});
});

Expand All @@ -89,6 +83,6 @@ describe("when the user chooses does not change the product", () => {
const button = await screen.findByRole("button", { name: "Select" });
await user.click(button);
expect(softwareMock.selectProduct).not.toHaveBeenCalled();
expect(mockUseNavigate).toHaveBeenCalledWith("/");
expect(mockNavigateFn).toHaveBeenCalledWith("/");
});
});
6 changes: 0 additions & 6 deletions web/src/components/storage/ProposalPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ const FakeProposalTargetSection = ({ calculateProposal }) => {
};

jest.mock("~/client");
jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => jest.fn()
}));

jest.mock("~/components/core/SectionSkeleton", () => mockComponent("Loading proposal"));
jest.mock("~/components/storage/ProposalTargetSection", () => FakeProposalTargetSection);

jest.mock("~/components/storage/ProposalSettingsSection", () => mockComponent("Settings section"));
jest.mock("~/components/storage/ProposalActionsSection", () => mockComponent("Actions section"));

Expand Down
Loading

0 comments on commit 0413912

Please sign in to comment.