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

Deduplication of warn when selected is set on <option> #11821

Merged
merged 5 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 28 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,34 @@ describe('ReactDOMSelect', () => {
}
});

it('should warn if selected is set on <option>', () => {
spyOnDev(console, 'error');

ReactTestUtils.renderIntoDocument(
<select>
<option selected={true} />
<option selected={true} />
</select>,
);
if (__DEV__) {
// Test deduplication
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is not relevant here because you’re not testing deduplication :-)
To actually test deduplication, move it after the second render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaearon Just so I understand. I am doing the following twice which should fire the warning twice:

<option selected={true} />
<option selected={true} />

And so isn't this testing for deduplication of the warning?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It depends on how deduplication is implemented—in general I'd say rendering twice is a more sure way to tell if it works or not. You're right that in this case it's enough, but I would feel safer if we render twice and then check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good

expect(console.error.calls.count()).toBe(1);
}

ReactTestUtils.renderIntoDocument(
<select>
<option selected={true} />
<option selected={true} />
</select>,
);
if (__DEV__) {
expect(console.error.calls.argsFor(0)[0]).toContain(
'Use the `defaultValue` or `value` props on <select> instead of ' +
'setting `selected` on <option>.',
);
}
});

it('should warn if value is null and multiple is true', () => {
spyOnDev(console, 'error');
ReactTestUtils.renderIntoDocument(
Expand Down
15 changes: 10 additions & 5 deletions packages/react-dom/src/client/ReactDOMFiberOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import React from 'react';
import warning from 'fbjs/lib/warning';

let didWarnSelectedSetOnOption = false;

function flattenChildren(children) {
let content = '';

Expand All @@ -36,11 +38,14 @@ function flattenChildren(children) {
export function validateProps(element: Element, props: Object) {
// TODO (yungsters): Remove support for `selected` in <option>.
if (__DEV__) {
warning(
props.selected == null,
'Use the `defaultValue` or `value` props on <select> instead of ' +
'setting `selected` on <option>.',
);
if (!didWarnSelectedSetOnOption) {
warning(
props.selected == null,
'Use the `defaultValue` or `value` props on <select> instead of ' +
'setting `selected` on <option>.',
);
didWarnSelectedSetOnOption = true;
}
}
}

Expand Down