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 1 commit
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
35 changes: 35 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,19 @@ 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\\"
/>
<View
accessible={true}
focusable={true}
Expand Down Expand Up @@ -82,12 +89,19 @@ 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\\"
/>
<View
accessible={true}
focusable={true}
Expand Down Expand Up @@ -144,12 +158,19 @@ 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\\"
/>
<MyButton
onPress={[Function anonymous]}
type=\\"primary\\"
Expand Down Expand Up @@ -194,12 +215,19 @@ 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\\"
/>
<MyButton
onPress={[Function anonymous]}
type=\\"primary\\"
Expand Down Expand Up @@ -244,12 +272,19 @@ 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\\"
/>
<View
accessible={true}
focusable={true}
Expand Down
13 changes: 13 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,9 @@ class Banana extends React.Component<any, any> {
testID="bananaChef"
placeholder={PLACEHOLDER_CHEF}
value={INPUT_CHEF}
defaultValue={DEFAULT_INPUT_CHEF}
/>
<TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} />
<MyButton onPress={this.changeFresh} type="primary">
Change freshness!
</MyButton>
Expand Down Expand Up @@ -201,6 +205,15 @@ 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(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
5 changes: 2 additions & 3 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => {
const getTextInputNodeByDisplayValue = (node, value) => {
try {
const { TextInput } = require('react-native');
const nodeValue = node.props.value || node.props.defaultValue;
Copy link
Member

@thymikee thymikee Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to be explicit here and check if node.props.value === undefined. Would be great to also have a test for value being 0 and empty string to test that

Copy link
Collaborator Author

@MattAgn MattAgn Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed that would be better! which value do we use if value is empty string?
for 0, is is possible that it is the number 0 in an input field ? or will it be "0" ?

Copy link
Member

@thymikee thymikee Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's always gonna be a string for Text, but the only way to be sure would be to test it. I recall numbers also work here, so the Text component may perform some implicit conversion to string.

Nevertheless, if the value is "" the defaultValue is no longer used as the value is set to not-undefined

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh alright I did not know that, I'll make the changes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flow does not let me put 0 in the value prop of the TextInput, it wants a string. Should I ignore it or do we consider it's fine? (especially since we'll now be checking for undefined and not false values)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to test the number then :)

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