Skip to content

feat: ...byRole type queries accepts second arg #875

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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@testing-library/react-native",
"version": "9.0.0",
"version": "9.0.0-alpha.0",
"description": "Simple and complete React Native testing utilities that encourage good testing practices.",
"main": "build/index.js",
"typings": "./typings/index.d.ts",
Expand Down
76 changes: 72 additions & 4 deletions src/__tests__/a11yAPI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const BUTTON_LABEL = 'cool button';
const BUTTON_HINT = 'click this button';
const TEXT_LABEL = 'cool text';
const TEXT_HINT = 'static text';
const ONE_OCCURANCE = 'more words';
const TWO_OCCURANCE = 'cooler text';
// Little hack to make all the methods happy with type
const NO_MATCHES_TEXT: any = 'not-existent-element';
const FOUND_TWO_INSTANCES = 'Expected 1 but found 2 instances';
Expand Down Expand Up @@ -70,6 +72,22 @@ function Section() {
);
}

function ButtonsWithText() {
return (
<>
<TouchableOpacity accessibilityRole="button">
<Text>{TWO_OCCURANCE}</Text>
</TouchableOpacity>
<TouchableOpacity accessibilityRole="button">
<Text>{TWO_OCCURANCE}</Text>
</TouchableOpacity>
<TouchableOpacity accessibilityRole="button">
<Text>{ONE_OCCURANCE}</Text>
</TouchableOpacity>
</>
);
}

