mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make element discovery able to do nested discovery
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
e93ac55d1d
commit
18a3325977
@ -4,37 +4,45 @@
|
|||||||
*/
|
*/
|
||||||
import type { RenderResult } from "@testing-library/react";
|
import type { RenderResult } from "@testing-library/react";
|
||||||
|
|
||||||
|
type DiscoverySourceTypes = RenderResult | Element;
|
||||||
|
|
||||||
export const querySingleElement =
|
export const querySingleElement =
|
||||||
(attributeName: string, attributeValue: string) =>
|
(attributeName: string, attributeValue: string) =>
|
||||||
(rendered: RenderResult) => {
|
(source: DiscoverySourceTypes) => {
|
||||||
const dataAttribute = `data-${attributeName}-test`;
|
const dataAttribute = `data-${attributeName}-test`;
|
||||||
|
|
||||||
return rendered.baseElement.querySelector(
|
return getBaseElement(source).querySelector(
|
||||||
`[${dataAttribute}="${attributeValue}"]`,
|
`[${dataAttribute}="${attributeValue}"]`,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const queryAllElements =
|
export const queryAllElements =
|
||||||
(attributeName: string) => (rendered: RenderResult) => {
|
(attributeName: string) => (source: DiscoverySourceTypes) => {
|
||||||
const dataAttribute = `data-${attributeName}-test`;
|
const dataAttribute = `data-${attributeName}-test`;
|
||||||
|
|
||||||
const results = [...rendered.baseElement.querySelectorAll(`[${dataAttribute}]`)];
|
const results = [
|
||||||
|
...getBaseElement(source).querySelectorAll(`[${dataAttribute}]`),
|
||||||
|
];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
elements: results,
|
elements: results,
|
||||||
attributeValues: results.map(result => result.getAttribute(dataAttribute)),
|
|
||||||
|
attributeValues: results.map((result) =>
|
||||||
|
result.getAttribute(dataAttribute),
|
||||||
|
),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSingleElement =
|
export const getSingleElement =
|
||||||
(attributeName: string, attributeValue: string) =>
|
(attributeName: string, attributeValue: string) =>
|
||||||
(rendered: RenderResult) => {
|
(source: DiscoverySourceTypes) => {
|
||||||
const dataAttribute = `data-${attributeName}-test`;
|
const dataAttribute = `data-${attributeName}-test`;
|
||||||
|
|
||||||
const element = querySingleElement(attributeName, attributeValue)(rendered);
|
const element = querySingleElement(attributeName, attributeValue)(source);
|
||||||
|
|
||||||
if (!element) {
|
if (!element) {
|
||||||
const validValues = queryAllElements(attributeName)(rendered).attributeValues;
|
const validValues =
|
||||||
|
queryAllElements(attributeName)(source).attributeValues;
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Couldn't find HTML element with attribute "${dataAttribute}" with value "${attributeValue}". Valid values are:\n\n"${validValues.join(
|
`Couldn't find HTML element with attribute "${dataAttribute}" with value "${attributeValue}". Valid values are:\n\n"${validValues.join(
|
||||||
@ -45,3 +53,6 @@ export const getSingleElement =
|
|||||||
|
|
||||||
return element;
|
return element;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getBaseElement = (source: DiscoverySourceTypes) =>
|
||||||
|
"baseElement" in source ? source.baseElement : source;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user