|
| 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 | +let ReactDOM; |
| 14 | +let act; |
| 15 | + |
| 16 | +describe('ReactSuspenseEffectsSemanticsDOM', () => { |
| 17 | + beforeEach(() => { |
| 18 | + jest.resetModules(); |
| 19 | + |
| 20 | + React = require('react'); |
| 21 | + ReactDOM = require('react-dom'); |
| 22 | + act = require('jest-react').act; |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not cause a cycle when combined with a render phase update', () => { |
| 26 | + let scheduleSuspendingUpdate; |
| 27 | + |
| 28 | + function App() { |
| 29 | + const [value, setValue] = React.useState(true); |
| 30 | + |
| 31 | + scheduleSuspendingUpdate = () => setValue(!value); |
| 32 | + |
| 33 | + return ( |
| 34 | + <> |
| 35 | + <React.Suspense fallback="Loading..."> |
| 36 | + <ComponentThatCausesBug value={value} /> |
| 37 | + <ComponentThatSuspendsOnUpdate shouldSuspend={!value} /> |
| 38 | + </React.Suspense> |
| 39 | + </> |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + function ComponentThatCausesBug({value}) { |
| 44 | + const [mirroredValue, setMirroredValue] = React.useState(value); |
| 45 | + if (mirroredValue !== value) { |
| 46 | + setMirroredValue(value); |
| 47 | + } |
| 48 | + |
| 49 | + // eslint-disable-next-line no-unused-vars |
| 50 | + const [_, setRef] = React.useState(null); |
| 51 | + |
| 52 | + return <div ref={setRef} />; |
| 53 | + } |
| 54 | + |
| 55 | + const promise = Promise.resolve(); |
| 56 | + |
| 57 | + function ComponentThatSuspendsOnUpdate({shouldSuspend}) { |
| 58 | + if (shouldSuspend) { |
| 59 | + // Fake Suspend |
| 60 | + throw promise; |
| 61 | + } |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + act(() => { |
| 66 | + const root = ReactDOM.createRoot(document.createElement('div')); |
| 67 | + root.render(<App />); |
| 68 | + }); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + scheduleSuspendingUpdate(); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments