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>
This commit is contained in:
Iku-tursoandJanne Savolainen authored and GitHub committed 2022-03-31 16:57:05 +03:00
1 parent ab2cd27a27
commit a277cfcf02
583 files changed
+13073 -6556

No files matched your search

@@ -0,0 +1,337 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { checkForUpdates } from "../app-updater";
import { docsUrl, productName, supportUrl } from "../../common/vars";
import { exitApp } from "../exit-app";
import { broadcastMessage } from "../../common/ipc";
import { openBrowser } from "../../common/utils";
import { showAbout } from "./menu";
import windowManagerInjectable from "../window-manager.injectable";
import {
BrowserWindow,
MenuItem,
MenuItemConstructorOptions,
webContents,
} from "electron";
import loggerInjectable from "../../common/logger.injectable";
import appNameInjectable from "../app-paths/app-name/app-name.injectable";
import electronMenuItemsInjectable from "./electron-menu-items.injectable";
import isAutoUpdateEnabledInjectable from "../is-auto-update-enabled.injectable";
import navigateToPreferencesInjectable from "../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
import navigateToExtensionsInjectable from "../../common/front-end-routing/routes/extensions/navigate-to-extensions.injectable";
import navigateToCatalogInjectable from "../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import navigateToWelcomeInjectable from "../../common/front-end-routing/routes/welcome/navigate-to-welcome.injectable";
import navigateToAddClusterInjectable from "../../common/front-end-routing/routes/add-cluster/navigate-to-add-cluster.injectable";
import isMacInjectable from "../../common/vars/is-mac.injectable";
import { computed } from "mobx";
function ignoreIf(check: boolean, menuItems: MenuItemConstructorOptions[]) {
return check ? [] : menuItems;
}
export interface MenuItemOpts extends MenuItemConstructorOptions {
submenu?: MenuItemConstructorOptions[];
}
const applicationMenuItemsInjectable = getInjectable({
id: "application-menu-items",
instantiate: (di) => {
const logger = di.inject(loggerInjectable);
const appName = di.inject(appNameInjectable);
const isMac = di.inject(isMacInjectable);
const isAutoUpdateEnabled = di.inject(isAutoUpdateEnabledInjectable);
const electronMenuItems = di.inject(electronMenuItemsInjectable);
return computed((): MenuItemOpts[] => {
// TODO: These injects should happen outside of the computed.
// TODO: Remove temporal dependencies in WindowManager to make sure timing is correct.
const windowManager = di.inject(windowManagerInjectable);
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
const navigateToExtensions = di.inject(navigateToExtensionsInjectable);
const navigateToCatalog = di.inject(navigateToCatalogInjectable);
const navigateToWelcome = di.inject(navigateToWelcomeInjectable);
const navigateToAddCluster = di.inject(navigateToAddClusterInjectable);
const autoUpdateDisabled = !isAutoUpdateEnabled();
logger.info(`[MENU]: autoUpdateDisabled=${autoUpdateDisabled}`);
const macAppMenu: MenuItemOpts = {
label: appName,
id: "root",
submenu: [
{
label: `About ${productName}`,
id: "about",
click(menuItem: MenuItem, browserWindow: BrowserWindow) {
showAbout(browserWindow);
},
},
...ignoreIf(autoUpdateDisabled, [
{
label: "Check for updates",
click() {
checkForUpdates().then(() => windowManager.ensureMainWindow());
},
},
]),
{ type: "separator" },
{
label: "Preferences",
accelerator: "CmdOrCtrl+,",
id: "preferences",
click() {
navigateToPreferences();
},
},
{
label: "Extensions",
accelerator: "CmdOrCtrl+Shift+E",
id: "extensions",
click() {
navigateToExtensions();
},
},
{ type: "separator" },
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ role: "unhide" },
{ type: "separator" },
{
label: "Quit",
accelerator: "Cmd+Q",
id: "quit",
click() {
exitApp();
},
},
],
};
const fileMenu: MenuItemOpts = {
label: "File",
id: "file",
submenu: [
{
label: "Add Cluster",
accelerator: "CmdOrCtrl+Shift+A",
id: "add-cluster",
click() {
navigateToAddCluster();
},
},
...ignoreIf(isMac, [
{ type: "separator" },
{
label: "Preferences",
id: "preferences",
accelerator: "Ctrl+,",
click() {
navigateToPreferences();
},
},
{
label: "Extensions",
accelerator: "Ctrl+Shift+E",
click() {
navigateToExtensions();
},
},
]),
{ type: "separator" },
...(isMac
? ([
{
role: "close",
label: "Close Window",
accelerator: "Shift+Cmd+W",
},
] as MenuItemConstructorOptions[])
: []),
...ignoreIf(isMac, [
{
label: "Exit",
accelerator: "Alt+F4",
id: "quit",
click() {
exitApp();
},
},
]),
],
};
const editMenu: MenuItemOpts = {
label: "Edit",
id: "edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
],
};
const viewMenu: MenuItemOpts = {
label: "View",
id: "view",
submenu: [
{
label: "Catalog",
accelerator: "Shift+CmdOrCtrl+C",
id: "catalog",
click() {
navigateToCatalog();
},
},
{
label: "Command Palette...",
accelerator: "Shift+CmdOrCtrl+P",
id: "command-palette",
click(_m, _b, event) {
/**
* Don't broadcast unless it was triggered by menu iteration so that
* there aren't double events in renderer
*
* NOTE: this `?` is required because of a bug in playwright. https://github.com/microsoft/playwright/issues/10554
*/
if (!event?.triggeredByAccelerator) {
broadcastMessage("command-palette:open");
}
},
},
{ type: "separator" },
{
label: "Back",
accelerator: "CmdOrCtrl+[",
id: "go-back",
click() {
webContents
.getAllWebContents()
.filter((wc) => wc.getType() === "window")
.forEach((wc) => wc.goBack());
},
},
{
label: "Forward",
accelerator: "CmdOrCtrl+]",
id: "go-forward",
click() {
webContents
.getAllWebContents()
.filter((wc) => wc.getType() === "window")
.forEach((wc) => wc.goForward());
},
},
{
label: "Reload",
accelerator: "CmdOrCtrl+R",
id: "reload",
click() {
windowManager.reload();
},
},
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetZoom" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
};
const helpMenu: MenuItemOpts = {
role: "help",
id: "help",
submenu: [
{
label: "Welcome",
id: "welcome",
click() {
navigateToWelcome();
},
},
{
label: "Documentation",
id: "documentation",
click: async () => {
openBrowser(docsUrl).catch((error) => {
logger.error("[MENU]: failed to open browser", { error });
});
},
},
{
label: "Support",
id: "support",
click: async () => {
openBrowser(supportUrl).catch((error) => {
logger.error("[MENU]: failed to open browser", { error });
});
},
},
...ignoreIf(isMac, [
{
label: `About ${productName}`,
id: "about",
click(menuItem: MenuItem, browserWindow: BrowserWindow) {
showAbout(browserWindow);
},
},
...ignoreIf(autoUpdateDisabled, [
{
label: "Check for updates",
click() {
checkForUpdates().then(() =>
windowManager.ensureMainWindow(),
);
},
},
]),
]),
],
};
// Prepare menu items order
const appMenu = new Map([
["mac", macAppMenu],
["file", fileMenu],
["edit", editMenu],
["view", viewMenu],
["help", helpMenu],
]);
// Modify menu from extensions-api
for (const menuItem of electronMenuItems.get()) {
if (!appMenu.has(menuItem.parentId)) {
logger.error(
`[MENU]: cannot register menu item for parentId=${menuItem.parentId}, parent item doesn't exist`,
{ menuItem },
);
continue;
}
appMenu.get(menuItem.parentId).submenu.push(menuItem);
}
if (!isMac) {
appMenu.delete("mac");
}
return [...appMenu.values()];
});
},
});
export default applicationMenuItemsInjectable;
+7 -297
View File
@@ -2,30 +2,18 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, webContents } from "electron";
import { app, BrowserWindow, dialog, Menu } from "electron";
import { autorun, IComputedValue } from "mobx";
import type { WindowManager } from "../window-manager";
import { appName, isMac, isWindows, docsUrl, supportUrl, productName } from "../../common/vars";
import logger from "../logger";
import { exitApp } from "../exit-app";
import { broadcastMessage } from "../../common/ipc";
import { openBrowser } from "../../common/utils";
import { appName, isWindows, productName } from "../../common/vars";
import packageJson from "../../../package.json";
import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../../common/routes";
import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater";
import type { MenuRegistration } from "./menu-registration";
import type { MenuItemOpts } from "./application-menu-items.injectable";
export type MenuTopId = "mac" | "file" | "edit" | "view" | "help";
interface MenuItemsOpts extends MenuItemConstructorOptions {
submenu?: MenuItemConstructorOptions[];
}
export function initMenu(
windowManager: WindowManager,
electronMenuItems: IComputedValue<MenuRegistration[]>,
applicationMenuItems: IComputedValue<MenuItemOpts[]>,
) {
return autorun(() => buildMenu(windowManager, electronMenuItems.get()), {
return autorun(() => buildMenu(applicationMenuItems.get()), {
delay: 100,
});
}
@@ -48,288 +36,10 @@ export function showAbout(browserWindow: BrowserWindow) {
});
}
export function getAppMenu(
windowManager: WindowManager,
electronMenuItems: MenuRegistration[],
) {
function ignoreIf(check: boolean, menuItems: MenuItemConstructorOptions[]) {
return check ? [] : menuItems;
}
async function navigate(url: string) {
logger.info(`[MENU]: navigating to ${url}`);
await windowManager.navigate(url);
}
const autoUpdateDisabled = !isAutoUpdateEnabled();
logger.info(`[MENU]: autoUpdateDisabled=${autoUpdateDisabled}`);
const macAppMenu: MenuItemsOpts = {
label: app.getName(),
id: "root",
submenu: [
{
label: `About ${productName}`,
id: "about",
click(menuItem: MenuItem, browserWindow: BrowserWindow) {
showAbout(browserWindow);
},
},
...ignoreIf(autoUpdateDisabled, [{
label: "Check for updates",
click() {
checkForUpdates()
.then(() => windowManager.ensureMainWindow());
},
}]),
{ type: "separator" },
{
label: "Preferences",
accelerator: "CmdOrCtrl+,",
id: "preferences",
click() {
navigate(preferencesURL());
},
},
{
label: "Extensions",
accelerator: "CmdOrCtrl+Shift+E",
id: "extensions",
click() {
navigate(extensionsURL());
},
},
{ type: "separator" },
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ role: "unhide" },
{ type: "separator" },
{
label: "Quit",
accelerator: "Cmd+Q",
id: "quit",
click() {
exitApp();
},
},
],
};
const fileMenu: MenuItemsOpts = {
label: "File",
id: "file",
submenu: [
{
label: "Add Cluster",
accelerator: "CmdOrCtrl+Shift+A",
id: "add-cluster",
click() {
navigate(addClusterURL());
},
},
...ignoreIf(isMac, [
{ type: "separator" },
{
label: "Preferences",
id: "preferences",
accelerator: "Ctrl+,",
click() {
navigate(preferencesURL());
},
},
{
label: "Extensions",
accelerator: "Ctrl+Shift+E",
click() {
navigate(extensionsURL());
},
},
]),
{ type: "separator" },
...(isMac ? [
{
role: "close",
label: "Close Window",
accelerator: "Shift+Cmd+W",
},
] as MenuItemConstructorOptions[] : []),
...ignoreIf(isMac, [
{
label: "Exit",
accelerator: "Alt+F4",
id: "quit",
click() {
exitApp();
},
},
]),
],
};
const editMenu: MenuItemsOpts = {
label: "Edit",
id: "edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
],
};
const viewMenu: MenuItemsOpts = {
label: "View",
id: "view",
submenu: [
{
label: "Catalog",
accelerator: "Shift+CmdOrCtrl+C",
id: "catalog",
click() {
navigate(catalogURL());
},
},
{
label: "Command Palette...",
accelerator: "Shift+CmdOrCtrl+P",
id: "command-palette",
click(_m, _b, event) {
/**
* Don't broadcast unless it was triggered by menu iteration so that
* there aren't double events in renderer
*
* NOTE: this `?` is required because of a bug in playwright. https://github.com/microsoft/playwright/issues/10554
*/
if (!event?.triggeredByAccelerator) {
broadcastMessage("command-palette:open");
}
},
},
{ type: "separator" },
{
label: "Back",
accelerator: "CmdOrCtrl+[",
id: "go-back",
click() {
webContents.getAllWebContents().filter(wc => wc.getType() === "window").forEach(wc => wc.goBack());
},
},
{
label: "Forward",
accelerator: "CmdOrCtrl+]",
id: "go-forward",
click() {
webContents.getAllWebContents().filter(wc => wc.getType() === "window").forEach(wc => wc.goForward());
},
},
{
label: "Reload",
accelerator: "CmdOrCtrl+R",
id: "reload",
click() {
windowManager.reload();
},
},
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetZoom" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
};
const helpMenu: MenuItemsOpts = {
role: "help",
id: "help",
submenu: [
{
label: "Welcome",
id: "welcome",
click() {
navigate(welcomeURL());
},
},
{
label: "Documentation",
id: "documentation",
click: async () => {
openBrowser(docsUrl).catch(error => {
logger.error("[MENU]: failed to open browser", { error });
});
},
},
{
label: "Support",
id: "support",
click: async () => {
openBrowser(supportUrl).catch(error => {
logger.error("[MENU]: failed to open browser", { error });
});
},
},
...ignoreIf(isMac, [
{
label: `About ${productName}`,
id: "about",
click(menuItem: MenuItem, browserWindow: BrowserWindow) {
showAbout(browserWindow);
},
},
...ignoreIf(autoUpdateDisabled, [{
label: "Check for updates",
click() {
checkForUpdates()
.then(() => windowManager.ensureMainWindow());
},
}]),
]),
],
};
// Prepare menu items order
const appMenu = new Map([
["mac", macAppMenu],
["file", fileMenu],
["edit", editMenu],
["view", viewMenu],
["help", helpMenu],
]);
// Modify menu from extensions-api
for (const menuItem of electronMenuItems) {
if (!appMenu.has(menuItem.parentId)) {
logger.error(
`[MENU]: cannot register menu item for parentId=${menuItem.parentId}, parent item doesn't exist`,
{ menuItem },
);
continue;
}
appMenu.get(menuItem.parentId).submenu.push(menuItem);
}
if (!isMac) {
appMenu.delete("mac");
}
return [...appMenu.values()];
}
export function buildMenu(
windowManager: WindowManager,
electronMenuItems: MenuRegistration[],
applicationMenuItems: MenuItemOpts[],
) {
Menu.setApplicationMenu(
Menu.buildFromTemplate(getAppMenu(windowManager, electronMenuItems)),
Menu.buildFromTemplate(applicationMenuItems),
);
}