Skip to content

Commit

Permalink
✨ Added a utility function to ensure element existence with querySele…
Browse files Browse the repository at this point in the history
…ctor/querySelectorAll

Closes #5038
  • Loading branch information
Balaji Sridharan committed Aug 19, 2024
1 parent f735e60 commit df7a973
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ export const gotoNextView = (container: Element) => {
)!;
fireEvent.click(nextButton);
};

export const safeQuerySelector = <T extends HTMLElement = HTMLElement>(
container: HTMLElement,
selector: string,
): T => {
const element = container.querySelector(selector);
if (element) {
return element as T;
}

throw new Error(`Element with selector '${selector}' not found`);
};

export const safeQuerySelectorAll = <T extends HTMLElement = HTMLElement>(
container: HTMLElement,
selector: string,
): T[] => {
const elements = Array.from(container.querySelectorAll(selector)) as T[];
if (elements.length) {
return elements;
}

throw new Error(`Element with selector '${selector}' not found`);
};

0 comments on commit df7a973

Please sign in to comment.