let React;
let ReactNoop;
let Scheduler;
let act;
let assertLog;
let waitForThrow;
describe('ReactFlushSync (AggregateError not available)', () => {
beforeEach(() => {
jest.resetModules();
global.AggregateError = undefined;
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('internal-test-utils').act;
const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
waitForThrow = InternalTestUtils.waitForThrow;
});
function Text({text}) {
Scheduler.log(text);
return text;
}
test('completely exhausts synchronous work queue even if something throws', async () => {
function Throws({error}) {
throw error;
}
const root1 = ReactNoop.createRoot();
const root2 = ReactNoop.createRoot();
const root3 = ReactNoop.createRoot();
await act(async () => {
root1.render(<Text text="Hi" />);
root2.render(<Text text="Andrew" />);
root3.render(<Text text="!" />);
});
assertLog(['Hi', 'Andrew', '!']);
const aahh = new Error('AAHH!');
const nooo = new Error('Noooooooooo!');
let error;
try {
ReactNoop.flushSync(() => {
root1.render(<Throws error={aahh} />);
root2.render(<Throws error={nooo} />);
root3.render(<Text text="aww" />);
});
} catch (e) {
error = e;
}
assertLog(['aww']);
expect(root1).toMatchRenderedOutput(null);
expect(root2).toMatchRenderedOutput(null);
expect(root3).toMatchRenderedOutput('aww');
expect(error).toBe(aahh);
await waitForThrow(nooo);
});
});