Skip to content

Commit 093c8a2

Browse files
committed
Add extra passing test for an edge case
Mentioned by @acdlite to watch out for
1 parent cd39503 commit 093c8a2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,27 @@ describe('ReactHooks', () => {
690690
);
691691
});
692692

693+
it('throws when reading context inside useMemo after outside it', () => {
694+
const {useMemo, createContext} = React;
695+
const ReactCurrentDispatcher =
696+
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
697+
.ReactCurrentDispatcher;
698+
699+
const ThemeContext = createContext('light');
700+
let firstRead;
701+
function App() {
702+
firstRead = ReactCurrentDispatcher.current.readContext(ThemeContext);
703+
return useMemo(() => {
704+
return ReactCurrentDispatcher.current.readContext(ThemeContext);
705+
}, []);
706+
}
707+
708+
expect(() => ReactTestRenderer.create(<App />)).toThrow(
709+
'Context can only be read inside the body of a component',
710+
);
711+
expect(firstRead).toBe('light');
712+
});
713+
693714
it('throws when reading context inside useEffect', () => {
694715
const {useEffect, createContext} = React;
695716
const ReactCurrentDispatcher =

0 commit comments

Comments
 (0)