-
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.
Skip double invoking effects in Offscreen
- Loading branch information
Showing
2 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
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
100 changes: 100 additions & 0 deletions
100
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,100 @@ | ||
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(['A: render', 'A: render']); | ||
}); | ||
} | ||
|
||
// @gate enableOffscreen | ||
it('should default to no strinct effects', () => { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
|
||
act(() => { | ||
root.render( | ||
<Offscreen mode="hidden"> | ||
<Component label="A" /> | ||
</Offscreen>, | ||
); | ||
}); | ||
|
||
expect(log).toEqual(['A: render']); | ||
}); | ||
}); |
typo "strinct" -> "strict"