1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/dock/__test__/dock-tabs.test.tsx
Iku-turso a277cfcf02
Technical requirements for behavioural unit tests (#5084)
* Implement a lot of technical requirements for behavioural unit tests

Note: the crux of this was to make routing env-agnostic, and not based on URLs as magic strings, but instead something type-enforced.

Note: extension-based routes comply to same exact interface by "late-registering" their routes when installed. Routes are just injectables.

Note: another chunk of global shared state is no more.

Note: a lot of explicit side effects have been cornered to injectables.

Note: a lot of stuff has become reactive as part if this.

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make a directory commonly available

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Require id for <Select /> to prevent non-deterministic renders

This was caused by global state in a 3rd party lib: "react-select".

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Specify id for all <Select /> to satisfy previous commit

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Prevent explicit side effect in component by using existing dependency instead

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Extract instantiation of "conf" as injectables for causing side effects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Introduce a legacy-helper to make gradual refactoring of inheritors of Singleton easier

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Make legacy unit tests for hotbar green and more simple by using the new legacy helper

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Temporarily kludge all unit tests green with a disclaimer about allowing side-effects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove kludge in previous commit by explicitly permitting specific side effects where old unit tests require it

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Prevent old unit test with side effects from accessing file system

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Migrate to actual typing for di.permitSideEffects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Prevent unit tests from failing because of non-standard method of HTML-element not present in js-dom

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Adapt integration tests to recent changes

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Fix code style

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Fix artifact from bad rebase

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Add a deprecation from a review comment

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove change that is not required

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove redundant comment

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix code style

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove redundant file

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix bad merge

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Improve variable name

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Tweak logger interface to be more descriptive

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make injecting legacy singleton always provide new instance

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove conditional typing when not needed

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Improve naming of variable

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove unnecessary code style changes

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove flag for causing side effects from too broad scope

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Override side-effects in unit test using injectable instead of monkey patching

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Flag some side-effects and add general overrides

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Fix unit tests in CI by removing explicit side-effect

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove explicit side-effect from getting default shell

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce abstraction for getting absolute paths

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Switch to using abstraction for getting absolute path to control explicit side effect

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce abstraction for joining paths

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Switch to using abstraction for joining paths to control explicit side effect

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix fake implementation for join paths

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix test after removing explicit side effect

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove explicit side effects from kubeconfig-syncs

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix arguments after removing explicit side effect

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make registrators not async for not being needed anymore

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Make generalCatalogEntities non-observable, as there is no requirement

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Remove redundant code

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

* Simplify logic for registering general catalog entity sources

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Add TODO

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Replace function for getting application menu items with reactive solution

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix typo in interface name

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove global shared state usages of hot bar store

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove redundant enum

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>
2022-03-31 16:57:05 +03:00

223 lines
6.8 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import fse from "fs-extra";
import { DockTabs } from "../dock-tabs";
import { DockStore, DockTab, TabKind } from "../dock/store";
import { noop } from "../../../utils";
import { ThemeStore } from "../../../theme.store";
import { UserStore } from "../../../../common/user-store";
import { getDiForUnitTesting } from "../../../getDiForUnitTesting";
import dockStoreInjectable from "../dock/store.injectable";
import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor";
import directoryForUserDataInjectable
from "../../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import getConfigurationFileModelInjectable
from "../../../../common/get-configuration-file-model/get-configuration-file-model.injectable";
import appVersionInjectable
from "../../../../common/get-configuration-file-model/app-version/app-version.injectable";
jest.mock("electron", () => ({
app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
},
ipcMain: {
on: jest.fn(),
handle: jest.fn(),
},
ipcRenderer: {
on: jest.fn(),
invoke: jest.fn(),
},
}));
Object.defineProperty(window, "ResizeObserver", {
writable: true,
value: jest.fn().mockImplementation(() => ({
observe: jest.fn(),
disconnect: jest.fn(),
})),
});
const initialTabs: DockTab[] = [
{ id: "terminal", kind: TabKind.TERMINAL, title: "Terminal", pinned: false },
{ id: "create", kind: TabKind.CREATE_RESOURCE, title: "Create resource", pinned: false },
{ id: "edit", kind: TabKind.EDIT_RESOURCE, title: "Edit resource", pinned: false },
{ id: "install", kind: TabKind.INSTALL_CHART, title: "Install chart", pinned: false },
{ id: "logs", kind: TabKind.POD_LOGS, title: "Logs", pinned: false },
];
const getComponent = (dockStore: DockStore) => (
<DockTabs
tabs={dockStore.tabs}
selectedTab={dockStore.selectedTab}
autoFocus={true}
onChangeTab={noop}
/>
);
const getTabKinds = (dockStore: DockStore) => dockStore.tabs.map((tab) => tab.kind);
describe("<DockTabs />", () => {
let dockStore: DockStore;
let render: DiRender;
beforeEach(async () => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
render = renderFor(di);
di.override(
directoryForUserDataInjectable,
() => "some-test-suite-specific-directory-for-user-data",
);
di.permitSideEffects(getConfigurationFileModelInjectable);
di.permitSideEffects(appVersionInjectable);
await di.runSetups();
dockStore = di.inject(dockStoreInjectable);
UserStore.createInstance();
ThemeStore.createInstance();
await dockStore.whenReady;
dockStore.tabs = initialTabs;
});
afterEach(() => {
ThemeStore.resetInstance();
UserStore.resetInstance();
// TODO: A unit test may not cause side effects. Here accessing file system is a side effect.
fse.remove("some-test-suite-specific-directory-for-user-data");
});
it("renders w/o errors", () => {
const { container } = render(getComponent(dockStore));
expect(container).toBeInstanceOf(HTMLElement);
});
it("has 6 tabs (1 tab is initial terminal)", () => {
const { container } = render(getComponent(dockStore));
const tabs = container.querySelectorAll(".Tab");
expect(tabs.length).toBe(initialTabs.length);
});
it("opens a context menu", () => {
const { container, getByText } = render(getComponent(dockStore));
const tab = container.querySelector(".Tab");
fireEvent.contextMenu(tab);
expect(getByText("Close all tabs")).toBeInTheDocument();
});
it("closes selected tab", () => {
const { container, getByText, rerender } = render(
getComponent(dockStore),
);
const tab = container.querySelector(".Tab");
fireEvent.contextMenu(tab);
fireEvent.click(getByText("Close"));
rerender(getComponent(dockStore));
const tabs = container.querySelectorAll(".Tab");
expect(tabs.length).toBe(initialTabs.length - 1);
expect(getTabKinds(dockStore)).toEqual([
TabKind.CREATE_RESOURCE,
TabKind.EDIT_RESOURCE,
TabKind.INSTALL_CHART,
TabKind.POD_LOGS,
]);
});
it("closes other tabs", () => {
const { container, getByText, rerender } = render(getComponent(dockStore));
const tab = container.querySelectorAll(".Tab")[3];
fireEvent.contextMenu(tab);
fireEvent.click(getByText("Close other tabs"));
rerender(getComponent(dockStore));
const tabs = container.querySelectorAll(".Tab");
expect(tabs.length).toBe(1);
expect(getTabKinds(dockStore)).toEqual([initialTabs[3].kind]);
});
it("closes all tabs", () => {
const { container, getByText, rerender } = render(getComponent(dockStore));
const tab = container.querySelector(".Tab");
fireEvent.contextMenu(tab);
const command = getByText("Close all tabs");
fireEvent.click(command);
rerender(getComponent(dockStore));
const tabs = container.querySelectorAll(".Tab");
expect(tabs.length).toBe(0);
});
it("closes tabs to the right", () => {
const { container, getByText, rerender } = render(getComponent(dockStore));
const tab = container.querySelectorAll(".Tab")[3]; // 4th of 5
fireEvent.contextMenu(tab);
fireEvent.click(getByText("Close tabs to the right"));
rerender(getComponent(dockStore));
expect(getTabKinds(dockStore)).toEqual(
initialTabs.slice(0, 4).map(tab => tab.kind),
);
});
it("disables 'Close All' & 'Close Other' items if only 1 tab available", () => {
dockStore.tabs = [{
id: "terminal", kind: TabKind.TERMINAL, title: "Terminal", pinned: false,
}];
const { container, getByText } = render(getComponent(dockStore));
const tab = container.querySelector(".Tab");
fireEvent.contextMenu(tab);
const closeAll = getByText("Close all tabs");
const closeOthers = getByText("Close other tabs");
expect(closeAll).toHaveClass("disabled");
expect(closeOthers).toHaveClass("disabled");
});
it("disables 'Close To The Right' item if last tab clicked", () => {
dockStore.tabs = [
{ id: "terminal", kind: TabKind.TERMINAL, title: "Terminal", pinned: false },
{ id: "logs", kind: TabKind.POD_LOGS, title: "Pod Logs", pinned: false },
];
const { container, getByText } = render(getComponent(dockStore));
const tab = container.querySelectorAll(".Tab")[1];
fireEvent.contextMenu(tab);
const command = getByText("Close tabs to the right");
expect(command).toHaveClass("disabled");
});
});