@testing-library/react-native
Version:
Simple and complete React Native testing utilities that encourage good testing practices.
64 lines (56 loc) • 1.96 kB
text/typescript
import type { ReactTestInstance } from 'react-test-renderer';
import { findAll } from '../helpers/findAll';
import { isHostText } from '../helpers/host-component-names';
import { matchTextContent } from '../helpers/matchers/matchTextContent';
import { TextMatch, TextMatchOptions } from '../matches';
import { makeQueries } from './makeQueries';
import type {
FindAllByQuery,
FindByQuery,
GetAllByQuery,
GetByQuery,
QueryAllByQuery,
QueryByQuery,
} from './makeQueries';
import type { CommonQueryOptions } from './options';
type ByTextOptions = CommonQueryOptions & TextMatchOptions;
const queryAllByText = (
instance: ReactTestInstance
): QueryAllByQuery<TextMatch, ByTextOptions> =>
function queryAllByTextFn(text, options = {}) {
return findAll(
instance,
(node) => isHostText(node) && matchTextContent(node, text, options),
{
...options,
matchDeepestOnly: true,
}
);
};
const getMultipleError = (text: TextMatch) =>
`Found multiple elements with text: ${String(text)}`;
const getMissingError = (text: TextMatch) =>
`Unable to find an element with text: ${String(text)}`;
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByText,
getMissingError,
getMultipleError
);
export type ByTextQueries = {
getByText: GetByQuery<TextMatch, ByTextOptions>;
getAllByText: GetAllByQuery<TextMatch, ByTextOptions>;
queryByText: QueryByQuery<TextMatch, ByTextOptions>;
queryAllByText: QueryAllByQuery<TextMatch, ByTextOptions>;
findByText: FindByQuery<TextMatch, ByTextOptions>;
findAllByText: FindAllByQuery<TextMatch, ByTextOptions>;
};
export const bindByTextQueries = (
instance: ReactTestInstance
): ByTextQueries => ({
getByText: getBy(instance),
getAllByText: getAllBy(instance),
queryByText: queryBy(instance),
queryAllByText: queryAllBy(instance),
findByText: findBy(instance),
findAllByText: findAllBy(instance),
});