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-tursoandJanne Savolainen authored and Janne Savolainen committed 2022-10-21 08:23:18 +03:00
1 parent 14ab27288a
commit e93ac55d1d
1 file changed
+7 -4
@@ -18,7 +18,12 @@ export const queryAllElements =
(attributeName: string) => (rendered: RenderResult) => { (attributeName: string) => (rendered: RenderResult) => {
const dataAttribute = `data-${attributeName}-test`; 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 = export const getSingleElement =
@@ -29,9 +34,7 @@ export const getSingleElement =
const element = querySingleElement(attributeName, attributeValue)(rendered); const element = querySingleElement(attributeName, attributeValue)(rendered);
if (!element) { if (!element) {
const validValues = queryAllElements(attributeName)(rendered).map( const validValues = queryAllElements(attributeName)(rendered).attributeValues;
(elem) => elem.getAttribute(dataAttribute),
);
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(