Skip to content

Commit 2448504

Browse files
committed
Failing test for Context.Consumer in suspended Suspense
See issue #19701.
1 parent 60ba723 commit 2448504

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

+52
Original file line numberDiff line numberDiff line change
@@ -1778,5 +1778,57 @@ describe('ReactSuspense', () => {
17781778
expect(Scheduler).toFlushExpired(['new value']);
17791779
expect(root).toMatchRenderedOutput('new value');
17801780
});
1781+
1782+
it('updates context consumer within child of suspended suspense component when context updates', () => {
1783+
const {createContext, useState} = React;
1784+
1785+
const ValueContext = createContext(null);
1786+
1787+
const promiseThatNeverResolves = new Promise(() => {});
1788+
function Child() {
1789+
return (
1790+
<ValueContext.Consumer>
1791+
{value => {
1792+
Scheduler.unstable_yieldValue(`Received context value [${value}]`);
1793+
if (value === 'default') return <Text text="default" />;
1794+
throw promiseThatNeverResolves;
1795+
}}
1796+
</ValueContext.Consumer>
1797+
);
1798+
}
1799+
1800+
let setValue;
1801+
function Wrapper({children}) {
1802+
const [value, _setValue] = useState('default');
1803+
setValue = _setValue;
1804+
return (
1805+
<ValueContext.Provider value={value}>
1806+
{children}
1807+
</ValueContext.Provider>
1808+
);
1809+
}
1810+
1811+
function App() {
1812+
return (
1813+
<Wrapper>
1814+
<Suspense fallback={<Text text="Loading..." />}>
1815+
<Child />
1816+
</Suspense>
1817+
</Wrapper>
1818+
);
1819+
}
1820+
1821+
const root = ReactTestRenderer.create(<App />);
1822+
expect(Scheduler).toHaveYielded(['Received context value [default]', 'default']);
1823+
expect(root).toMatchRenderedOutput('default');
1824+
1825+
act(() => setValue('new value'));
1826+
expect(Scheduler).toHaveYielded(['Received context value [new value]', 'Loading...']);
1827+
expect(root).toMatchRenderedOutput('Loading...');
1828+
1829+
act(() => setValue('default'));
1830+
expect(Scheduler).toHaveYielded(['Received context value [default]', 'default']);
1831+
expect(root).toMatchRenderedOutput('default');
1832+
});
17811833
});
17821834
});

0 commit comments

Comments
 (0)