Skip to content

Commit 2547b44

Browse files
committed
Add a regression test
1 parent bfa66bf commit 2547b44

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
let React;
13+
14+
let ReactDOM;
15+
let act;
16+
17+
describe('ReactDOMSafariMicrotaskBug-test', () => {
18+
let container;
19+
let simulateSafariBug;
20+
21+
beforeEach(() => {
22+
// In Safari, microtasks don't always run on clean stack.
23+
// This setup crudely approximates it.
24+
// In reality, the sync flush happens when an iframe is added to the page.
25+
// https://github.com/facebook/react/issues/22459
26+
let queue = [];
27+
window.queueMicrotask = function(cb) {
28+
queue.push(cb);
29+
};
30+
simulateSafariBug = function() {
31+
queue.forEach(cb => cb());
32+
queue = [];
33+
};
34+
35+
jest.resetModules();
36+
container = document.createElement('div');
37+
React = require('react');
38+
ReactDOM = require('react-dom');
39+
act = require('jest-react').act;
40+
41+
document.body.appendChild(container);
42+
});
43+
44+
afterEach(() => {
45+
document.body.removeChild(container);
46+
});
47+
48+
it('should be resilient to buggy queueMicrotask', async () => {
49+
let ran = false;
50+
function Foo() {
51+
const [state, setState] = React.useState(0);
52+
return (
53+
<div
54+
ref={() => {
55+
if (!ran) {
56+
ran = true;
57+
setState(1);
58+
simulateSafariBug();
59+
}
60+
}}>
61+
{state}
62+
</div>
63+
);
64+
}
65+
const root = ReactDOM.createRoot(container);
66+
await act(async () => {
67+
root.render(<Foo />);
68+
});
69+
expect(container.textContent).toBe('1');
70+
});
71+
});

0 commit comments

Comments
 (0)