'use strict';
let React;
let ReactDOMClient;
let act;
let assertLog;
let Scheduler;
describe('ReactDOMSafariMicrotaskBug-test', () => {
let container;
let overrideQueueMicrotask;
let flushFakeMicrotasks;
beforeEach(() => {
const originalQueueMicrotask = queueMicrotask;
overrideQueueMicrotask = false;
const fakeMicrotaskQueue = [];
global.queueMicrotask = cb => {
if (overrideQueueMicrotask) {
fakeMicrotaskQueue.push(cb);
} else {
originalQueueMicrotask(cb);
}
};
flushFakeMicrotasks = () => {
while (fakeMicrotaskQueue.length > 0) {
const cb = fakeMicrotaskQueue.shift();
cb();
}
};
jest.resetModules();
container = document.createElement('div');
React = require('react');
ReactDOMClient = require('react-dom/client');
act = require('internal-test-utils').act;
assertLog = require('internal-test-utils').assertLog;
Scheduler = require('scheduler');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
});
it('should deal with premature microtask in commit phase', async () => {
let ran = false;
function Foo() {
const [state, setState] = React.useState(0);
return (
<div
ref={() => {
overrideQueueMicrotask = true;
if (!ran) {
ran = true;
setState(1);
flushFakeMicrotasks();
Scheduler.log(
'Content at end of ref callback: ' + container.textContent,
);
}
}}>
{state}
</div>
);
}
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Foo />);
});
assertLog(['Content at end of ref callback: 0']);
expect(container.textContent).toBe('1');
});
it('should deal with premature microtask in event handler', async () => {
function Foo() {
const [state, setState] = React.useState(0);
return (
<button
onClick={() => {
overrideQueueMicrotask = true;
setState(1);
flushFakeMicrotasks();
Scheduler.log(
'Content at end of click handler: ' + container.textContent,
);
}}>
{state}
</button>
);
}
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Foo />);
});
expect(container.textContent).toBe('0');
await act(() => {
container.firstChild.dispatchEvent(
new MouseEvent('click', {bubbles: true}),
);
});
assertLog(['Content at end of click handler: 1']);
expect(container.textContent).toBe('1');
});
});