Skip to content

Commit

Permalink
[web] Limit when link to navigation page is rendered
Browse files Browse the repository at this point in the history
By not displaying it when already in the product selection path.
  • Loading branch information
dgdavid committed Mar 13, 2023
1 parent 0843b70 commit 0560498
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
5 changes: 3 additions & 2 deletions web/src/components/core/ChangeProductButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
*/

import React from "react";
import { Link } from "react-router-dom";
import { Link, useLocation } from "react-router-dom";
import { useSoftware } from "~/context/software";
import { Icon } from "~/components/layout";

export default function ChangeProductButton() {
const { products } = useSoftware();
const { pathname } = useLocation();
const multiProduct = products?.length > 1;

if (!multiProduct) {
if (!multiProduct || pathname === "/products") {
return null;
}

Expand Down
19 changes: 17 additions & 2 deletions web/src/components/core/ChangeProductButton.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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

Expand All @@ -48,6 +48,21 @@ beforeEach(() => {
});

describe("ChangeProductButton", () => {
describe("when it's already in the product selection path", () => {
beforeEach(() => {
mockRoutes("/products");
mockProducts = [
{ id: "openSUSE", name: "openSUSE Tumbleweed" },
{ id: "Leap Micro", name: "openSUSE Micro" }
];
});

it("renders nothing", async () => {
const { container } = installerRender(<ChangeProductButton />);
await waitFor(() => expect(container).toBeEmptyDOMElement());
});
});

describe("when there is only a single product", () => {
beforeEach(() => {
mockProducts = [
Expand All @@ -56,7 +71,7 @@ describe("ChangeProductButton", () => {
});

it("renders nothing", async () => {
const { container } = plainRender(<ChangeProductButton />);
const { container } = installerRender(<ChangeProductButton />);
await waitFor(() => expect(container).toBeEmptyDOMElement());
});
});
Expand Down
22 changes: 20 additions & 2 deletions web/src/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import { render } from "@testing-library/react";
import { createClient } from "~/client/index";
import { InstallerClientProvider } from "~/context/installer";

/**
* Internal mock for manipulating routes, using ["/"] by default
*/
const initialRoutes = jest.fn().mockReturnValue(["/"]);

/**
* Allows checking when react-router-dom navigate function was
* called with certain path
Expand All @@ -42,6 +47,18 @@ import { InstallerClientProvider } from "~/context/installer";
*/
const mockNavigateFn = jest.fn();

/**
* Allows manipulating MemoryRouter routes for testing purpose
*
* NOTE: on purpose, it will take effect only once.
*
* @example
* mockRoutes("/products", "/storage");
*
* @param {...string} routes
*/
const mockRoutes = (...routes) => initialRoutes.mockReturnValueOnce(routes);

// Centralize the react-router-dom mock here
jest.mock('react-router-dom', () => ({
...jest.requireActual("react-router-dom"),
Expand All @@ -55,7 +72,7 @@ const Providers = ({ children }) => {

return (
<InstallerClientProvider client={client}>
<MemoryRouter>
<MemoryRouter initialEntries={initialRoutes()}>
{children}
</MemoryRouter>
</InstallerClientProvider>
Expand Down Expand Up @@ -138,5 +155,6 @@ export {
createCallbackMock,
mockComponent,
mockLayout,
mockNavigateFn
mockNavigateFn,
mockRoutes,
};

0 comments on commit 0560498

Please sign in to comment.