Skip to content

Commit 6a65b76

Browse files
committed
feat: add getAllByTestId and queryAllByTestId queries
1 parent e5ce263 commit 6a65b76

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/helpers/getByAPI.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,26 @@ export const getAllByProps = (instance: ReactTestInstance) =>
173173
return results;
174174
};
175175

176+
export const getAllByTestId = (instance: ReactTestInstance) =>
177+
function getAllByTestIdFn(testID: string) {
178+
const results = instance.findAllByProps({ testID });
179+
if (results.length === 0) {
180+
throw new ErrorWithStack(
181+
`No instances found with testID: ${String(testID)}`,
182+
getAllByTestIdFn
183+
);
184+
}
185+
return results;
186+
};
187+
176188
export const getByAPI = (instance: ReactTestInstance) => ({
177189
getByTestId: getByTestId(instance),
178190
getByName: getByName(instance),
179191
getByType: getByType(instance),
180192
getByText: getByText(instance),
181193
getByPlaceholder: getByPlaceholder(instance),
182194
getByProps: getByProps(instance),
195+
getAllByTestId: getAllByTestId(instance),
183196
getAllByName: getAllByName(instance),
184197
getAllByType: getAllByType(instance),
185198
getAllByText: getAllByText(instance),

src/helpers/queryByAPI.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getByText,
88
getByPlaceholder,
99
getByProps,
10+
getAllByTestId,
1011
getAllByName,
1112
getAllByType,
1213
getAllByText,
@@ -121,13 +122,24 @@ export const queryAllByProps = (instance: ReactTestInstance) => (props: {
121122
}
122123
};
123124

125+
export const queryAllByTestId = (instance: ReactTestInstance) => (
126+
testID: string
127+
) => {
128+
try {
129+
return getAllByTestId(instance)(testID);
130+
} catch (error) {
131+
return [];
132+
}
133+
};
134+
124135
export const queryByAPI = (instance: ReactTestInstance) => ({
125136
queryByTestId: queryByTestId(instance),
126137
queryByName: queryByName(instance),
127138
queryByType: queryByType(instance),
128139
queryByText: queryByText(instance),
129140
queryByPlaceholder: queryByPlaceholder(instance),
130141
queryByProps: queryByProps(instance),
142+
queryAllByTestId: queryAllByTestId(instance),
131143
queryAllByName: queryAllByName(instance),
132144
queryAllByType: queryAllByType(instance),
133145
queryAllByText: queryAllByText(instance),

0 commit comments

Comments
 (0)