Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 2097cc7

Browse files
ajsmthbcarroll22
authored andcommitted
fix: change filter prop to selector for queries (#76)
* feat: formatting options for render() to remove options from debug output * update name -- propsToRemove to just removeProps * omitProps: remove prettyPrint plugin in favour of removing props in toJSON() call * update options in render() to destructure debug config * update filter fn to be named selector to match testing library API BREAKING CHANGE: The filter option has been renamed to selector to maintain compatibility with the rest of the Testing Library API.
1 parent c9344ab commit 2097cc7

File tree

6 files changed

+55
-11
lines changed

6 files changed

+55
-11
lines changed

src/lib/__tests__/misc.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Picker, Switch, View } from 'react-native';
2+
import { Picker, Switch, View, Text, TextInput, Button } from 'react-native';
33

44
import { render, queryByProp, queryByTestId, cleanup } from '../../';
55

@@ -33,3 +33,47 @@ it('should render test', () => {
3333

3434
expect(getByDisplayValue(true)).toBeTruthy();
3535
});
36+
37+
test('selector option in queries filter out elements', () => {
38+
function filterByLabel(label) {
39+
return {
40+
selector: ({ props }) => props.accessibilityLabel === label,
41+
};
42+
}
43+
44+
const { getByText, getByRole, getByDisplayValue, getByTitle } = render(
45+
<>
46+
<Text>hello world</Text>
47+
<Text accessibilityLabel="labelled">hello world</Text>
48+
49+
<View accessibilityRole="link" />
50+
<View accessibilityRole="link" accessibilityLabel="labelled" />
51+
52+
<TextInput value="hello joe" />
53+
<TextInput value="hello joe" accessibilityLabel="labelled" />
54+
55+
<Button title="hello joe" />
56+
<Button title="hello joe" accessibilityLabel="labelled" />
57+
</>,
58+
);
59+
60+
// more than one match:
61+
expect(() => getByText(/hello world/i)).toThrow();
62+
// filtered
63+
getByText(/hello world/i, filterByLabel('labelled'));
64+
65+
// more than one match:
66+
expect(() => getByRole('link')).toThrow();
67+
// filtered
68+
getByRole('link', filterByLabel('labelled'));
69+
70+
// more than one match:
71+
expect(() => getByDisplayValue(/hello joe/i)).toThrow();
72+
// filtered
73+
getByDisplayValue(/hello joe/i, filterByLabel('labelled'));
74+
75+
// more than one match:
76+
expect(() => getByTitle(/hello joe/i)).toThrow();
77+
// filtered
78+
getByTitle(/hello joe/i, filterByLabel('labelled'));
79+
});

src/lib/queries/display-value.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
function queryAllByDisplayValue(
1010
container,
1111
value,
12-
{ filter = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
12+
{ selector = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
1313
) {
1414
const matcher = exact ? matches : fuzzyMatches;
1515
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1616

1717
return Array.from(container.findAll(node => validComponentFilter(node, 'displayValueComponents')))
18-
.filter(filter)
18+
.filter(selector)
1919
.filter(node => {
2020
if (node.type === 'Picker') {
2121
return matcher(node.getProp('selectedValue'), node, value, matchNormalizer);

src/lib/queries/role.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ const validTraits = [
5050
'text',
5151
];
5252

53-
function queryAllByRole(container, value, { filter = n => n } = {}) {
53+
function queryAllByRole(container, value, { selector = n => n } = {}) {
5454
const roleElements = container.findAll(c => c.getProp('accessibilityRole'));
5555
const traitElements = container.findAll(c => c.getProp('accessibilityTraits'));
5656

57-
return [...roleElements, ...traitElements].filter(filter).filter(node => {
57+
return [...roleElements, ...traitElements].filter(selector).filter(node => {
5858
const role = node.getProp('accessibilityRole');
5959
const traits = node.getProp('accessibilityTraits');
6060

src/lib/queries/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import {
1010
function queryAllByText(
1111
container,
1212
text,
13-
{ filter = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
13+
{ selector = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
1414
) {
1515
const matcher = exact ? matches : fuzzyMatches;
1616
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1717

1818
return Array.from(container.findAll(node => validComponentFilter(node, 'textComponents')))
19-
.filter(filter)
19+
.filter(selector)
2020
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer));
2121
}
2222

src/lib/queries/title.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
function queryAllByTitle(
1010
container,
1111
value,
12-
{ filter = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
12+
{ selector = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
1313
) {
1414
const matcher = exact ? matches : fuzzyMatches;
1515
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
1616

1717
return Array.from(container.findAll(node => validComponentFilter(node, 'titleComponents')))
18-
.filter(filter)
18+
.filter(selector)
1919
.filter(node => matcher(node.getProp('title'), node, value, matchNormalizer));
2020
}
2121

src/lib/query-helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ function queryAllByProp(
104104
prop,
105105
container,
106106
match,
107-
{ filter = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
107+
{ selector = n => n, exact = true, collapseWhitespace, trim, normalizer } = {},
108108
) {
109109
const matcher = exact ? matches : fuzzyMatches;
110110
const matchNormalizer = makeNormalizer({ collapseWhitespace, trim, normalizer });
111111

112112
return Array.from(container.findAll(c => c.getProp(prop)))
113-
.filter(filter)
113+
.filter(selector)
114114
.filter(node => {
115115
const value = node.getProp(prop);
116116

0 commit comments

Comments
 (0)