Skip to content

Commit f2a7cca

Browse files
authored
fix: getByDisplayValue not checking TextInput's defaultValue (#656)
* fix: make getByDisplayValue check the default value of text inputs * fix: make getByDisplayValue only use defaultValue if value is undefined if the value of TextInput is an empty string, then the TextInput will not use the defaultValue and display the empty string. Our tests now do the same
1 parent f81ebc3 commit f2a7cca

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@ exports[`debug 1`] = `
2020
/>
2121
<TextInput
2222
allowFontScaling={true}
23+
defaultValue=\\"What did you inspect?\\"
2324
placeholder=\\"Who inspected freshness?\\"
2425
rejectResponderTermination={true}
2526
testID=\\"bananaChef\\"
2627
underlineColorAndroid=\\"transparent\\"
2728
value=\\"I inspected freshie\\"
2829
/>
30+
<TextInput
31+
allowFontScaling={true}
32+
defaultValue=\\"What banana?\\"
33+
rejectResponderTermination={true}
34+
underlineColorAndroid=\\"transparent\\"
35+
/>
36+
<TextInput
37+
allowFontScaling={true}
38+
defaultValue=\\"hello\\"
39+
rejectResponderTermination={true}
40+
underlineColorAndroid=\\"transparent\\"
41+
value=\\"\\"
42+
/>
2943
<View
3044
accessible={true}
3145
focusable={true}
@@ -82,12 +96,26 @@ exports[`debug changing component: bananaFresh button message should now be "fre
8296
/>
8397
<TextInput
8498
allowFontScaling={true}
99+
defaultValue=\\"What did you inspect?\\"
85100
placeholder=\\"Who inspected freshness?\\"
86101
rejectResponderTermination={true}
87102
testID=\\"bananaChef\\"
88103
underlineColorAndroid=\\"transparent\\"
89104
value=\\"I inspected freshie\\"
90105
/>
106+
<TextInput
107+
allowFontScaling={true}
108+
defaultValue=\\"What banana?\\"
109+
rejectResponderTermination={true}
110+
underlineColorAndroid=\\"transparent\\"
111+
/>
112+
<TextInput
113+
allowFontScaling={true}
114+
defaultValue=\\"hello\\"
115+
rejectResponderTermination={true}
116+
underlineColorAndroid=\\"transparent\\"
117+
value=\\"\\"
118+
/>
91119
<View
92120
accessible={true}
93121
focusable={true}
@@ -144,12 +172,26 @@ exports[`debug: shallow 1`] = `
144172
/>
145173
<TextInput
146174
allowFontScaling={true}
175+
defaultValue=\\"What did you inspect?\\"
147176
placeholder=\\"Who inspected freshness?\\"
148177
rejectResponderTermination={true}
149178
testID=\\"bananaChef\\"
150179
underlineColorAndroid=\\"transparent\\"
151180
value=\\"I inspected freshie\\"
152181
/>
182+
<TextInput
183+
allowFontScaling={true}
184+
defaultValue=\\"What banana?\\"
185+
rejectResponderTermination={true}
186+
underlineColorAndroid=\\"transparent\\"
187+
/>
188+
<TextInput
189+
allowFontScaling={true}
190+
defaultValue=\\"hello\\"
191+
rejectResponderTermination={true}
192+
underlineColorAndroid=\\"transparent\\"
193+
value=\\"\\"
194+
/>
153195
<MyButton
154196
onPress={[Function anonymous]}
155197
type=\\"primary\\"
@@ -194,12 +236,26 @@ exports[`debug: shallow with message 1`] = `
194236
/>
195237
<TextInput
196238
allowFontScaling={true}
239+
defaultValue=\\"What did you inspect?\\"
197240
placeholder=\\"Who inspected freshness?\\"
198241
rejectResponderTermination={true}
199242
testID=\\"bananaChef\\"
200243
underlineColorAndroid=\\"transparent\\"
201244
value=\\"I inspected freshie\\"
202245
/>
246+
<TextInput
247+
allowFontScaling={true}
248+
defaultValue=\\"What banana?\\"
249+
rejectResponderTermination={true}
250+
underlineColorAndroid=\\"transparent\\"
251+
/>
252+
<TextInput
253+
allowFontScaling={true}
254+
defaultValue=\\"hello\\"
255+
rejectResponderTermination={true}
256+
underlineColorAndroid=\\"transparent\\"
257+
value=\\"\\"
258+
/>
203259
<MyButton
204260
onPress={[Function anonymous]}
205261
type=\\"primary\\"
@@ -244,12 +300,26 @@ exports[`debug: with message 1`] = `
244300
/>
245301
<TextInput
246302
allowFontScaling={true}
303+
defaultValue=\\"What did you inspect?\\"
247304
placeholder=\\"Who inspected freshness?\\"
248305
rejectResponderTermination={true}
249306
testID=\\"bananaChef\\"
250307
underlineColorAndroid=\\"transparent\\"
251308
value=\\"I inspected freshie\\"
252309
/>
310+
<TextInput
311+
allowFontScaling={true}
312+
defaultValue=\\"What banana?\\"
313+
rejectResponderTermination={true}
314+
underlineColorAndroid=\\"transparent\\"
315+
/>
316+
<TextInput
317+
allowFontScaling={true}
318+
defaultValue=\\"hello\\"
319+
rejectResponderTermination={true}
320+
underlineColorAndroid=\\"transparent\\"
321+
value=\\"\\"
322+
/>
253323
<View
254324
accessible={true}
255325
focusable={true}

src/__tests__/render.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
1616
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
1717
const INPUT_FRESHNESS = 'Custom Freshie';
1818
const INPUT_CHEF = 'I inspected freshie';
19+
const DEFAULT_INPUT_CHEF = 'What did you inspect?';
20+
const DEFAULT_INPUT_CUSTOMER = 'What banana?';
1921

2022
class MyButton extends React.Component<any> {
2123
render() {
@@ -67,7 +69,10 @@ class Banana extends React.Component<any, any> {
6769
testID="bananaChef"
6870
placeholder={PLACEHOLDER_CHEF}
6971
value={INPUT_CHEF}
72+
defaultValue={DEFAULT_INPUT_CHEF}
7073
/>
74+
<TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} />
75+
<TextInput defaultValue={'hello'} value="" />
7176
<MyButton onPress={this.changeFresh} type="primary">
7277
Change freshness!
7378
</MyButton>
@@ -201,6 +206,18 @@ test('getByDisplayValue, queryByDisplayValue', () => {
201206
expect(() => queryByDisplayValue(/fresh/i)).toThrow('Expected 1 but found 2');
202207
});
203208

209+
test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => {
210+
const { getByDisplayValue, queryByDisplayValue } = render(<Banana />);
211+
expect(() => getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrow();
212+
expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull();
213+
214+
expect(() => getByDisplayValue('hello')).toThrow();
215+
expect(queryByDisplayValue('hello')).toBeNull();
216+
217+
expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
218+
expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
219+
});
220+
204221
test('getAllByDisplayValue, queryAllByDisplayValue', () => {
205222
const { getAllByDisplayValue, queryAllByDisplayValue } = render(<Banana />);
206223
const inputs = getAllByDisplayValue(/fresh/i);

src/helpers/getByAPI.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => {
107107
const getTextInputNodeByDisplayValue = (node, value) => {
108108
try {
109109
const { TextInput } = require('react-native');
110+
const nodeValue =
111+
node.props.value !== undefined
112+
? node.props.value
113+
: node.props.defaultValue;
110114
return (
111115
filterNodeByType(node, TextInput) &&
112-
(typeof value === 'string'
113-
? value === node.props.value
114-
: value.test(node.props.value))
116+
(typeof value === 'string' ? value === nodeValue : value.test(nodeValue))
115117
);
116118
} catch (error) {
117119
throw createLibraryNotSupportedError(error);

0 commit comments

Comments
 (0)