Skip to content
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

TextField: For uncontrolled scenarios, update value when defaultValue changes #5903

Merged
merged 2 commits into from
Aug 13, 2018
Merged

TextField: For uncontrolled scenarios, update value when defaultValue changes #5903

merged 2 commits into from
Aug 13, 2018

Conversation

JasonGore
Copy link
Member

@JasonGore JasonGore commented Aug 10, 2018

Pull request checklist

Description of changes

For uncontrolled scenarios (when value prop is undefined), update value when defaultValue prop changes.

Focus areas to test

Add tests to make sure value is updated (or not) depending on controlled and uncontrolled scenarios.

Microsoft Reviewers: Open in CodeFlow

Copy link
Member

@Markionium Markionium left a comment

Choose a reason for hiding this comment

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

Few comments on the tests. I'll approve anyhow, so you can decide what you want to do with them ;)


textField.setProps({ value: testValue, defaultValue: defaultValue2 });
expect(textField.getDOMNode().querySelector('input')).toBeTruthy();
expect(textField.getDOMNode().querySelector('input')!.value).toEqual(testValue);
Copy link
Member

Choose a reason for hiding this comment

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

I know pulling out values that are used multiple times as a dev seems to do the right thing to do. Personally when it comes to test code i find it easier to read when just repeating the values.

    textField.setProps({ value: 'John', defaultValue: 'Your name' });

    expect(textField.getDOMNode().querySelector('input')).toBeTruthy();
    expect(textField.getDOMNode().querySelector('input')!.value).toEqual('John');

Additionally it improves readability a bit more even when you put a new line between the stuff you do and the things you assert.
In the case above the setProps and expect.

Copy link
Member Author

@JasonGore JasonGore Aug 13, 2018

Choose a reason for hiding this comment

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

Related to my other comment, I feel this is basically a test to say input is non-null && input is John before accessing input, not two separate tests.

expect(textField.getDOMNode().querySelector('input')!.value).toEqual(testValue);

textField.setProps({ defaultValue: undefined });
expect(textField.getDOMNode().querySelector('input')).toBeTruthy();
Copy link
Member

@Markionium Markionium Aug 11, 2018

Choose a reason for hiding this comment

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

I think we can probably remove these .toBeTruthy() asserts. I'm not sure how they add value? We're basically testing querySelector in JSDom, which should not be our responsibility :) The test would fail on the next line anyhow if it does not exist.

Copy link
Member Author

@JasonGore JasonGore Aug 13, 2018

Choose a reason for hiding this comment

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

This was the convention throughout this file and I copied a test, but I'm all for standardizing on test methodology. However, it's not testing for the existence of querySelector, it's testing for the existence of the input field. Since the next line is assuming input is non-null, this seems it would generate clearer failure output if things are not as expected.

@JasonGore JasonGore merged commit fcc8f76 into microsoft:master Aug 13, 2018
@microsoft microsoft locked as resolved and limited conversation to collaborators Aug 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Textfield: Updating defaultValue doesnot update the value in input.
2 participants