mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
feat: add getAllByTestId and queryAllByTestId queries (#198)
* feat: add getAllByTestId and queryAllByTestId queries * Add test for getAllByTestId and queryAllByTestId - Filter out intermediate components in getAllByTestId. testIDs may be passed down to child components and appearing multiple times in the render tree. Some React Native components render multiple anyways. <Text> is an example. * Update TypeScript typings * Fix flow types * Update TS typings test
This commit is contained in:
committed by
Michał Pierzchała
parent
9369efee73
commit
fead652013
@@ -212,6 +212,21 @@ export const getAllByProps = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const getAllByTestId = (instance: ReactTestInstance) =>
|
||||
function getAllByTestIdFn(testID: string): ReactTestInstance[] {
|
||||
const results = instance
|
||||
.findAllByProps({ testID })
|
||||
.filter(element => typeof element.type === 'string');
|
||||
|
||||
if (results.length === 0) {
|
||||
throw new ErrorWithStack(
|
||||
`No instances found with testID: ${String(testID)}`,
|
||||
getAllByTestIdFn
|
||||
);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
export const getByAPI = (instance: ReactTestInstance) => ({
|
||||
getByTestId: getByTestId(instance),
|
||||
getByName: getByName(instance),
|
||||
@@ -220,6 +235,7 @@ export const getByAPI = (instance: ReactTestInstance) => ({
|
||||
getByPlaceholder: getByPlaceholder(instance),
|
||||
getByDisplayValue: getByDisplayValue(instance),
|
||||
getByProps: getByProps(instance),
|
||||
getAllByTestId: getAllByTestId(instance),
|
||||
getAllByName: getAllByName(instance),
|
||||
getAllByType: getAllByType(instance),
|
||||
getAllByText: getAllByText(instance),
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
getByPlaceholder,
|
||||
getByDisplayValue,
|
||||
getByProps,
|
||||
getAllByTestId,
|
||||
getAllByName,
|
||||
getAllByType,
|
||||
getAllByText,
|
||||
@@ -142,6 +143,16 @@ export const queryAllByProps = (instance: ReactTestInstance) => (props: {
|
||||
}
|
||||
};
|
||||
|
||||
export const queryAllByTestId = (instance: ReactTestInstance) => (
|
||||
testID: string
|
||||
) => {
|
||||
try {
|
||||
return getAllByTestId(instance)(testID);
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const queryByAPI = (instance: ReactTestInstance) => ({
|
||||
queryByTestId: queryByTestId(instance),
|
||||
queryByName: queryByName(instance),
|
||||
@@ -150,6 +161,7 @@ export const queryByAPI = (instance: ReactTestInstance) => ({
|
||||
queryByPlaceholder: queryByPlaceholder(instance),
|
||||
queryByDisplayValue: queryByDisplayValue(instance),
|
||||
queryByProps: queryByProps(instance),
|
||||
queryAllByTestId: queryAllByTestId(instance),
|
||||
queryAllByName: queryAllByName(instance),
|
||||
queryAllByType: queryAllByType(instance),
|
||||
queryAllByText: queryAllByText(instance),
|
||||
|
||||
Reference in New Issue
Block a user