From 05604980662e2dac9d76726bd4318ce945ebf0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz=20Gonz=C3=A1lez?= Date: Thu, 9 Mar 2023 13:10:59 +0000 Subject: [PATCH] [web] Limit when link to navigation page is rendered By not displaying it when already in the product selection path. --- .../components/core/ChangeProductButton.jsx | 5 +++-- .../core/ChangeProductButton.test.jsx | 19 ++++++++++++++-- web/src/test-utils.js | 22 +++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/web/src/components/core/ChangeProductButton.jsx b/web/src/components/core/ChangeProductButton.jsx index 01cc40a3af..fb351495a9 100644 --- a/web/src/components/core/ChangeProductButton.jsx +++ b/web/src/components/core/ChangeProductButton.jsx @@ -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; } diff --git a/web/src/components/core/ChangeProductButton.test.jsx b/web/src/components/core/ChangeProductButton.test.jsx index 283e17f39c..9517762db4 100644 --- a/web/src/components/core/ChangeProductButton.test.jsx +++ b/web/src/components/core/ChangeProductButton.test.jsx @@ -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"; @@ -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(); + await waitFor(() => expect(container).toBeEmptyDOMElement()); + }); + }); + describe("when there is only a single product", () => { beforeEach(() => { mockProducts = [ @@ -56,7 +71,7 @@ describe("ChangeProductButton", () => { }); it("renders nothing", async () => { - const { container } = plainRender(); + const { container } = installerRender(); await waitFor(() => expect(container).toBeEmptyDOMElement()); }); }); diff --git a/web/src/test-utils.js b/web/src/test-utils.js index 253e90dc19..a8b75d59a4 100644 --- a/web/src/test-utils.js +++ b/web/src/test-utils.js @@ -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 @@ -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"), @@ -55,7 +72,7 @@ const Providers = ({ children }) => { return ( - + {children} @@ -138,5 +155,6 @@ export { createCallbackMock, mockComponent, mockLayout, - mockNavigateFn + mockNavigateFn, + mockRoutes, };