test('getByA11yLabel, queryByA11yLabel, findByA11yLabel', async () => {
const { getByA11yLabel, queryByA11yLabel, findByA11yLabel } = render(
<Section />
Expand Down Expand Up @@ -186,12 +204,40 @@ test('getByA11yRole, queryByA11yRole, findByA11yRole', async () => {

const asyncButton = await findByA11yRole('button');
expect(asyncButton.props.accessibilityRole).toEqual('button');
await expect(findByA11yRole(NO_MATCHES_TEXT, waitForOptions)).rejects.toThrow(
getNoInstancesFoundMessage('accessibilityRole')
);
await expect(
findByA11yRole(NO_MATCHES_TEXT, {}, waitForOptions)
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole'));
await expect(findByA11yRole('link')).rejects.toThrow(FOUND_TWO_INSTANCES);
});

test('getByA11yRole, queryByA11yRole, findByA11yRole with name', async () => {
const { getByA11yRole, queryByA11yRole, findByA11yRole } = render(
<ButtonsWithText />
);

expect(() => getByA11yRole('button', { name: TWO_OCCURANCE })).toThrow(
FOUND_TWO_INSTANCES
);
getByA11yRole('button', { name: ONE_OCCURANCE });

expect(queryByA11yRole('button', { name: NO_MATCHES_TEXT })).toBeNull();
expect(() => queryByA11yRole('button', { name: TWO_OCCURANCE })).toThrow(
FOUND_TWO_INSTANCES
);

await findByA11yRole('button', {
name: ONE_OCCURANCE,
});
await expect(
findByA11yRole('button', { ...waitForOptions, name: NO_MATCHES_TEXT })
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole', 'button'));
await expect(
findByA11yRole('button', {
name: TWO_OCCURANCE,
})
).rejects.toThrow(FOUND_TWO_INSTANCES);
});

test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole', async () => {
const { getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole } = render(
<Section />
Expand All @@ -207,10 +253,32 @@ test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole', async () => {

await expect(findAllByA11yRole('link')).resolves.toHaveLength(2);
await expect(
findAllByA11yRole(NO_MATCHES_TEXT, waitForOptions)
findAllByA11yRole(NO_MATCHES_TEXT, {}, waitForOptions)
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole'));
});

test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole with name', async () => {
const { getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole } = render(
<ButtonsWithText />
);

expect(getAllByA11yRole('button', { name: TWO_OCCURANCE })).toHaveLength(2);
expect(getAllByA11yRole('button', { name: ONE_OCCURANCE })).toHaveLength(1);
expect(queryAllByA11yRole('button', { name: TWO_OCCURANCE })).toHaveLength(2);

expect(() => getAllByA11yRole('button', { name: NO_MATCHES_TEXT })).toThrow(
getNoInstancesFoundMessage('accessibilityRole', 'button')
);
expect(queryAllByA11yRole('button', { name: NO_MATCHES_TEXT })).toEqual([]);

await expect(
findAllByA11yRole('button', { name: TWO_OCCURANCE })
).resolves.toHaveLength(2);
await expect(
findAllByA11yRole('button', { name: NO_MATCHES_TEXT })
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole', 'button'));
});

// TODO: accessibilityStates was removed from RN 0.62
test.skip('getByA11yStates, queryByA11yStates', () => {
const { getByA11yStates, queryByA11yStates } = render(<Section />);
Expand Down
31 changes: 19 additions & 12 deletions src/helpers/a11yAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type QueryAllReturn = Array<ReactTestInstance>;
type FindReturn = Promise<GetReturn>;
type FindAllReturn = Promise<GetAllReturn>;

export type QueryOptions = {
name?: string | RegExp,
};
export type A11yAPI = {|
// Label
getByA11yLabel: (string | RegExp) => GetReturn,
Expand Down Expand Up @@ -40,18 +43,22 @@ export type A11yAPI = {|
findAllByHintText: (string | RegExp, ?WaitForOptions) => FindAllReturn,

// Role
getByA11yRole: (A11yRole | RegExp) => GetReturn,
getByRole: (A11yRole | RegExp) => GetReturn,
getAllByA11yRole: (A11yRole | RegExp) => GetAllReturn,
getAllByRole: (A11yRole | RegExp) => GetAllReturn,
queryByA11yRole: (A11yRole | RegExp) => QueryReturn,
queryByRole: (A11yRole | RegExp) => QueryReturn,
queryAllByA11yRole: (A11yRole | RegExp) => QueryAllReturn,
queryAllByRole: (A11yRole | RegExp) => QueryAllReturn,
findByA11yRole: (A11yRole, ?WaitForOptions) => FindReturn,
findByRole: (A11yRole, ?WaitForOptions) => FindReturn,
findAllByA11yRole: (A11yRole, ?WaitForOptions) => FindAllReturn,
findAllByRole: (A11yRole, ?WaitForOptions) => FindAllReturn,
getByA11yRole: (A11yRole | RegExp, ?QueryOptions, ?QueryOptions) => GetReturn,
getByRole: (A11yRole | RegExp, ?QueryOptions) => GetReturn,
getAllByA11yRole: (A11yRole | RegExp, ?QueryOptions) => GetAllReturn,
getAllByRole: (A11yRole | RegExp, ?QueryOptions) => GetAllReturn,
queryByA11yRole: (A11yRole | RegExp, ?QueryOptions) => QueryReturn,
queryByRole: (A11yRole | RegExp, ?QueryOptions) => QueryReturn,
queryAllByA11yRole: (A11yRole | RegExp, ?QueryOptions) => QueryAllReturn,
queryAllByRole: (A11yRole | RegExp, ?QueryOptions) => QueryAllReturn,
findByA11yRole: (A11yRole, ?QueryOptions, ?WaitForOptions) => FindReturn,
findByRole: (A11yRole, ?QueryOptions, ?WaitForOptions) => FindReturn,
findAllByA11yRole: (
A11yRole,
?QueryOptions,
?WaitForOptions
) => FindAllReturn,
findAllByRole: (A11yRole, ?QueryOptions, ?WaitForOptions) => FindAllReturn,

// States
getByA11yStates: (A11yStates | Array<A11yStates>) => GetReturn,
Expand Down
66 changes: 53 additions & 13 deletions src/helpers/makeA11yQuery.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// @flow
import waitFor from '../waitFor';
import { getQueriesForElement } from '../within';
import type { WaitForOptions } from '../waitFor';
import {
ErrorWithStack,
prepareErrorMessage,
createQueryByError,
} from './errors';

type QueryOptions = {
Copy link
Contributor Author

@kiranjd kiranjd Mar 30, 2022

Choose a reason for hiding this comment

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

The name in QueryOptions type for getBy is optional. But, it's a must for the function, filterWithName. Is this duplication that I get rid of or is this okay?
(I haven't worked with types all that much 😬 )

name: string | RegExp,
};

function isNodeValid(node: ReactTestInstance) {
return typeof node.type === 'string';
}
Expand All @@ -33,8 +38,27 @@ const makeA11yQuery = <P: mixed, M: mixed>(
): ((instance: ReactTestInstance) => { ... }) => (
instance: ReactTestInstance
) => {
const getBy = (matcher: M) => {
const filterWithName = (
node: ReactTestInstance,
options: QueryOptions,
matcher: M
) => {
const matchesRole =
isNodeValid(node) && matcherFn(node.props[name], matcher);

return (
matchesRole && !!getQueriesForElement(node).queryByText(options.name)
);
};

const getBy = (matcher: M, queryOptions?: QueryOptions) => {
try {
if (queryOptions?.name) {
return instance.find((node) =>
filterWithName(node, queryOptions, matcher)
);
}

return instance.find(
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
);
Expand All @@ -46,10 +70,18 @@ const makeA11yQuery = <P: mixed, M: mixed>(
}
};

const getAllBy = (matcher: M) => {
const results = instance.findAll(
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
);
const getAllBy = (matcher: M, options?: QueryOptions) => {
let results = [];

if (options?.name) {
results = instance.findAll((node) =>
filterWithName(node, options, matcher)
);
} else {
results = instance.findAll(
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher)
);
}

if (results.length === 0) {
throw new ErrorWithStack(
Expand All @@ -61,28 +93,36 @@ const makeA11yQuery = <P: mixed, M: mixed>(
return results;
};

const queryBy = (matcher: M) => {
const queryBy = (matcher: M, options?: QueryOptions) => {
try {
return getBy(matcher);
return getBy(matcher, options);
} catch (error) {
return createQueryByError(error, queryBy);
}
};

const queryAllBy = (matcher: M) => {
const queryAllBy = (matcher: M, options?: QueryOptions) => {
try {
return getAllBy(matcher);
return getAllBy(matcher, options);
} catch (error) {
return [];
}
};

const findBy = (matcher: M, waitForOptions?: WaitForOptions) => {
return waitFor(() => getBy(matcher), waitForOptions);
const findBy = (
matcher: M,
queryOptions?: QueryOptions,
waitForOptions?: WaitForOptions
) => {
return waitFor(() => getBy(matcher, queryOptions), waitForOptions);
};

const findAllBy = (matcher: M, waitForOptions?: WaitForOptions) => {
return waitFor(() => getAllBy(matcher), waitForOptions);
const findAllBy = (
matcher: M,
queryOptions: QueryOptions,
waitForOptions?: WaitForOptions
) => {
return waitFor(() => getAllBy(matcher, queryOptions), waitForOptions);
};

return {
Expand Down