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

Commit bcde7d7

Browse files
author
Brandon Carroll
committed
feat: only traverse valid parents and children
1 parent 4980ac2 commit bcde7d7

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/__tests__/misc.js

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

55
import { fireEvent, render } from '../';
@@ -42,3 +42,42 @@ test('fragments can show diffs', () => {
4242
// See https://github.com/jest-community/snapshot-diff
4343
expect(firstRender).toMatchDiffSnapshot(asFragment());
4444
});
45+
46+
test('finds only valid children', () => {
47+
const Wrapper = ({ children }) => <View>{children}</View>;
48+
49+
const { container } = render(
50+
<View>
51+
<Wrapper>
52+
<Text>hey</Text>
53+
<Text>sup</Text>
54+
<View />
55+
</Wrapper>
56+
</View>,
57+
);
58+
59+
expect(
60+
// AppContainer
61+
// => node text
62+
// => Text
63+
// => View (from Wrapper)
64+
// => View
65+
container.children[0].children[0].children[0].children[0],
66+
).toBe('hey');
67+
});
68+
69+
test('it finds only valid parents', () => {
70+
const Wrapper = ({ children }) => <View>{children}</View>;
71+
72+
const { baseElement, getByText } = render(
73+
<View testID="view">
74+
<Wrapper>
75+
<Text>hey</Text>
76+
<Text>sup</Text>
77+
</Wrapper>
78+
</View>,
79+
);
80+
81+
expect(getByText('hey').parentNode.parentNode.props.testID).toBe('view');
82+
expect(baseElement.parentNode).toBeNull();
83+
});

src/lib/__tests__/misc.js

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

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

src/lib/query-helpers.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ function validComponentFilter(node, key) {
2424
return key ? getConfig(key).includes(node.type) : typeof node.type === 'string';
2525
}
2626

27+
function flatten(arr) {
28+
return arr.reduce(
29+
(flat, toFlatten) => flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten),
30+
[],
31+
);
32+
}
33+
34+
function getChildren(node) {
35+
return node.children.map(child => {
36+
if (typeof child === 'string') {
37+
return child;
38+
} else if (validComponentFilter(child)) {
39+
return proxyUnsafeProperties(child);
40+
}
41+
42+
return getChildren(child);
43+
});
44+
}
45+
46+
function getParent(node) {
47+
if (node.parent) {
48+
return validComponentFilter(node.parent)
49+
? proxyUnsafeProperties(node.parent)
50+
: getParent(node.parent);
51+
}
52+
53+
return null;
54+
}
55+
2756
function proxyUnsafeProperties(node) {
2857
// We take the guiding principles seriously around these parts. These methods just let
2958
// you do too much unfortunately, and they make it hard to follow the rules of the
@@ -55,6 +84,10 @@ function proxyUnsafeProperties(node) {
5584
/* istanbul ignore next */
5685
return target.props[prop];
5786
};
87+
case 'children':
88+
return flatten(getChildren(target));
89+
case 'parentNode':
90+
return getParent(target);
5891
case '$$typeof':
5992
return Symbol.for('ntl.element');
6093
case 'findAllByProps':

0 commit comments

Comments
 (0)