From 0b400d2b345b2808a5d5362e6c0c1cdadde0fff6 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 26 Jan 2022 16:09:20 -0500 Subject: [PATCH] Fix topbar to inject isLinux and isWindows to fix tests Signed-off-by: Sebastian Malton --- src/common/vars/is-linux.injectable.ts | 13 ++++++ src/common/vars/is-windows.injectable.ts | 13 ++++++ .../layout/top-bar/top-bar.test.tsx | 46 +++++++++++++------ .../components/layout/top-bar/top-bar.tsx | 14 ++++-- 4 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 src/common/vars/is-linux.injectable.ts create mode 100644 src/common/vars/is-windows.injectable.ts diff --git a/src/common/vars/is-linux.injectable.ts b/src/common/vars/is-linux.injectable.ts new file mode 100644 index 0000000000..a603f01951 --- /dev/null +++ b/src/common/vars/is-linux.injectable.ts @@ -0,0 +1,13 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; +import { isLinux } from "../vars"; + +const isLinuxInjectable = getInjectable({ + instantiate: () => isLinux, + lifecycle: lifecycleEnum.singleton, +}); + +export default isLinuxInjectable; diff --git a/src/common/vars/is-windows.injectable.ts b/src/common/vars/is-windows.injectable.ts new file mode 100644 index 0000000000..76a08a4af2 --- /dev/null +++ b/src/common/vars/is-windows.injectable.ts @@ -0,0 +1,13 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; +import { isWindows } from "../vars"; + +const isWindowsInjectable = getInjectable({ + instantiate: () => isWindows, + lifecycle: lifecycleEnum.singleton, +}); + +export default isWindowsInjectable; diff --git a/src/renderer/components/layout/top-bar/top-bar.test.tsx b/src/renderer/components/layout/top-bar/top-bar.test.tsx index bf5279ab77..8973c8ace6 100644 --- a/src/renderer/components/layout/top-bar/top-bar.test.tsx +++ b/src/renderer/components/layout/top-bar/top-bar.test.tsx @@ -14,13 +14,14 @@ import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable"; import { computed } from "mobx"; import directoryForUserDataInjectable from "../../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; import mockFs from "mock-fs"; +import isLinuxInjectable from "../../../../common/vars/is-linux.injectable"; +import isWindowsInjectable from "../../../../common/vars/is-windows.injectable"; jest.mock("../../../../common/vars", () => { const { SemVer } = require("semver"); return { ...jest.requireActual<{}>("../../../../common/vars"), - isMac: true, appSemVer: new SemVer("1.0.0"), }; }); @@ -93,30 +94,30 @@ describe("", () => { }); it("renders home button", async () => { - const { getByTestId } = render(); + const { findByTestId } = render(); - expect(await getByTestId("home-button")).toBeInTheDocument(); + expect(await findByTestId("home-button")).toBeInTheDocument(); }); it("renders history arrows", async () => { - const { getByTestId } = render(); + const { findByTestId } = render(); - expect(await getByTestId("history-back")).toBeInTheDocument(); - expect(await getByTestId("history-forward")).toBeInTheDocument(); + expect(await findByTestId("history-back")).toBeInTheDocument(); + expect(await findByTestId("history-forward")).toBeInTheDocument(); }); it("enables arrow by ipc event", async () => { - const { getByTestId } = render(); + const { findByTestId } = render(); - expect(await getByTestId("history-back")).not.toHaveClass("disabled"); - expect(await getByTestId("history-forward")).not.toHaveClass("disabled"); + expect(await findByTestId("history-back")).not.toHaveClass("disabled"); + expect(await findByTestId("history-forward")).not.toHaveClass("disabled"); }); it("triggers browser history back and forward", async () => { - const { getByTestId } = render(); + const { findByTestId } = render(); - const prevButton = await getByTestId("history-back"); - const nextButton = await getByTestId("history-forward"); + const prevButton = await findByTestId("history-back"); + const nextButton = await findByTestId("history-forward"); fireEvent.click(prevButton); @@ -139,12 +140,15 @@ describe("", () => { }, ])); - const { getByTestId } = render(); + const { findByTestId } = render(); - expect(await getByTestId(testId)).toHaveTextContent(text); + expect(await findByTestId(testId)).toHaveTextContent(text); }); - it("doesn't show windows title buttons", () => { + it("doesn't show windows title buttons on macos", () => { + di.override(isLinuxInjectable, () => false); + di.override(isWindowsInjectable, () => false); + const { queryByTestId } = render(); expect(queryByTestId("window-menu")).not.toBeInTheDocument(); @@ -152,4 +156,16 @@ describe("", () => { expect(queryByTestId("window-maximize")).not.toBeInTheDocument(); expect(queryByTestId("window-close")).not.toBeInTheDocument(); }); + + it("does show windows title buttons on linux", () => { + di.override(isLinuxInjectable, () => true); + di.override(isWindowsInjectable, () => false); + + const { queryByTestId } = render(); + + expect(queryByTestId("window-menu")).toBeInTheDocument(); + expect(queryByTestId("window-minimize")).toBeInTheDocument(); + expect(queryByTestId("window-maximize")).toBeInTheDocument(); + expect(queryByTestId("window-close")).toBeInTheDocument(); + }); }); diff --git a/src/renderer/components/layout/top-bar/top-bar.tsx b/src/renderer/components/layout/top-bar/top-bar.tsx index 9a122250d4..6036ca7d10 100644 --- a/src/renderer/components/layout/top-bar/top-bar.tsx +++ b/src/renderer/components/layout/top-bar/top-bar.tsx @@ -15,16 +15,19 @@ import { watchHistoryState } from "../../../remote-helpers/history-updater"; import { isActiveRoute, navigate } from "../../../navigation"; import { catalogRoute, catalogURL } from "../../../../common/routes"; import { IpcMainWindowEvents } from "../../../../main/window-manager"; -import { isLinux, isWindows } from "../../../../common/vars"; import { cssNames } from "../../../utils"; import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable"; import { withInjectables } from "@ogre-tools/injectable-react"; import type { TopBarRegistration } from "./top-bar-registration"; +import isLinuxInjectable from "../../../../common/vars/is-linux.injectable"; +import isWindowsInjectable from "../../../../common/vars/is-windows.injectable"; -interface Props extends React.HTMLAttributes {} +export interface TopBarProps extends React.HTMLAttributes {} interface Dependencies { items: IComputedValue; + isWindows: boolean; + isLinux: boolean; } const prevEnabled = observable.box(false); @@ -38,7 +41,7 @@ ipcRendererOn("history:can-go-forward", (event, state: boolean) => { nextEnabled.set(state); }); -const NonInjectedTopBar = (({ items, children, ...rest }: Props & Dependencies) => { +const NonInjectedTopBar = observer(({ items, children, isWindows, isLinux, ...rest }: TopBarProps & Dependencies) => { const elem = useRef(); const window = useMemo(() => getCurrentWindow(), []); @@ -159,10 +162,11 @@ const renderRegisteredItems = (items: TopBarRegistration[]) => ( -export const TopBar = withInjectables(observer(NonInjectedTopBar), { +export const TopBar = withInjectables(NonInjectedTopBar, { getProps: (di, props) => ({ items: di.inject(topBarItemsInjectable), - + isLinux: di.inject(isLinuxInjectable), + isWindows: di.inject(isWindowsInjectable), ...props, }), });