act: Move didScheduleLegacyUpdate to ensureRootIsScheduled (#26552)

`act` uses the `didScheduleLegacyUpdate` field to simulate the behavior
of batching in React <17 and below. It's a quirk leftover from a
previous implementation, not intentionally designed.

This sets `didScheduleLegacyUpdate` every time a legacy root receives an
update as opposed to only when the `executionContext` is empty. There's
no real reason to do it this way over some other way except that it's
how it used to work before #26512 and we should try our best to maintain
the existing behavior, quirks and all, since existing tests may have
come to accidentally rely on it.

This should fix some (though not all) of the internal Meta tests that
started failing after #26512 landed.

Will add a regression test before merging.
This commit is contained in:
Andrew Clark
2023-04-10 14:40:18 -04:00
committed by GitHub
parent 9a9da7721e
commit fec97ecbc4
4 changed files with 81 additions and 16 deletions
+3 -3
View File
@@ -20,13 +20,13 @@ function captureAssertion(fn) {
return {pass: true};
}
function assertYieldsWereCleared(Scheduler) {
function assertYieldsWereCleared(Scheduler, caller) {
const actualYields = Scheduler.unstable_clearLog();
if (actualYields.length !== 0) {
const error = Error(
'The event log is not empty. Call assertLog(...) first.'
);
Error.captureStackTrace(error, assertYieldsWereCleared);
Error.captureStackTrace(error, caller);
throw error;
}
}
@@ -34,7 +34,7 @@ function assertYieldsWereCleared(Scheduler) {
function toMatchRenderedOutput(ReactNoop, expectedJSX) {
if (typeof ReactNoop.getChildrenAsJSX === 'function') {
const Scheduler = ReactNoop._Scheduler;
assertYieldsWereCleared(Scheduler);
assertYieldsWereCleared(Scheduler, toMatchRenderedOutput);
return captureAssertion(() => {
expect(ReactNoop.getChildrenAsJSX()).toEqual(expectedJSX);
});