-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix setState ignored in Safari when iframe is added to DOM in the sam…
…e commit (#23111) * Fix setState being ignored in Safari * Add a regression test * Add comment
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/react-dom/src/__tests__/ReactDOMSafariMicrotaskBug-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
|
||
let ReactDOM; | ||
let act; | ||
|
||
describe('ReactDOMSafariMicrotaskBug-test', () => { | ||
let container; | ||
let simulateSafariBug; | ||
|
||
beforeEach(() => { | ||
// In Safari, microtasks don't always run on clean stack. | ||
// This setup crudely approximates it. | ||
// In reality, the sync flush happens when an iframe is added to the page. | ||
// https://github.com/facebook/react/issues/22459 | ||
let queue = []; | ||
window.queueMicrotask = function(cb) { | ||
queue.push(cb); | ||
}; | ||
simulateSafariBug = function() { | ||
queue.forEach(cb => cb()); | ||
queue = []; | ||
}; | ||
|
||
jest.resetModules(); | ||
container = document.createElement('div'); | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
act = require('jest-react').act; | ||
|
||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
it('should be resilient to buggy queueMicrotask', async () => { | ||
let ran = false; | ||
function Foo() { | ||
const [state, setState] = React.useState(0); | ||
return ( | ||
<div | ||
ref={() => { | ||
if (!ran) { | ||
ran = true; | ||
setState(1); | ||
simulateSafariBug(); | ||
} | ||
}}> | ||
{state} | ||
</div> | ||
); | ||
} | ||
const root = ReactDOM.createRoot(container); | ||
await act(async () => { | ||
root.render(<Foo />); | ||
}); | ||
expect(container.textContent).toBe('1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
790b524
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice