Skip to content

Commit 77b9666

Browse files
committed
Test that hydration doesn't call focus
1 parent 536c1cf commit 77b9666

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/renderers/dom/shared/__tests__/ReactServerRendering-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,26 @@ describe('ReactDOMServer', () => {
351351
expect(numClicks).toEqual(2);
352352
});
353353

354+
// We have a polyfill for autoFocus on the client, but we intentionally don't
355+
// want it to call focus() when hydrating because this can mess up existing
356+
// focus before the JS has loaded.
357+
it('should emit autofocus on the server but not focus() when hydrating', () => {
358+
var element = document.createElement('div');
359+
element.innerHTML = ReactDOMServer.renderToString(
360+
<input autoFocus={true} />,
361+
);
362+
expect(element.firstChild.autofocus).toBe(true);
363+
364+
// It should not be called on mount.
365+
element.firstChild.focus = jest.fn();
366+
ReactDOM.hydrate(<input autoFocus={true} />, element);
367+
expect(element.firstChild.focus).not.toHaveBeenCalled();
368+
369+
// Or during an update.
370+
ReactDOM.render(<input autoFocus={true} />, element);
371+
expect(element.firstChild.focus).not.toHaveBeenCalled();
372+
});
373+
354374
it('should throw with silly args', () => {
355375
expect(
356376
ReactDOMServer.renderToString.bind(ReactDOMServer, {x: 123}),

0 commit comments

Comments
 (0)