Skip to content

feat: RegExp support for *ByTestId queries #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/__tests__/getByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ test('getByTestId returns only native elements', () => {
'No instances found with testID: myComponent'
);
});

test('getAllByTestId supports a regex matcher', () => {
const { getAllByTestId } = render(
<View>
<Text testID="text">Text</Text>
<TextInput testID="textInput" />
<View testID="view" />
<Button testID="button" title="Button" onPress={jest.fn()} />
<MyComponent testID="myComponent" />
</View>
);

expect(getAllByTestId(/text/)).toHaveLength(2);
});
2 changes: 1 addition & 1 deletion src/helpers/findByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const findByTestId = (instance: ReactTestInstance) => (
) => makeFindQuery(instance, getByTestId, testId, waitForOptions);

export const findAllByTestId = (instance: ReactTestInstance) => (
testId: string,
testId: string | RegExp,
waitForOptions: WaitForOptions = {}
) => makeFindQuery(instance, getAllByTestId, testId, waitForOptions);

Expand Down
14 changes: 12 additions & 2 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ const getTextInputNodeByDisplayValue = (node, value) => {
}
};

const getNodeByTestId = (node, testID) => {
try {
return typeof testID === 'string'
? testID === node.props.testID
: testID.test(node.props.testID);
} catch (error) {
throw createLibraryNotSupportedError(error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use createLibraryNotSupportedError when importing something from react-native. No need for this try/catch here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, fair enough. Sorted

}
};

export const getByText = (instance: ReactTestInstance) =>
function getByTextFn(text: string | RegExp) {
try {
Expand Down Expand Up @@ -183,9 +193,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) =>
};

export const getAllByTestId = (instance: ReactTestInstance) =>
function getAllByTestIdFn(testID: string): ReactTestInstance[] {
function getAllByTestIdFn(testID: string | RegExp): ReactTestInstance[] {
const results = instance
.findAllByProps({ testID })
.findAll((node) => getNodeByTestId(node, testID))
.filter((element) => typeof element.type === 'string');

if (results.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/queryByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const queryAllByDisplayValue = (instance: ReactTestInstance) => (
};

export const queryAllByTestId = (instance: ReactTestInstance) => (
testID: string
testID: string | RegExp
) => {
try {
return getAllByTestId(instance)(testID);
Expand Down
4 changes: 3 additions & 1 deletion typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const getAllBy: ReactTestInstance[][] = [
tree.getAllByDisplayValue('my value'),
tree.getAllByDisplayValue(/value/g),
tree.getAllByTestId('test-id'),
tree.getAllByTestId(/value/g),
tree.getAllByA11yLabel('label'),
tree.getAllByLabelText('label'),
tree.getAllByA11yHint('label'),
Expand Down Expand Up @@ -111,6 +112,7 @@ const queryAllBy: ReactTestInstance[][] = [
tree.queryAllByDisplayValue('my value'),
tree.queryAllByDisplayValue(/value/g),
tree.queryAllByTestId('test-id'),
tree.queryAllByTestId(/value/),
tree.queryAllByA11yLabel('label'),
tree.queryAllByLabelText('label'),
tree.queryAllByA11yHint('label'),
Expand Down Expand Up @@ -177,7 +179,7 @@ const findAllBy: Promise<ReactTestInstance[]>[] = [
tree.findAllByDisplayValue(/View/g),
tree.findAllByDisplayValue(/View/g, { timeout: 10, interval: 10 }),
tree.findAllByTestId('test-id'),
tree.findAllByTestId('test-id', { timeout: 10, interval: 10 }),
tree.findAllByTestId(/value/, { timeout: 10, interval: 10 }),
tree.findAllByA11yLabel('label'),
tree.findAllByA11yLabel('label', { timeout: 10, interval: 10 }),
tree.findAllByLabelText('label'),
Expand Down
6 changes: 3 additions & 3 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface GetByAPI {
getByPlaceholderText: (placeholder: string | RegExp) => ReactTestInstance;
getByDisplayValue: (value: string | RegExp) => ReactTestInstance;
getByTestId: (testID: string) => ReactTestInstance;
getAllByTestId: (testID: string) => Array<ReactTestInstance>;
getAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance>;
getAllByText: (text: string | RegExp) => Array<ReactTestInstance>;
getAllByPlaceholderText: (
placeholder: string | RegExp
Expand Down Expand Up @@ -70,7 +70,7 @@ interface QueryByAPI {
) => ReactTestInstance | null;
queryByDisplayValue: (value: string | RegExp) => ReactTestInstance | null;
queryByTestId: (testID: string) => ReactTestInstance | null;
queryAllByTestId: (testID: string) => Array<ReactTestInstance> | [];
queryAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance> | [];
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance> | [];
queryAllByPlaceholderText: (
placeholder: string | RegExp
Expand Down Expand Up @@ -151,7 +151,7 @@ interface FindByAPI {
waitForOptions?: WaitForOptions
) => FindAllReturn;
findAllByTestId: (
testID: string,
testID: string | RegExp,
waitForOptions?: WaitForOptions
) => FindAllReturn;
}
Expand Down