1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix topbar to inject isLinux and isWindows to fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-01-26 16:09:20 -05:00
parent ba45bfea1e
commit fa72f28fc5
4 changed files with 66 additions and 19 deletions

View File

@ -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;

View File

@ -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;

View File

@ -14,13 +14,14 @@ import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable";
import { computed } from "mobx"; import { computed } from "mobx";
import directoryForUserDataInjectable from "../../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; import directoryForUserDataInjectable from "../../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import mockFs from "mock-fs"; 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", () => { jest.mock("../../../../common/vars", () => {
const { SemVer } = require("semver"); const { SemVer } = require("semver");
return { return {
...jest.requireActual<{}>("../../../../common/vars"), ...jest.requireActual<{}>("../../../../common/vars"),
isMac: true,
appSemVer: new SemVer("1.0.0"), appSemVer: new SemVer("1.0.0"),
}; };
}); });
@ -97,30 +98,30 @@ describe("<TopBar/>", () => {
}); });
it("renders home button", async () => { it("renders home button", async () => {
const { getByTestId } = render(<TopBar/>); const { findByTestId } = render(<TopBar/>);
expect(await getByTestId("home-button")).toBeInTheDocument(); expect(await findByTestId("home-button")).toBeInTheDocument();
}); });
it("renders history arrows", async () => { it("renders history arrows", async () => {
const { getByTestId } = render(<TopBar/>); const { findByTestId } = render(<TopBar/>);
expect(await getByTestId("history-back")).toBeInTheDocument(); expect(await findByTestId("history-back")).toBeInTheDocument();
expect(await getByTestId("history-forward")).toBeInTheDocument(); expect(await findByTestId("history-forward")).toBeInTheDocument();
}); });
it("enables arrow by ipc event", async () => { it("enables arrow by ipc event", async () => {
const { getByTestId } = render(<TopBar/>); const { findByTestId } = render(<TopBar/>);
expect(await getByTestId("history-back")).not.toHaveClass("disabled"); expect(await findByTestId("history-back")).not.toHaveClass("disabled");
expect(await getByTestId("history-forward")).not.toHaveClass("disabled"); expect(await findByTestId("history-forward")).not.toHaveClass("disabled");
}); });
it("triggers browser history back and forward", async () => { it("triggers browser history back and forward", async () => {
const { getByTestId } = render(<TopBar/>); const { findByTestId } = render(<TopBar/>);
const prevButton = await getByTestId("history-back"); const prevButton = await findByTestId("history-back");
const nextButton = await getByTestId("history-forward"); const nextButton = await findByTestId("history-forward");
fireEvent.click(prevButton); fireEvent.click(prevButton);
@ -143,12 +144,15 @@ describe("<TopBar/>", () => {
}, },
])); ]));
const { getByTestId } = render(<TopBar/>); const { findByTestId } = render(<TopBar/>);
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(<TopBar/>); const { queryByTestId } = render(<TopBar/>);
expect(queryByTestId("window-menu")).not.toBeInTheDocument(); expect(queryByTestId("window-menu")).not.toBeInTheDocument();
@ -156,4 +160,16 @@ describe("<TopBar/>", () => {
expect(queryByTestId("window-maximize")).not.toBeInTheDocument(); expect(queryByTestId("window-maximize")).not.toBeInTheDocument();
expect(queryByTestId("window-close")).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(<TopBar/>);
expect(queryByTestId("window-menu")).toBeInTheDocument();
expect(queryByTestId("window-minimize")).toBeInTheDocument();
expect(queryByTestId("window-maximize")).toBeInTheDocument();
expect(queryByTestId("window-close")).toBeInTheDocument();
});
}); });

View File

@ -13,18 +13,21 @@ import { ipcRendererOn } from "../../../../common/ipc";
import { watchHistoryState } from "../../../remote-helpers/history-updater"; import { watchHistoryState } from "../../../remote-helpers/history-updater";
import { isActiveRoute, navigate } from "../../../navigation"; import { isActiveRoute, navigate } from "../../../navigation";
import { catalogRoute, catalogURL } from "../../../../common/routes"; import { catalogRoute, catalogURL } from "../../../../common/routes";
import { isLinux, isWindows } from "../../../../common/vars";
import { cssNames } from "../../../utils"; import { cssNames } from "../../../utils";
import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable"; import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable";
import { withInjectables } from "@ogre-tools/injectable-react"; import { withInjectables } from "@ogre-tools/injectable-react";
import type { TopBarRegistration } from "./top-bar-registration"; import type { TopBarRegistration } from "./top-bar-registration";
import { emitOpenAppMenuAsContextMenu, requestWindowAction } from "../../../ipc"; import { emitOpenAppMenuAsContextMenu, requestWindowAction } from "../../../ipc";
import { WindowAction } from "../../../../common/ipc/window"; import { WindowAction } from "../../../../common/ipc/window";
import isLinuxInjectable from "../../../../common/vars/is-linux.injectable";
import isWindowsInjectable from "../../../../common/vars/is-windows.injectable";
interface Props extends React.HTMLAttributes<any> {} export interface TopBarProps extends React.HTMLAttributes<any> {}
interface Dependencies { interface Dependencies {
items: IComputedValue<TopBarRegistration[]>; items: IComputedValue<TopBarRegistration[]>;
isWindows: boolean;
isLinux: boolean;
} }
const prevEnabled = observable.box(false); const prevEnabled = observable.box(false);
@ -38,7 +41,7 @@ ipcRendererOn("history:can-go-forward", (event, state: boolean) => {
nextEnabled.set(state); nextEnabled.set(state);
}); });
const NonInjectedTopBar = (({ items, children, ...rest }: Props & Dependencies) => { const NonInjectedTopBar = observer(({ items, children, isWindows, isLinux, ...rest }: TopBarProps & Dependencies) => {
const elem = useRef<HTMLDivElement>(); const elem = useRef<HTMLDivElement>();
const openAppContextMenu = () => { const openAppContextMenu = () => {
@ -161,9 +164,11 @@ const renderRegisteredItems = (items: TopBarRegistration[]) => (
export const TopBar = withInjectables(observer(NonInjectedTopBar), { export const TopBar = withInjectables<Dependencies, TopBarProps>(NonInjectedTopBar, {
getProps: (di, props) => ({ getProps: (di, props) => ({
items: di.inject(topBarItemsInjectable), items: di.inject(topBarItemsInjectable),
isLinux: di.inject(isLinuxInjectable),
isWindows: di.inject(isWindowsInjectable),
...props, ...props,
}), }),
}); });