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

Commit b9f52bf

Browse files
committed
refactor: change component filtering logic
1 parent 53455a9 commit b9f52bf

File tree

4 files changed

+26
-77
lines changed

4 files changed

+26
-77
lines changed

src/__tests__/query-helpers.js

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,15 @@
11
import { defaultFilter, filterNodeByType } from '../query-helpers';
22

3-
test('filterNodeByType returns true when node.type is a match', () => {
4-
expect(filterNodeByType({ type: 'test' }, 'test')).toEqual(true);
3+
test('filterNodeByType returns `true` when node.type matches the provided type', () => {
4+
expect(filterNodeByType({ type: 'Text' }, 'Text')).toEqual(true);
55
});
6-
test('filterNodeByType returns false when node.type is not a match', () => {
7-
expect(filterNodeByType({ type: 'test' }, 'tst')).toEqual(false);
6+
test('filterNodeByType returns `false` when node.type does not match the provided type', () => {
7+
expect(filterNodeByType({ type: 'Text' }, 'Test')).toEqual(false);
88
});
99

10-
test('filterNodeByType returns true when node.type.name is a match', () => {
11-
expect(filterNodeByType({ type: { name: 'test' } }, 'test')).toEqual(true);
10+
test('defaultFilter returns `true` when node.type is in the mocked type list', () => {
11+
expect(defaultFilter({ type: 'Text' })).toEqual(true);
1212
});
13-
test('filterNodeByType returns false when node.type.name is not a match', () => {
14-
expect(filterNodeByType({ type: { name: 'test' } }, 'tst')).toEqual(false);
15-
});
16-
17-
test('filterNodeByType returns true when node.type.displayName is a match', () => {
18-
expect(filterNodeByType({ type: { displayName: 'test' } }, 'test')).toEqual(true);
19-
});
20-
test('filterNodeByType returns false when node.type.displayName is not a match', () => {
21-
expect(filterNodeByType({ type: { displayName: 'test' } }, 'tst')).toEqual(false);
22-
});
23-
24-
test('filterNodeByType returns false when no node is given', () => {
25-
expect(filterNodeByType(null, 'test')).toEqual(false);
26-
});
27-
28-
test('filterNodeByType returns false when undefined is given', () => {
29-
expect(filterNodeByType(undefined, 'test')).toEqual(false);
30-
});
31-
32-
//function defaultFilter(node) {
33-
// const name =
34-
// node.type.displayName ||
35-
// node.type.name ||
36-
// (node.type.render // handle React.forwardRef
37-
// ? node.type.render.displayName || node.type.render.name
38-
// : 'Unknown');
39-
//
40-
// return name !== 'Unknown';
41-
//}
42-
43-
test('defaultFilter returns `false` when node.type.displayName is provided', () => {
44-
expect(defaultFilter({ type: { displayName: 'test' } })).toEqual(true);
45-
});
46-
test('defaultFilter returns `false` when node.type.name is provided', () => {
47-
expect(defaultFilter({ type: { name: 'test' } })).toEqual(true);
48-
});
49-
test('defaultFilter returns `false` when node.type.render.displayName is provided', () => {
50-
expect(defaultFilter({ type: { render: { displayName: 'test' } } })).toEqual(true);
51-
});
52-
test('defaultFilter returns `false` when node.type.render.name is provided', () => {
53-
expect(defaultFilter({ type: { render: { name: 'test' } } })).toEqual(true);
54-
});
55-
test('defaultFilter returns `true` when when it cannot find a type name on the node', () => {
56-
expect(defaultFilter({ type: {} })).toEqual(false);
13+
test('defaultFilter returns `false` when node.type is not in the mocked type list', () => {
14+
expect(defaultFilter({ type: 'Test' })).toEqual(false);
5715
});

src/get-node-text.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { Text, TextInput } from 'react-native';
21
import { filterNodeByType } from './query-helpers';
32

43
function getNodeText(node) {
5-
if (filterNodeByType(node, TextInput)) {
4+
if (filterNodeByType(node, 'TextInput')) {
65
return node.props.value;
76
}
87

9-
return Array.from(node.children)
10-
.filter(node => filterNodeByType(node, 'Text') && Boolean(node.props.children))
11-
.map(({ props }) => props.children)
12-
.join('');
8+
return Array.from(node.children).join('');
139
}
1410

1511
export { getNodeText };

src/queries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const queryAllByA11yRole = queryAllByProp.bind(null, 'accessibilityRole');
2020
function queryAllByText(
2121
container,
2222
text,
23-
{ types = [Text, TextInput], exact = true, collapseWhitespace, trim, normalizer } = {},
23+
{ types = ['Text', 'TextInput'], exact = true, collapseWhitespace, trim, normalizer } = {},
2424
) {
2525
const rootInstance = container.root;
2626
const matcher = exact ? matches : fuzzyMatches;

src/query-helpers.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,8 @@ function getElementError(message, container) {
1111
return new Error([message, debugDOM(container)].filter(Boolean).join('\n\n'));
1212
}
1313

14-
function filterNodeByType(node = {}, type) {
15-
if (!node) {
16-
return false;
17-
}
18-
19-
return (
20-
node.type === type ||
21-
(node.type && node.type.name === type) ||
22-
(node.type && node.type.displayName === type) ||
23-
(node.type && node.type.render && node.type.render.name === type) ||
24-
(node.type && node.type.render && node.type.render.displayName === type) ||
25-
false
26-
);
14+
function filterNodeByType(node, type) {
15+
return node.type === type;
2716
}
2817

2918
function firstResultOrNull(queryFunction, ...args) {
@@ -54,14 +43,20 @@ function firstResultOrNull(queryFunction, ...args) {
5443
// native components of only returning odd results. In the example above, that would be the inner-most
5544
// <Text /> component, which is the one you'll actually want (it's the mocked native <Text />).
5645
function defaultFilter(node) {
57-
const name =
58-
node.type.displayName ||
59-
node.type.name ||
60-
(node.type.render // handle React.forwardRef
61-
? node.type.render.displayName || node.type.render.name
62-
: 'Unknown');
46+
const mockedTypes = [
47+
'Image',
48+
'Text',
49+
'TextInput',
50+
'Modal',
51+
'View',
52+
'RefreshControl',
53+
'ScrollView',
54+
'ActivityIndicator',
55+
'ListView',
56+
'ListViewDataSource',
57+
];
6358

64-
return name !== 'Unknown';
59+
return mockedTypes.includes(node.type);
6560
}
6661

6762
function queryAllByProp(

0 commit comments

Comments
 (0)