From 3df6e2042c91d6d2a10bace1c07ea581560a1e99 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Tue, 7 Dec 2021 16:14:58 +0300 Subject: [PATCH] Adding tests Signed-off-by: Alex Andreev --- .../layout/__tests__/topbar-linux.test.tsx | 57 ++++++++++++++ .../layout/__tests__/topbar-win.test.tsx | 77 +++++++++++++++++++ .../layout/__tests__/topbar.test.tsx | 9 +++ 3 files changed, 143 insertions(+) create mode 100644 src/renderer/components/layout/__tests__/topbar-linux.test.tsx create mode 100644 src/renderer/components/layout/__tests__/topbar-win.test.tsx diff --git a/src/renderer/components/layout/__tests__/topbar-linux.test.tsx b/src/renderer/components/layout/__tests__/topbar-linux.test.tsx new file mode 100644 index 0000000000..00d327e318 --- /dev/null +++ b/src/renderer/components/layout/__tests__/topbar-linux.test.tsx @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import React from "react"; +import { render } from "@testing-library/react"; +import "@testing-library/jest-dom/extend-expect"; +import { TopBar } from "../topbar"; +import { TopBarRegistry } from "../../../../extensions/registries"; + +jest.mock("../../../../common/ipc"); +jest.mock("../../../../common/vars", () => { + return { + isLinux: true, + isWindows: false, + }; +}); + +describe(" in Linux", () => { + beforeEach(() => { + TopBarRegistry.createInstance(); + }); + + afterEach(() => { + TopBarRegistry.resetInstance(); + }); + + it("shows menu icon", () => { + const { getByTestId } = render(); + + expect(getByTestId("window-menu")).toBeInTheDocument(); + }); + + it("doesn't show windows title buttons", () => { + const { queryByTestId } = render(); + + expect(queryByTestId("window-minimize")).not.toBeInTheDocument(); + expect(queryByTestId("window-maximize")).not.toBeInTheDocument(); + expect(queryByTestId("window-close")).not.toBeInTheDocument(); + }); +}); diff --git a/src/renderer/components/layout/__tests__/topbar-win.test.tsx b/src/renderer/components/layout/__tests__/topbar-win.test.tsx new file mode 100644 index 0000000000..c3be9d2826 --- /dev/null +++ b/src/renderer/components/layout/__tests__/topbar-win.test.tsx @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +import React from "react"; +import { render, fireEvent } from "@testing-library/react"; +import "@testing-library/jest-dom/extend-expect"; +import { TopBar } from "../topbar"; +import { TopBarRegistry } from "../../../../extensions/registries"; +import { IpcMainWindowEvents } from "../../../../main/window-manager"; +import { broadcastMessage } from "../../../../common/ipc"; + +jest.mock("../../../../common/ipc"); + +jest.mock("../../../../common/vars", () => { + return { + isWindows: true, + isLinux: false, + }; +}); + +describe(" in Windows", () => { + beforeEach(() => { + TopBarRegistry.createInstance(); + }); + + afterEach(() => { + TopBarRegistry.resetInstance(); + }); + + it("shows window controls", () => { + const { getByTestId } = render(); + + expect(getByTestId("window-menu")).toBeInTheDocument(); + expect(getByTestId("window-minimize")).toBeInTheDocument(); + expect(getByTestId("window-maximize")).toBeInTheDocument(); + expect(getByTestId("window-close")).toBeInTheDocument(); + }); + + it("triggers ipc events on click", () => { + const { getByTestId } = render(); + + const menu = getByTestId("window-menu"); + const minimize = getByTestId("window-minimize"); + const maximize = getByTestId("window-maximize"); + const close = getByTestId("window-close"); + + fireEvent.click(menu); + expect(broadcastMessage).toHaveBeenCalledWith(IpcMainWindowEvents.OPEN_CONTEXT_MENU); + + fireEvent.click(minimize); + expect(broadcastMessage).toHaveBeenCalledWith(IpcMainWindowEvents.MINIMIZE); + + fireEvent.click(maximize); + expect(broadcastMessage).toHaveBeenCalledWith(IpcMainWindowEvents.MAXIMIZE); + + fireEvent.click(close); + expect(broadcastMessage).toHaveBeenCalledWith(IpcMainWindowEvents.CLOSE); + }); +}); diff --git a/src/renderer/components/layout/__tests__/topbar.test.tsx b/src/renderer/components/layout/__tests__/topbar.test.tsx index 7eb4b01dda..17a9655ec1 100644 --- a/src/renderer/components/layout/__tests__/topbar.test.tsx +++ b/src/renderer/components/layout/__tests__/topbar.test.tsx @@ -134,4 +134,13 @@ describe("", () => { expect(await getByTestId(testId)).toHaveTextContent(text); }); + + it("doesn't show windows title buttons", () => { + const { queryByTestId } = render(); + + expect(queryByTestId("window-menu")).not.toBeInTheDocument(); + expect(queryByTestId("window-minimize")).not.toBeInTheDocument(); + expect(queryByTestId("window-maximize")).not.toBeInTheDocument(); + expect(queryByTestId("window-close")).not.toBeInTheDocument(); + }); });