Skip to content

Commit 0840d44

Browse files
hdupratthymikee
authored andcommitted
fix: (getByText) more robust null/undefined check for text children (#239)
* fix: (getNodeByText) explicitly check if child is undefined or null * test: Update getByText test cases
1 parent 008cae2 commit 0840d44

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/__tests__/__snapshots__/render.test.js.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ exports[`debug 1`] = `
5757
>
5858
Second Text
5959
</Text>
60+
<Text>
61+
0
62+
</Text>
6063
</View>"
6164
`;
6265

@@ -117,6 +120,9 @@ exports[`debug changing component: bananaFresh button message should now be "fre
117120
>
118121
Second Text
119122
</Text>
123+
<Text>
124+
0
125+
</Text>
120126
</View>"
121127
`;
122128

@@ -162,6 +168,9 @@ exports[`debug: shallow 1`] = `
162168
>
163169
Second Text
164170
</Text>
171+
<Text>
172+
0
173+
</Text>
165174
</View>"
166175
`;
167176

@@ -209,6 +218,9 @@ exports[`debug: shallow with message 1`] = `
209218
>
210219
Second Text
211220
</Text>
221+
<Text>
222+
0
223+
</Text>
212224
</View>"
213225
`;
214226

@@ -271,6 +283,9 @@ exports[`debug: with message 1`] = `
271283
>
272284
Second Text
273285
</Text>
286+
<Text>
287+
0
288+
</Text>
274289
</View>"
275290
`;
276291

src/__tests__/render.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Banana extends React.Component<any, any> {
5151
};
5252

5353
render() {
54+
const test = 0;
5455
return (
5556
<View>
5657
<Text>Is the banana fresh?</Text>
@@ -72,6 +73,7 @@ class Banana extends React.Component<any, any> {
7273
</Button>
7374
<Text testID="duplicateText">First Text</Text>
7475
<Text testID="duplicateText">Second Text</Text>
76+
<Text>{test}</Text>
7577
</View>
7678
);
7779
}
@@ -121,7 +123,7 @@ test('getByName, queryByName', () => {
121123

122124
expect(bananaFresh.props.children).toBe('not fresh');
123125
expect(() => getByName('InExistent')).toThrow('No instances found');
124-
expect(() => getByName(Text)).toThrow('Expected 1 but found 5');
126+
expect(() => getByName(Text)).toThrow('Expected 1 but found 6');
125127

126128
expect(queryByName('Button')).toBe(button);
127129
expect(queryByName('InExistent')).toBeNull();
@@ -165,9 +167,12 @@ test('getByText, queryByText', () => {
165167
expect(sameButton.props.children).toBe('not fresh');
166168
expect(() => getByText('InExistent')).toThrow('No instances found');
167169

170+
const zeroText = getByText('0');
171+
168172
expect(queryByText(/change/i)).toBe(button);
169173
expect(queryByText('InExistent')).toBeNull();
170174
expect(() => queryByText(/fresh/)).toThrow('Expected 1 but found 3');
175+
expect(queryByText('0')).toBe(zeroText);
171176
});
172177

173178
test('getByText, queryByText with children as Array', () => {

src/helpers/getByAPI.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const getNodeByText = (node, text) => {
2222
if (isTextComponent) {
2323
const textChildren = React.Children.map(
2424
node.props.children,
25-
// In some cases child might be undefined
26-
child => (child ? child.toString() : '')
25+
// In some cases child might be undefined or null
26+
child => (child !== undefined && child !== null ? child.toString() : '')
2727
);
2828
if (textChildren) {
2929
const textToTest = textChildren.join('');

0 commit comments

Comments
 (0)