-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a failing test for Offscreen in StrictMode
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
packages/react-reconciler/src/__tests__/ReactOffscreenStrictMode-test.internal.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,84 @@ | ||
let React; | ||
let Offscreen; | ||
let ReactDOMClient; | ||
let act; | ||
|
||
describe('ReactOffscreenStrictMode', () => { | ||
let log; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
log = []; | ||
|
||
React = require('react'); | ||
Offscreen = React.unstable_Offscreen; | ||
ReactDOMClient = require('react-dom/client'); | ||
act = require('jest-react').act; | ||
|
||
const ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.enableStrictEffects = __DEV__; | ||
}); | ||
|
||
function Component({ label }) { | ||
React.useEffect(() => { | ||
log.push(`${label}: useEffect mount`); | ||
return () => log.push(`${label}: useEffect unmount`); | ||
}); | ||
|
||
React.useLayoutEffect(() => { | ||
log.push(`${label}: useLayoutEffect mount`); | ||
return () => log.push(`${label}: useLayoutEffect unmount`); | ||
}); | ||
|
||
log.push(`${label}: render`); | ||
|
||
return null; | ||
} | ||
|
||
if (__DEV__) { | ||
// @gate enableOffscreen | ||
it('should trigger strict effects when offscreen is visible', () => { | ||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container, { | ||
unstable_strictMode: true, | ||
}); | ||
|
||
act(() => { | ||
root.render( | ||
<Offscreen mode="visible"> | ||
<Component label="A" /> | ||
</Offscreen> | ||
); | ||
}); | ||
|
||
expect(log).toEqual([ | ||
'A: render', | ||
'A: render', | ||
'A: useLayoutEffect mount', | ||
'A: useEffect mount', | ||
'A: useLayoutEffect unmount', | ||
'A: useEffect unmount', | ||
'A: useLayoutEffect mount', | ||
'A: useEffect mount', | ||
]); | ||
}); | ||
|
||
// @gate enableOffscreen | ||
it('should not trigger strict effects when offscreen is hidden', () => { | ||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container, { | ||
unstable_strictMode: true, | ||
}); | ||
|
||
act(() => { | ||
root.render( | ||
<Offscreen mode="hidden"> | ||
<Component label="A" /> | ||
</Offscreen> | ||
); | ||
}); | ||
|
||
expect(log).toEqual([]); | ||
}); | ||
} | ||
}); |