mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Extract React Application as a Feature (#7441)
* Fix webpack config for react Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Introduce package for discovering html elements in unit tests Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Switch to using discovery of html elements from package Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Introduce competition for starting react application inside the Feature Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Move stuff in application start to earlier timeslot having no real need to be done so late Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Switch to using react application root feature being more friendly to extending Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Switch to using more familiar pattern of higher order components for wrapping react application Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Adapt to more familiar pattern for higher order components Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Rename feature for clarity Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> --------- Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
5827fc9d9a
commit
b5e564271e
50 files changed
+968
-172
No files matched your search
@@ -1,115 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { RenderResult } from "@testing-library/react";
|
||||
|
||||
type DiscoverySourceTypes = RenderResult | Element;
|
||||
|
||||
export type QuerySingleElement = (
|
||||
attributeName: string,
|
||||
attributeValue?: string
|
||||
) => { discovered: Element | null } & Discover;
|
||||
|
||||
export type GetSingleElement = (
|
||||
attributeName: string,
|
||||
attributeValue?: string
|
||||
) => { discovered: Element } & Discover;
|
||||
|
||||
export type QueryAllElements = (attributeName: string) => {
|
||||
discovered: Element[];
|
||||
attributeValues: (string | null)[];
|
||||
};
|
||||
|
||||
export interface Discover {
|
||||
querySingleElement: QuerySingleElement;
|
||||
queryAllElements: QueryAllElements;
|
||||
getSingleElement: GetSingleElement;
|
||||
}
|
||||
|
||||
export const discoverFor = (getSource: () => DiscoverySourceTypes): Discover => ({
|
||||
querySingleElement: querySingleElement(getSource),
|
||||
queryAllElements: queryAllElements(getSource),
|
||||
getSingleElement: getSingleElement(getSource),
|
||||
});
|
||||
|
||||
export const querySingleElement =
|
||||
(getSource: () => DiscoverySourceTypes): QuerySingleElement =>
|
||||
(attributeName, attributeValue) => {
|
||||
const source = getSource();
|
||||
|
||||
const dataAttribute = `data-${attributeName}-test`;
|
||||
|
||||
const selector = attributeValue
|
||||
? `[${dataAttribute}="${attributeValue}"]`
|
||||
: `[${dataAttribute}]`;
|
||||
|
||||
const discovered = getBaseElement(source).querySelector(selector);
|
||||
|
||||
const nestedDiscover = discoverFor(() => {
|
||||
if (!discovered) {
|
||||
throw new Error("Tried to do nested discover using source that does not exist");
|
||||
}
|
||||
|
||||
return discovered;
|
||||
});
|
||||
|
||||
return {
|
||||
discovered,
|
||||
|
||||
...nestedDiscover,
|
||||
};
|
||||
};
|
||||
|
||||
export const queryAllElements =
|
||||
(getSource: () => DiscoverySourceTypes): QueryAllElements =>
|
||||
(attributeName) => {
|
||||
const source = getSource();
|
||||
|
||||
const dataAttribute = `data-${attributeName}-test`;
|
||||
|
||||
const results = [
|
||||
...getBaseElement(source).querySelectorAll(`[${dataAttribute}]`),
|
||||
];
|
||||
|
||||
return {
|
||||
discovered: results,
|
||||
|
||||
attributeValues: results.map((result) =>
|
||||
result.getAttribute(dataAttribute),
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
export const getSingleElement =
|
||||
(getSource: () => DiscoverySourceTypes): GetSingleElement =>
|
||||
(attributeName, attributeValue) => {
|
||||
const dataAttribute = `data-${attributeName}-test`;
|
||||
|
||||
const { discovered, ...nestedDiscover } = querySingleElement(getSource)(
|
||||
attributeName,
|
||||
attributeValue,
|
||||
);
|
||||
|
||||
if (!discovered) {
|
||||
const validValues =
|
||||
queryAllElements(getSource)(attributeName).attributeValues;
|
||||
|
||||
if (attributeValue) {
|
||||
throw new Error(
|
||||
`Couldn't find HTML-element with attribute "${dataAttribute}" with value "${attributeValue}". Present values are:\n\n"${validValues.join(
|
||||
'",\n"',
|
||||
)}"`,
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Couldn't find HTML-element with attribute "${dataAttribute}"`,
|
||||
);
|
||||
}
|
||||
|
||||
return { discovered, ...nestedDiscover };
|
||||
};
|
||||
|
||||
const getBaseElement = (source: DiscoverySourceTypes) =>
|
||||
"baseElement" in source ? source.baseElement : source;
|
||||
@@ -59,7 +59,7 @@ import { Namespace } from "../../../common/k8s-api/endpoints";
|
||||
import { getOverrideFsWithFakes } from "../../../test-utils/override-fs-with-fakes";
|
||||
import applicationMenuItemCompositeInjectable from "../../../features/application-menu/main/application-menu-item-composite.injectable";
|
||||
import { getCompositePaths } from "../../../common/utils/composite/get-composite-paths/get-composite-paths";
|
||||
import { discoverFor } from "./discovery-of-html-elements";
|
||||
import { discoverFor } from "@k8slens/react-testing-library-discovery";
|
||||
import { findComposite } from "../../../common/utils/composite/find-composite/find-composite";
|
||||
import shouldStartHiddenInjectable from "../../../main/electron-app/features/should-start-hidden.injectable";
|
||||
import fsInjectable from "../../../common/fs/fs.injectable";
|
||||
|
||||
Reference in new issue
Block a user