Skip to content

Commit

Permalink
Failing test for Context.Consumer in suspended Suspense
Browse files Browse the repository at this point in the history
See issue #19701.
  • Loading branch information
overlookmotel committed Aug 26, 2020
1 parent 60ba723 commit 2448504
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1778,5 +1778,57 @@ describe('ReactSuspense', () => {
expect(Scheduler).toFlushExpired(['new value']);
expect(root).toMatchRenderedOutput('new value');
});

it('updates context consumer within child of suspended suspense component when context updates', () => {
const {createContext, useState} = React;

const ValueContext = createContext(null);

const promiseThatNeverResolves = new Promise(() => {});
function Child() {
return (
<ValueContext.Consumer>
{value => {
Scheduler.unstable_yieldValue(`Received context value [${value}]`);
if (value === 'default') return <Text text="default" />;
throw promiseThatNeverResolves;
}}
</ValueContext.Consumer>
);
}

let setValue;
function Wrapper({children}) {
const [value, _setValue] = useState('default');
setValue = _setValue;
return (
<ValueContext.Provider value={value}>
{children}
</ValueContext.Provider>
);
}

function App() {
return (
<Wrapper>
<Suspense fallback={<Text text="Loading..." />}>
<Child />
</Suspense>
</Wrapper>
);
}

const root = ReactTestRenderer.create(<App />);
expect(Scheduler).toHaveYielded(['Received context value [default]', 'default']);
expect(root).toMatchRenderedOutput('default');

act(() => setValue('new value'));
expect(Scheduler).toHaveYielded(['Received context value [new value]', 'Loading...']);
expect(root).toMatchRenderedOutput('Loading...');

act(() => setValue('default'));
expect(Scheduler).toHaveYielded(['Received context value [default]', 'default']);
expect(root).toMatchRenderedOutput('default');
});
});
});

0 comments on commit 2448504

Please sign in to comment.