1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/test-utils/findByTextWithMarkup.ts
Sebastian Malton c9f1594907 Upgrade XTermJs to v5 to get snapshot testing working
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-10 15:18:10 -05:00

35 lines
1.2 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { MatcherFunction, RenderResult, SelectorMatcherOptions, waitForOptions } from "@testing-library/react";
import { getDefaultNormalizer } from "@testing-library/react";
const hasTextFor = (text: string) => {
const normalize = getDefaultNormalizer();
return (
(node: HTMLElement | Element) => normalize(node.textContent ?? "") === text
);
};
export type FindByTextWithMarkup = (text: string, options?: SelectorMatcherOptions, waitForElementOptions?: waitForOptions) => Promise<void>;
export function findByTextWithMarkupFor(result: RenderResult): FindByTextWithMarkup {
return async (text, options, waitForOptions) => {
const hasText = hasTextFor(text);
const matcherFunction: MatcherFunction = (content, element): boolean => {
if (!element) {
return false;
}
const childrenDontHaveText = Array.from(element.children)
.every(child => !hasText(child));
return hasText(element) && childrenDontHaveText;
};
await result.findByText(matcherFunction, options, waitForOptions);
};
}