Skip to content

Commit

Permalink
Clean up legacy render from ReactTestUtilsAct-test (facebook#28091)
Browse files Browse the repository at this point in the history
Co-authored-by: Jack Pope <jackpope@meta.com>
  • Loading branch information
2 people authored and AndyPengc12 committed Apr 15, 2024
1 parent 5919068 commit 1993c1e
Showing 1 changed file with 33 additions and 84 deletions.
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

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

0 comments on commit 1993c1e

Please sign in to comment.