Skip to content

Commit

Permalink
Convert ReactServerRenderingHydration-test to createRoot (partially) (f…
Browse files Browse the repository at this point in the history
…acebook#28010)

Convert ReactServerRenderingHydration-test to createRoot (partially)

Some tests seem to be specifically testing the legacy APIs, maybe we
need to keep those around. Keeping this PR to the simple updates.
  • Loading branch information
kassens authored and AndyPengc12 committed Apr 15, 2024
1 parent 50bba42 commit 093ad1c
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('ReactDOMServerHydration', () => {
// We have a polyfill for autoFocus on the client, but we intentionally don't
// want it to call focus() when hydrating because this can mess up existing
// focus before the JS has loaded.
it('should emit autofocus on the server but not focus() when hydrating', () => {
it('should emit autofocus on the server but not focus() when hydrating', async () => {
const element = document.createElement('div');
element.innerHTML = ReactDOMServer.renderToString(
<input autoFocus={true} />,
Expand All @@ -131,26 +131,35 @@ describe('ReactDOMServerHydration', () => {

// It should not be called on mount.
element.firstChild.focus = jest.fn();
ReactDOM.hydrate(<input autoFocus={true} />, element);
const root = await act(() =>
ReactDOMClient.hydrateRoot(element, <input autoFocus={true} />),
);
expect(element.firstChild.focus).not.toHaveBeenCalled();

// Or during an update.
ReactDOM.render(<input autoFocus={true} />, element);
await act(() => {
root.render(<input autoFocus={true} />);
});
expect(element.firstChild.focus).not.toHaveBeenCalled();
});

it('should not focus on either server or client with autofocus={false}', () => {
it('should not focus on either server or client with autofocus={false}', async () => {
const element = document.createElement('div');
element.innerHTML = ReactDOMServer.renderToString(
<input autoFocus={false} />,
);
expect(element.firstChild.autofocus).toBe(false);

element.firstChild.focus = jest.fn();
ReactDOM.hydrate(<input autoFocus={false} />, element);
const root = await act(() =>
ReactDOMClient.hydrateRoot(element, <input autoFocus={false} />),
);

expect(element.firstChild.focus).not.toHaveBeenCalled();

ReactDOM.render(<input autoFocus={false} />, element);
await act(() => {
root.render(<input autoFocus={false} />);
});
expect(element.firstChild.focus).not.toHaveBeenCalled();
});

Expand Down

0 comments on commit 093ad1c

Please sign in to comment.