Skip to content

Fix issue of getByDisplayValue not checking TextInputs defaultValue #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/__tests__/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@ exports[`debug 1`] = `
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What did you inspect?\\"
placeholder=\\"Who inspected freshness?\\"
rejectResponderTermination={true}
testID=\\"bananaChef\\"
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What banana?\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"hello\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
value=\\"\\"
/>
<View
accessible={true}
focusable={true}
Expand Down Expand Up @@ -82,12 +96,26 @@ exports[`debug changing component: bananaFresh button message should now be "fre
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What did you inspect?\\"
placeholder=\\"Who inspected freshness?\\"
rejectResponderTermination={true}
testID=\\"bananaChef\\"
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What banana?\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"hello\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
value=\\"\\"
/>
<View
accessible={true}
focusable={true}
Expand Down Expand Up @@ -144,12 +172,26 @@ exports[`debug: shallow 1`] = `
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What did you inspect?\\"
placeholder=\\"Who inspected freshness?\\"
rejectResponderTermination={true}
testID=\\"bananaChef\\"
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What banana?\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"hello\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
value=\\"\\"
/>
<MyButton
onPress={[Function anonymous]}
type=\\"primary\\"
Expand Down Expand Up @@ -194,12 +236,26 @@ exports[`debug: shallow with message 1`] = `
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What did you inspect?\\"
placeholder=\\"Who inspected freshness?\\"
rejectResponderTermination={true}
testID=\\"bananaChef\\"
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What banana?\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"hello\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
value=\\"\\"
/>
<MyButton
onPress={[Function anonymous]}
type=\\"primary\\"
Expand Down Expand Up @@ -244,12 +300,26 @@ exports[`debug: with message 1`] = `
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What did you inspect?\\"
placeholder=\\"Who inspected freshness?\\"
rejectResponderTermination={true}
testID=\\"bananaChef\\"
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"What banana?\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
/>
<TextInput
allowFontScaling={true}
defaultValue=\\"hello\\"
rejectResponderTermination={true}
underlineColorAndroid=\\"transparent\\"
value=\\"\\"
/>
<View
accessible={true}
focusable={true}
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
const INPUT_FRESHNESS = 'Custom Freshie';
const INPUT_CHEF = 'I inspected freshie';
const DEFAULT_INPUT_CHEF = 'What did you inspect?';
const DEFAULT_INPUT_CUSTOMER = 'What banana?';

class MyButton extends React.Component<any> {
render() {
Expand Down Expand Up @@ -67,7 +69,10 @@ class Banana extends React.Component<any, any> {
testID="bananaChef"
placeholder={PLACEHOLDER_CHEF}
value={INPUT_CHEF}
defaultValue={DEFAULT_INPUT_CHEF}
/>
<TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} />
<TextInput defaultValue={'hello'} value="" />
<MyButton onPress={this.changeFresh} type="primary">
Change freshness!
</MyButton>
Expand Down Expand Up @@ -201,6 +206,18 @@ test('getByDisplayValue, queryByDisplayValue', () => {
expect(() => queryByDisplayValue(/fresh/i)).toThrow('Expected 1 but found 2');
});

test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => {
const { getByDisplayValue, queryByDisplayValue } = render(<Banana />);
expect(() => getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrow();
expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull();

expect(() => getByDisplayValue('hello')).toThrow();
expect(queryByDisplayValue('hello')).toBeNull();

expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
});

test('getAllByDisplayValue, queryAllByDisplayValue', () => {
const { getAllByDisplayValue, queryAllByDisplayValue } = render(<Banana />);
const inputs = getAllByDisplayValue(/fresh/i);
Expand Down
8 changes: 5 additions & 3 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => {
const getTextInputNodeByDisplayValue = (node, value) => {
try {
const { TextInput } = require('react-native');
const nodeValue =
node.props.value !== undefined
? node.props.value
: node.props.defaultValue;
return (
filterNodeByType(node, TextInput) &&
(typeof value === 'string'
? value === node.props.value
: value.test(node.props.value))
(typeof value === 'string' ? value === nodeValue : value.test(nodeValue))
);
} catch (error) {
throw createLibraryNotSupportedError(error);
Expand Down