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
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "@k8slens/eslint-config/eslint",
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
"@k8slens/eslint-config/prettier"
|
||||
@@ -0,0 +1,13 @@
|
||||
export type {
|
||||
Discover,
|
||||
GetSingleElement,
|
||||
QueryAllElements,
|
||||
QuerySingleElement,
|
||||
} from "./src/discovery-of-html-elements";
|
||||
|
||||
export {
|
||||
discoverFor,
|
||||
getSingleElement,
|
||||
queryAllElements,
|
||||
querySingleElement,
|
||||
} from "./src/discovery-of-html-elements";
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "@k8slens/react-testing-library-discovery",
|
||||
"private": false,
|
||||
"version": "1.0.0-alpha.0",
|
||||
"description": "A way to discover HTML-elements using react-testing-library",
|
||||
"type": "commonjs",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/lensapp/lens.git"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"author": {
|
||||
"name": "OpenLens Authors",
|
||||
"email": "info@k8slens.dev"
|
||||
},
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/lensapp/lens",
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
"dev": "webpack --mode=development --watch",
|
||||
"lint": "lens-lint",
|
||||
"lint:fix": "lens-lint --fix"
|
||||
},
|
||||
"dependencies": {
|
||||
"@testing-library/dom": "^8.19.0",
|
||||
"@testing-library/jest-dom": "^5.16.5",
|
||||
"@testing-library/react": "^13.4.0"
|
||||
}
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
import type { RenderResult } from "@testing-library/react";
|
||||
import { prettyDOM as prettyDom } from "@testing-library/dom";
|
||||
|
||||
type DiscoverySourceTypes = RenderResult | Element;
|
||||
|
||||
export type QuerySingleElement = (
|
||||
attributeName: string,
|
||||
attributeValue?: string,
|
||||
) => { discovered: Element | null } & Discover;
|
||||
|
||||
type Clickable = { click: () => void };
|
||||
|
||||
export type GetSingleElement = (
|
||||
attributeName: string,
|
||||
attributeValue?: string,
|
||||
) => { discovered: Element } & Discover & Clickable;
|
||||
|
||||
export type QueryAllElements = (attributeName: string) => {
|
||||
discovered: Element[];
|
||||
attributeValues: (string | null)[];
|
||||
};
|
||||
|
||||
export interface Discover {
|
||||
querySingleElement: QuerySingleElement;
|
||||
queryAllElements: QueryAllElements;
|
||||
getSingleElement: GetSingleElement;
|
||||
}
|
||||
|
||||
const getBaseElement = (source: DiscoverySourceTypes) =>
|
||||
"baseElement" in source ? source.baseElement : source;
|
||||
|
||||
export function querySingleElement(getSource: () => DiscoverySourceTypes): QuerySingleElement {
|
||||
return (attributeName, attributeValue) => {
|
||||
const source = getSource();
|
||||
|
||||
const dataAttribute = `data-${attributeName}-test`;
|
||||
|
||||
const selector = attributeValue
|
||||
? `[${dataAttribute}="${attributeValue}"]`
|
||||
: `[${dataAttribute}]`;
|
||||
|
||||
const discovered = getBaseElement(source).querySelector(selector);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
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 function queryAllElements(getSource: () => DiscoverySourceTypes): QueryAllElements {
|
||||
return (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 function getSingleElement(getSource: () => DiscoverySourceTypes): GetSingleElement {
|
||||
return (attributeName, attributeValue) => {
|
||||
const dataAttribute = `data-${attributeName}-test`;
|
||||
|
||||
const { discovered, ...nestedDiscover } = querySingleElement(getSource)(
|
||||
attributeName,
|
||||
attributeValue,
|
||||
);
|
||||
|
||||
if (!discovered) {
|
||||
// eslint-disable-next-line xss/no-mixed-html
|
||||
const html = prettyDom(getBaseElement(getSource()));
|
||||
|
||||
if (attributeValue) {
|
||||
const validValues = queryAllElements(getSource)(attributeName).attributeValues;
|
||||
|
||||
throw new Error(
|
||||
`Couldn't find HTML-element with attribute "${dataAttribute}" with value "${attributeValue}".\n\nPresent values are:\n\n"${validValues.join(
|
||||
'",\n"',
|
||||
)}"\n\nHTML is:\n\n${html}`,
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Couldn't find HTML-element with attribute "${dataAttribute}"\n\nHTML is:\n\n${html}`,
|
||||
);
|
||||
}
|
||||
|
||||
const click = () => {
|
||||
if ("click" in discovered && typeof discovered.click === "function") {
|
||||
discovered.click();
|
||||
} else {
|
||||
throw new Error(
|
||||
`Tried to click something that was not clickable:\n\n${prettyDom(discovered)}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return { discovered, click, ...nestedDiscover };
|
||||
};
|
||||
}
|
||||
|
||||
export function discoverFor(getSource: () => DiscoverySourceTypes): Discover {
|
||||
return {
|
||||
querySingleElement: querySingleElement(getSource),
|
||||
queryAllElements: queryAllElements(getSource),
|
||||
getSingleElement: getSingleElement(getSource),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "@k8slens/typescript/config/base.json",
|
||||
"include": ["**/*.ts"]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/webpack").configForNode;
|
||||
Reference in new issue
Block a user