import { buildQueries, fuzzyMatches, makeNormalizer, matches, } from "./all-utils"; import type { TestInstance } from "../types"; import type { GetErrorFunction, Matcher, SelectorMatcherOptions, } from "./all-utils"; import type { waitForOptions } from "../wait-for"; export type QueryByText = ( instance: TestInstance, id: Matcher, options?: SelectorMatcherOptions, ) => T | null; export type GetByText = ( instance: TestInstance, id: Matcher, options?: SelectorMatcherOptions, ) => T; export type FindByText = ( instance: TestInstance, id: Matcher, options?: SelectorMatcherOptions, waitForElementOptions?: waitForOptions, ) => Promise; const queryByTextBase: QueryByText = ( instance, text, { exact = false, collapseWhitespace, trim, normalizer, stripAnsi } = {}, ) => { const matcher = exact ? matches : fuzzyMatches; const matchNormalizer = makeNormalizer({ stripAnsi, collapseWhitespace, trim, normalizer, }); const str = instance.stdoutArr.map((output) => output.contents).join("\n"); if (matcher(str, instance, text, matchNormalizer)) return instance; else return null; }; const getMissingError: GetErrorFunction<[unknown]> = (_c, text) => `Unable to find an stdout line with the text: ${text}. This could be because the text is broken up by multiple lines. In this case, you can provide a function for your text matcher to make your matcher more flexible.`; const [_queryByTextWithSuggestions, _getByText, _findByText] = buildQueries( queryByTextBase, getMissingError, ); export function getByText( ...args: Parameters> ): ReturnType> { return _getByText(...args); } export function queryByText( ...args: Parameters> ): ReturnType> { return _queryByTextWithSuggestions(...args); } export function findByText( ...args: Parameters> ): ReturnType> { return _findByText(...args); }