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

Clean up legacy render from ReactTestUtilsAct-test #28091

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all 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
117 changes: 33 additions & 84 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

let React;
let ReactDOM;
let ReactDOMClient;
let ReactTestUtils;
let Scheduler;
Expand All @@ -33,91 +32,57 @@ describe('ReactTestUtils.act()', () => {
jest.restoreAllMocks();
});

// first we run all the tests with concurrent mode
if (__EXPERIMENTAL__) {
let concurrentRoot = null;
const renderConcurrent = (el, dom) => {
concurrentRoot = ReactDOMClient.createRoot(dom);
if (__DEV__) {
act(() => concurrentRoot.render(el));
} else {
concurrentRoot.render(el);
}
};

const unmountConcurrent = _dom => {
if (__DEV__) {
act(() => {
if (concurrentRoot !== null) {
concurrentRoot.unmount();
concurrentRoot = null;
}
});
} else {
if (concurrentRoot !== null) {
concurrentRoot.unmount();
concurrentRoot = null;
let root = null;
const renderConcurrent = (el, dom) => {
root = ReactDOMClient.createRoot(dom);
if (__DEV__) {
act(() => root.render(el));
} else {
root.render(el);
}
};

const unmountConcurrent = _dom => {
if (__DEV__) {
act(() => {
if (root !== null) {
root.unmount();
root = null;
}
});
} else {
if (root !== null) {
root.unmount();
root = null;
}
};

const rerenderConcurrent = el => {
act(() => concurrentRoot.render(el));
};

runActTests(
'concurrent mode',
renderConcurrent,
unmountConcurrent,
rerenderConcurrent,
);
}

// and then in legacy mode
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test previously ran against both legacy and concurrent modes. It doesn't seem to me that we need to maintain legacy coverage against act for now but I can move the cases into a separate internal test if folks think otherwise.


let legacyDom = null;
function renderLegacy(el, dom) {
legacyDom = dom;
ReactDOM.render(el, dom);
}

function unmountLegacy(dom) {
legacyDom = null;
ReactDOM.unmountComponentAtNode(dom);
}
}
};

function rerenderLegacy(el) {
ReactDOM.render(el, legacyDom);
}
const rerenderConcurrent = el => {
act(() => root.render(el));
};

runActTests('legacy mode', renderLegacy, unmountLegacy, rerenderLegacy);
runActTests(renderConcurrent, unmountConcurrent, rerenderConcurrent);

describe('unacted effects', () => {
function App() {
React.useEffect(() => {}, []);
return null;
}

it('does not warn in legacy mode', () => {
expect(() => {
ReactDOM.render(<App />, document.createElement('div'));
}).toErrorDev([]);
});

// @gate __DEV__
it('does not warn in concurrent mode', () => {
const root = ReactDOMClient.createRoot(document.createElement('div'));
it('does not warn', () => {
root = ReactDOMClient.createRoot(document.createElement('div'));
act(() => root.render(<App />));
});
});
});

function runActTests(label, render, unmount, rerender) {
describe(label, () => {
function runActTests(render, unmount, rerender) {
describe('concurrent render', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactTestUtils = require('react-dom/test-utils');
Scheduler = require('scheduler');
Expand Down Expand Up @@ -703,14 +668,6 @@ function runActTests(label, render, unmount, rerender) {

// @gate __DEV__
it('triggers fallbacks if available', async () => {
if (label !== 'legacy mode') {
// FIXME: Support for Concurrent Root intentionally removed
// from the public version of `act`. It will be added back in
// a future major version, before the Concurrent Root is released.
// Consider skipping all non-Legacy tests in this suite until then.
return;
}

let resolved = false;
let resolve;
const promise = new Promise(_resolve => {
Expand Down Expand Up @@ -759,16 +716,8 @@ function runActTests(label, render, unmount, rerender) {
});
});

if (label === 'concurrent mode') {
// In Concurrent Mode, refresh transitions delay indefinitely.
expect(document.querySelector('[data-test-id=spinner]')).toBeNull();
} else {
// In Legacy Mode, all fallbacks are forced to display,
// even during a refresh transition.
expect(
document.querySelector('[data-test-id=spinner]'),
).not.toBeNull();
}
// In Concurrent Mode, refresh transitions delay indefinitely.
expect(document.querySelector('[data-test-id=spinner]')).toBeNull();

// resolve the promise
await act(async () => {
Expand Down