Skip to content
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

feat: RegExp support for *ByTestId queries #535

Merged
merged 6 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions src/__tests__/getByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ test('getByTestId returns only native elements', () => {
'No instances found with testID: myComponent'
);
});

test('supports a regex matcher', () => {
const { getByTestId, 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(getByTestId(/view/)).toBeTruthy();
expect(getAllByTestId(/text/)).toHaveLength(2);
thymikee marked this conversation as resolved.
Show resolved Hide resolved
});
4 changes: 2 additions & 2 deletions src/helpers/findByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const makeFindQuery = <Text, Result>(
): Promise<Result> => waitFor(() => getQuery(instance)(text), waitForOptions);

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

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

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

const getNodeByTestId = (node, testID) => {
return typeof testID === 'string'
? testID === node.props.testID
: testID.test(node.props.testID);
};

export const getByText = (instance: ReactTestInstance) =>
function getByTextFn(text: string | RegExp) {
try {
Expand Down Expand Up @@ -124,7 +130,7 @@ export const getByDisplayValue = (instance: ReactTestInstance) =>
};

export const getByTestId = (instance: ReactTestInstance) =>
function getByTestIdFn(testID: string) {
function getByTestIdFn(testID: string | RegExp) {
try {
const results = getAllByTestId(instance)(testID);
if (results.length === 1) {
Expand Down Expand Up @@ -183,9 +189,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
4 changes: 2 additions & 2 deletions src/helpers/queryByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const queryByDisplayValue = (instance: ReactTestInstance) =>
};

export const queryByTestId = (instance: ReactTestInstance) =>
function queryByTestIdFn(testID: string) {
function queryByTestIdFn(testID: string | RegExp) {
try {
return getByTestId(instance)(testID);
} catch (error) {
Expand Down 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
8 changes: 6 additions & 2 deletions typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const getBy: ReactTestInstance[] = [
tree.getByDisplayValue('my value'),
tree.getByDisplayValue(/value/g),
tree.getByTestId('test-id'),
tree.getByTestId(/test-id/),
tree.getByA11yLabel('label'),
tree.getByLabelText('label'),
tree.getByA11yHint('label'),
Expand All @@ -64,6 +65,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 All @@ -88,6 +90,7 @@ const queryBy: Array<ReactTestInstance | null> = [
tree.queryByDisplayValue('my value'),
tree.queryByDisplayValue(/value/g),
tree.queryByTestId('test-id'),
tree.queryByTestId(/test-id/),
tree.queryByA11yHint('label'),
tree.queryByHintText('label'),
tree.queryByA11yLabel('label'),
Expand All @@ -111,6 +114,7 @@ const queryAllBy: ReactTestInstance[][] = [
tree.queryAllByDisplayValue('my value'),
tree.queryAllByDisplayValue(/value/g),
tree.queryAllByTestId('test-id'),
tree.queryAllByTestId(/test-id/),
tree.queryAllByA11yLabel('label'),
tree.queryAllByLabelText('label'),
tree.queryAllByA11yHint('label'),
Expand Down Expand Up @@ -141,7 +145,7 @@ const findBy: Promise<ReactTestInstance>[] = [
tree.findByDisplayValue(/value/g),
tree.findByDisplayValue(/value/g, { timeout: 10, interval: 10 }),
tree.findByTestId('test-id'),
tree.findByTestId('test-id', { timeout: 10, interval: 10 }),
tree.findByTestId(/test-id/, { timeout: 10, interval: 10 }),
tree.findByA11yLabel('label'),
tree.findByA11yLabel('label', { timeout: 10, interval: 10 }),
tree.findByLabelText('label'),
Expand Down Expand Up @@ -177,7 +181,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(/test-id/, { timeout: 10, interval: 10 }),
tree.findAllByA11yLabel('label'),
tree.findAllByA11yLabel('label', { timeout: 10, interval: 10 }),
tree.findAllByLabelText('label'),
Expand Down
12 changes: 6 additions & 6 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ interface GetByAPI {
getByText: (text: string | RegExp) => ReactTestInstance;
getByPlaceholderText: (placeholder: string | RegExp) => ReactTestInstance;
getByDisplayValue: (value: string | RegExp) => ReactTestInstance;
getByTestId: (testID: string) => ReactTestInstance;
getAllByTestId: (testID: string) => Array<ReactTestInstance>;
getByTestId: (testID: string | RegExp) => ReactTestInstance;
getAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance>;
getAllByText: (text: string | RegExp) => Array<ReactTestInstance>;
getAllByPlaceholderText: (
placeholder: string | RegExp
Expand Down Expand Up @@ -69,8 +69,8 @@ interface QueryByAPI {
placeholder: string | RegExp
) => ReactTestInstance | null;
queryByDisplayValue: (value: string | RegExp) => ReactTestInstance | null;
queryByTestId: (testID: string) => ReactTestInstance | null;
queryAllByTestId: (testID: string) => Array<ReactTestInstance> | [];
queryByTestId: (testID: string | RegExp) => ReactTestInstance | null;
queryAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance> | [];
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance> | [];
queryAllByPlaceholderText: (
placeholder: string | RegExp
Expand Down Expand Up @@ -137,7 +137,7 @@ interface FindByAPI {
value: string | RegExp,
waitForOptions?: WaitForOptions
) => FindReturn;
findByTestId: (testID: string, waitForOptions?: WaitForOptions) => FindReturn;
findByTestId: (testID: string | RegExp, waitForOptions?: WaitForOptions) => FindReturn;
findAllByText: (
text: string | RegExp,
waitForOptions?: WaitForOptions
Expand All @@ -151,7 +151,7 @@ interface FindByAPI {
waitForOptions?: WaitForOptions
) => FindAllReturn;
findAllByTestId: (
testID: string,
testID: string | RegExp,
waitForOptions?: WaitForOptions
) => FindAllReturn;
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/Queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const element = getByDisplayValue('username');

> getByTestId, getAllByTestId, queryByTestId, queryAllByTestId, findByTestId, findAllByTestId

Returns a `ReactTestInstance` with matching `testID` prop.
Returns a `ReactTestInstance` with matching `testID` prop. `testID` – may be a string or a regular expression.

```jsx
import { render } from '@testing-library/react-native';
Expand Down