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

@@ -6,6 +6,12 @@ import { getInjectable } from "@ogre-tools/injectable";
import attemptInstallByInfoInjectable from "../../components/+extensions/attempt-install-by-info/attempt-install-by-info.injectable";
import { bindProtocolAddRouteHandlers } from "./bind-protocol-add-route-handlers";
import lensProtocolRouterRendererInjectable from "../lens-protocol-router-renderer/lens-protocol-router-renderer.injectable";
import navigateToCatalogInjectable from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import navigateToAddClusterInjectable from "../../../common/front-end-routing/routes/add-cluster/navigate-to-add-cluster.injectable";
import navigateToExtensionsInjectable from "../../../common/front-end-routing/routes/extensions/navigate-to-extensions.injectable";
import navigateToEntitySettingsInjectable from "../../../common/front-end-routing/routes/entity-settings/navigate-to-entity-settings.injectable";
import navigateToClusterViewInjectable from "../../../common/front-end-routing/routes/cluster-view/navigate-to-cluster-view.injectable";
import navigateToPreferenceTabIdInjectable from "./navigate-to-preference-tab-id.injectable";
const bindProtocolAddRouteHandlersInjectable = getInjectable({
id: "bind-protocol-add-route-handlers",
@@ -13,9 +19,17 @@ const bindProtocolAddRouteHandlersInjectable = getInjectable({
instantiate: (di) =>
bindProtocolAddRouteHandlers({
attemptInstallByInfo: di.inject(attemptInstallByInfoInjectable),
lensProtocolRouterRenderer: di.inject(
lensProtocolRouterRendererInjectable,
),
navigateToCatalog: di.inject(navigateToCatalogInjectable),
navigateToAddCluster: di.inject(navigateToAddClusterInjectable),
navigateToExtensions: di.inject(navigateToExtensionsInjectable),
navigateToEntitySettings: di.inject(navigateToEntitySettingsInjectable),
navigateToClusterView: di.inject(navigateToClusterViewInjectable),
navigateToPreferenceTabId: di.inject(navigateToPreferenceTabIdInjectable),
}),
});
@@ -5,7 +5,6 @@
import React from "react";
import type { LensProtocolRouterRenderer } from "../lens-protocol-router-renderer/lens-protocol-router-renderer";
import { navigate } from "../../navigation/helpers";
import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
import { ClusterStore } from "../../../common/cluster-store/cluster-store";
import {
@@ -14,20 +13,28 @@ import {
LensProtocolRouter,
} from "../../../common/protocol-handler";
import { Notifications } from "../../components/notifications";
import * as routes from "../../../common/routes";
import type { ExtensionInfo } from "../../components/+extensions/attempt-install-by-info/attempt-install-by-info";
import type { NavigateToCatalog } from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import type { NavigateToEntitySettings } from "../../../common/front-end-routing/routes/entity-settings/navigate-to-entity-settings.injectable";
import type { NavigateToClusterView } from "../../../common/front-end-routing/routes/cluster-view/navigate-to-cluster-view.injectable";
interface Dependencies {
attemptInstallByInfo: (extensionInfo: ExtensionInfo) => Promise<void>;
lensProtocolRouterRenderer: LensProtocolRouterRenderer;
navigateToCatalog: NavigateToCatalog;
navigateToAddCluster: () => void;
navigateToExtensions: () => void;
navigateToEntitySettings: NavigateToEntitySettings;
navigateToClusterView: NavigateToClusterView;
navigateToPreferenceTabId: (tabId: string) => void;
}
export const bindProtocolAddRouteHandlers =
({ attemptInstallByInfo, lensProtocolRouterRenderer }: Dependencies) =>
({ attemptInstallByInfo, lensProtocolRouterRenderer, navigateToCatalog, navigateToAddCluster, navigateToExtensions, navigateToEntitySettings, navigateToClusterView, navigateToPreferenceTabId }: Dependencies) =>
() => {
lensProtocolRouterRenderer
.addInternalHandler("/preferences", ({ search: { highlight }}) => {
navigate(routes.preferencesURL({ fragment: highlight }));
.addInternalHandler("/preferences", ({ search: { highlight: tabId }}) => {
navigateToPreferenceTabId(tabId);
})
.addInternalHandler("/", ({ tail }) => {
if (tail) {
@@ -39,26 +46,19 @@ export const bindProtocolAddRouteHandlers =
);
}
navigate(routes.catalogURL());
navigateToCatalog();
})
.addInternalHandler("/landing", () => {
navigate(routes.catalogURL());
navigateToCatalog();
})
.addInternalHandler(
"/landing/view/:group/:kind",
({ pathname: { group, kind }}) => {
navigate(
routes.catalogURL({
params: {
group,
kind,
},
}),
);
navigateToCatalog({ group, kind });
},
)
.addInternalHandler("/cluster", () => {
navigate(routes.addClusterURL());
navigateToAddCluster();
})
.addInternalHandler(
"/entity/:entityId/settings",
@@ -66,7 +66,7 @@ export const bindProtocolAddRouteHandlers =
const entity = catalogEntityRegistry.getById(entityId);
if (entity) {
navigate(routes.entitySettingsURL({ params: { entityId }}));
navigateToEntitySettings(entityId);
} else {
Notifications.shortInfo(
<p>
@@ -83,7 +83,7 @@ export const bindProtocolAddRouteHandlers =
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
navigate(routes.clusterViewURL({ params: { clusterId }}));
navigateToClusterView(clusterId);
} else {
Notifications.shortInfo(
<p>
@@ -99,9 +99,7 @@ export const bindProtocolAddRouteHandlers =
const cluster = ClusterStore.getInstance().getById(clusterId);
if (cluster) {
navigate(
routes.entitySettingsURL({ params: { entityId: clusterId }}),
);
navigateToEntitySettings(clusterId);
} else {
Notifications.shortInfo(
<p>
@@ -112,7 +110,7 @@ export const bindProtocolAddRouteHandlers =
},
)
.addInternalHandler("/extensions", () => {
navigate(routes.extensionsURL());
navigateToExtensions();
})
.addInternalHandler(
`/extensions/install${LensProtocolRouter.ExtensionUrlSchema}`,
@@ -124,7 +122,7 @@ export const bindProtocolAddRouteHandlers =
.filter(Boolean)
.join("/");
navigate(routes.extensionsURL());
navigateToExtensions();
attemptInstallByInfo({ name, version, requireConfirmation: true });
},
);
@@ -0,0 +1,30 @@
/**
* 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 preferenceNavigationItemsInjectable from "../../components/+preferences/preferences-navigation/preference-navigation-items.injectable";
import navigateToPreferencesInjectable from "../../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
const navigateToPreferenceTabIdInjectable = getInjectable({
id: "navigate-to-preference-tab-id",
instantiate: (di) => {
const preferenceNavigationItems = di.inject(preferenceNavigationItemsInjectable);
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
return (tabId: string) => {
const targetTab = preferenceNavigationItems.get().find(item => item.id === tabId);
if (targetTab) {
targetTab.navigate();
return;
}
navigateToPreferences();
};
},
});
export default navigateToPreferenceTabIdInjectable;