Skip to content

Commit

Permalink
Add a failing test for Offscreen in StrictMode
Browse files Browse the repository at this point in the history
  • Loading branch information
sammy-SC committed Aug 5, 2022
1 parent 9fcaf88 commit e84f7d7
Showing 1 changed file with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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([]);
});
}
});

0 comments on commit e84f7d7

Please sign in to comment.