1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Make queries in element discovery return matching attribute values for easier testing

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-10-18 11:10:39 +03:00 committed by Janne Savolainen
parent 14ab27288a
commit e93ac55d1d
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A

View File

@ -18,7 +18,12 @@ export const queryAllElements =
(attributeName: string) => (rendered: RenderResult) => {
const dataAttribute = `data-${attributeName}-test`;
return [...rendered.baseElement.querySelectorAll(`[${dataAttribute}]`)];
const results = [...rendered.baseElement.querySelectorAll(`[${dataAttribute}]`)];
return {
elements: results,
attributeValues: results.map(result => result.getAttribute(dataAttribute)),
};
};
export const getSingleElement =
@ -29,9 +34,7 @@ export const getSingleElement =
const element = querySingleElement(attributeName, attributeValue)(rendered);
if (!element) {
const validValues = queryAllElements(attributeName)(rendered).map(
(elem) => elem.getAttribute(dataAttribute),
);
const validValues = queryAllElements(attributeName)(rendered).attributeValues;
throw new Error(
`Couldn't find HTML element with attribute "${dataAttribute}" with value "${attributeValue}". Valid values are:\n\n"${validValues.join(