mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
34fc03a8c9
* refactor: extract method filterNodeByType * feat: use makeQueries for byText * feat: use makeQueries for byPlaceholderText * feat: use makeQueries for byDisplayValue * refactor: renamed method in queryAllByTestId * refactor: rename method makeQuery to makeA11yQuery the renaming is done to avoid any confusion with makeQueries which should be the new way to add new queries * test: add test for errors for byTestId * sort exports Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
152 lines
4.7 KiB
JavaScript
152 lines
4.7 KiB
JavaScript
// @flow
|
|
import * as React from 'react';
|
|
import {
|
|
UNSAFE_getByType,
|
|
UNSAFE_getByProps,
|
|
UNSAFE_getAllByType,
|
|
UNSAFE_getAllByProps,
|
|
} from './getByAPI';
|
|
import { queryByTestId, queryAllByTestId } from './byTestId';
|
|
import { queryByText, queryAllByText } from './byText';
|
|
import {
|
|
queryByPlaceholderText,
|
|
queryAllByPlaceholderText,
|
|
} from './byPlaceholderText';
|
|
import { queryByDisplayValue, queryAllByDisplayValue } from './byDisplayValue';
|
|
import {
|
|
createQueryByError,
|
|
throwRemovedFunctionError,
|
|
throwRenamedFunctionError,
|
|
} from './errors';
|
|
|
|
export type QueryByAPI = {|
|
|
queryByText: (name: string | RegExp) => ReactTestInstance | null,
|
|
queryByPlaceholderText: (
|
|
placeholder: string | RegExp
|
|
) => ReactTestInstance | null,
|
|
queryByDisplayValue: (value: string | RegExp) => ReactTestInstance | null,
|
|
queryByTestId: (testID: string | RegExp) => ReactTestInstance | null,
|
|
queryAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance>,
|
|
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance>,
|
|
queryAllByPlaceholderText: (
|
|
placeholder: string | RegExp
|
|
) => Array<ReactTestInstance>,
|
|
queryAllByDisplayValue: (value: string | RegExp) => Array<ReactTestInstance>,
|
|
|
|
// Unsafe aliases
|
|
UNSAFE_queryByType: <P>(
|
|
type: React.ComponentType<P>
|
|
) => ReactTestInstance | null,
|
|
UNSAFE_queryAllByType: <P>(
|
|
type: React.ComponentType<P>
|
|
) => Array<ReactTestInstance>,
|
|
UNSAFE_queryByProps: (props: { [string]: any }) => ReactTestInstance | null,
|
|
UNSAFE_queryAllByProps: (props: {
|
|
[string]: any,
|
|
}) => Array<ReactTestInstance>,
|
|
queryByName: () => void,
|
|
queryByType: () => void,
|
|
queryByProps: () => void,
|
|
queryAllByName: () => void,
|
|
queryAllByType: () => void,
|
|
queryAllByProps: () => void,
|
|
|
|
queryByPlaceholder: () => void,
|
|
queryAllByPlaceholder: () => void,
|
|
|};
|
|
|
|
export const UNSAFE_queryByType = (
|
|
instance: ReactTestInstance
|
|
): ((type: React.ComponentType<any>) => ReactTestInstance | null) =>
|
|
function queryByTypeFn(type: React.ComponentType<any>) {
|
|
try {
|
|
return UNSAFE_getByType(instance)(type);
|
|
} catch (error) {
|
|
return createQueryByError(error, queryByTypeFn);
|
|
}
|
|
};
|
|
|
|
export const UNSAFE_queryByProps = (
|
|
instance: ReactTestInstance
|
|
): ((props: { [propName: string]: any }) => ReactTestInstance | null) =>
|
|
function queryByPropsFn(props: { [propName: string]: any }) {
|
|
try {
|
|
return UNSAFE_getByProps(instance)(props);
|
|
} catch (error) {
|
|
return createQueryByError(error, queryByPropsFn);
|
|
}
|
|
};
|
|
|
|
export const UNSAFE_queryAllByType = (
|
|
instance: ReactTestInstance
|
|
): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) => (
|
|
type: React.ComponentType<any>
|
|
) => {
|
|
try {
|
|
return UNSAFE_getAllByType(instance)(type);
|
|
} catch (error) {
|
|
return [];
|
|
}
|
|
};
|
|
|
|
export const UNSAFE_queryAllByProps = (
|
|
instance: ReactTestInstance
|
|
): ((props: {
|
|
[propName: string]: any,
|
|
}) => Array<ReactTestInstance>) => (props: { [propName: string]: any }) => {
|
|
try {
|
|
return UNSAFE_getAllByProps(instance)(props);
|
|
} catch (error) {
|
|
return [];
|
|
}
|
|
};
|
|
|
|
export const queryByAPI = (instance: ReactTestInstance): QueryByAPI => ({
|
|
queryByTestId: queryByTestId(instance),
|
|
queryByText: queryByText(instance),
|
|
queryByPlaceholderText: queryByPlaceholderText(instance),
|
|
queryByDisplayValue: queryByDisplayValue(instance),
|
|
queryAllByTestId: queryAllByTestId(instance),
|
|
queryAllByText: queryAllByText(instance),
|
|
queryAllByPlaceholderText: queryAllByPlaceholderText(instance),
|
|
queryAllByDisplayValue: queryAllByDisplayValue(instance),
|
|
|
|
// Unsafe
|
|
UNSAFE_queryByType: UNSAFE_queryByType(instance),
|
|
UNSAFE_queryAllByType: UNSAFE_queryAllByType(instance),
|
|
UNSAFE_queryByProps: UNSAFE_queryByProps(instance),
|
|
UNSAFE_queryAllByProps: UNSAFE_queryAllByProps(instance),
|
|
|
|
// Removed
|
|
queryByName: () =>
|
|
throwRemovedFunctionError('queryByName', 'migration-v2#removed-functions'),
|
|
queryAllByName: () =>
|
|
throwRemovedFunctionError(
|
|
'queryAllByName',
|
|
'migration-v2#removed-functions'
|
|
),
|
|
queryByType: () =>
|
|
throwRemovedFunctionError('queryByType', 'migration-v2#removed-functions'),
|
|
queryAllByType: () =>
|
|
throwRemovedFunctionError(
|
|
'queryAllByType',
|
|
'migration-v2#removed-functions'
|
|
),
|
|
queryByProps: () =>
|
|
throwRemovedFunctionError('queryByProps', 'migration-v2#removed-functions'),
|
|
queryAllByProps: () =>
|
|
throwRemovedFunctionError(
|
|
'queryAllByProps',
|
|
'migration-v2#removed-functions'
|
|
),
|
|
|
|
// Renamed
|
|
queryByPlaceholder: () =>
|
|
throwRenamedFunctionError('queryByPlaceholder', 'queryByPlaceholderText'),
|
|
queryAllByPlaceholder: () =>
|
|
throwRenamedFunctionError(
|
|
'queryAllByPlaceholder',
|
|
'queryAllByPlaceholderText'
|
|
),
|
|
});
|