Skip to content

Commit

Permalink
Prevent incorrect options SSR hydrate mismatch, fix facebook#11602
Browse files Browse the repository at this point in the history
Avoid overwrite options children nodes when setting the selected option.
  • Loading branch information
t4deu committed Jan 24, 2018
1 parent 4ca7855 commit 0a5f6e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

const TEXT_NODE_TYPE = 3;

let React;
let ReactDOM;
let ReactDOMServer;
Expand All @@ -37,6 +39,8 @@ const {
itClientRenders,
renderIntoDom,
serverRender,
streamRender,
clientRenderOnServerString,
} = ReactDOMServerIntegrationUtils(initModules);

describe('ReactDOMServerIntegration', () => {
Expand All @@ -45,6 +49,11 @@ describe('ReactDOMServerIntegration', () => {
});

describe('form controls', function() {
function expectNode(node, type, value) {
expect(node).not.toBe(null);
expect(node.nodeType).toBe(type);
expect(node.nodeValue).toMatch(value);
}
describe('inputs', function() {
itRenders('an input with a value and an onChange', async render => {
const e = await render(<input value="foo" onChange={() => {}} />);
Expand Down Expand Up @@ -341,6 +350,31 @@ describe('ReactDOMServerIntegration', () => {
);
});

describe('options', function() {
itRenders('an option with multiple text children', async render => {
const e = await render(
<select value="bar" readOnly={true}>
<option value="bar">A {'B'}</option>
</select>,
0,
);
const option = e.options[0];
if (
render === serverRender ||
render === streamRender ||
render === clientRenderOnServerString
) {
// We have three nodes because there is a comment between them.
expect(option.childNodes.length).toBe(3);
expectNode(option.childNodes[0], TEXT_NODE_TYPE, 'A');
expectNode(option.childNodes[2], TEXT_NODE_TYPE, 'B');
} else {
expect(option.childNodes.length).toBe(1);
expectNode(option.childNodes[0], TEXT_NODE_TYPE, 'A B');
}
});
});

describe('user interaction', function() {
let ControlledInput,
ControlledTextArea,
Expand Down
1 change: 0 additions & 1 deletion packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,6 @@ class ReactDOMServerRenderer {
props,
{
selected: selected,
children: optionChildren,
},
);
}
Expand Down

0 comments on commit 0a5f6e4

Please sign in to comment.