mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make element discovery able to discover without value for attribute
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
18a3325977
commit
c581713ca1
@ -7,13 +7,13 @@ import type { RenderResult } from "@testing-library/react";
|
|||||||
type DiscoverySourceTypes = RenderResult | Element;
|
type DiscoverySourceTypes = RenderResult | Element;
|
||||||
|
|
||||||
export const querySingleElement =
|
export const querySingleElement =
|
||||||
(attributeName: string, attributeValue: string) =>
|
(attributeName: string, attributeValue?: string) =>
|
||||||
(source: DiscoverySourceTypes) => {
|
(source: DiscoverySourceTypes) => {
|
||||||
const dataAttribute = `data-${attributeName}-test`;
|
const dataAttribute = `data-${attributeName}-test`;
|
||||||
|
|
||||||
return getBaseElement(source).querySelector(
|
const selector = attributeValue ? `[${dataAttribute}="${attributeValue}"]` : `[${dataAttribute}]` ;
|
||||||
`[${dataAttribute}="${attributeValue}"]`,
|
|
||||||
);
|
return getBaseElement(source).querySelector(selector);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const queryAllElements =
|
export const queryAllElements =
|
||||||
@ -34,7 +34,7 @@ export const queryAllElements =
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getSingleElement =
|
export const getSingleElement =
|
||||||
(attributeName: string, attributeValue: string) =>
|
(attributeName: string, attributeValue?: string) =>
|
||||||
(source: DiscoverySourceTypes) => {
|
(source: DiscoverySourceTypes) => {
|
||||||
const dataAttribute = `data-${attributeName}-test`;
|
const dataAttribute = `data-${attributeName}-test`;
|
||||||
|
|
||||||
@ -44,11 +44,15 @@ export const getSingleElement =
|
|||||||
const validValues =
|
const validValues =
|
||||||
queryAllElements(attributeName)(source).attributeValues;
|
queryAllElements(attributeName)(source).attributeValues;
|
||||||
|
|
||||||
throw new Error(
|
if(attributeValue) {
|
||||||
`Couldn't find HTML element with attribute "${dataAttribute}" with value "${attributeValue}". Valid values are:\n\n"${validValues.join(
|
throw new Error(
|
||||||
'",\n"',
|
`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 element;
|
return element;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user