Skip to content

Commit

Permalink
Concurrent Mode test for uMS render mutation
Browse files Browse the repository at this point in the history
Same test as the one added in facebook#20665, but for Concurrent Mode.
  • Loading branch information
acdlite committed Jan 30, 2021
1 parent bb1b795 commit eab1bee
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,53 @@ describe('useMutableSource', () => {
param => param.version,
);

let mutatedValueInRender = 0;
function MutateDuringRead() {
const value = useMutableSource(
mutableSource,
defaultGetSnapshot,
defaultSubscribe,
);
Scheduler.unstable_yieldValue('MutateDuringRead:' + value);
// Note that mutating an exeternal value during render is a side effect and is not supported.
source.value = mutatedValueInRender++;
return null;
}

expect(() => {
expect(() => {
act(() => {
ReactNoop.render(<MutateDuringRead />);
});
}).toThrow(
'Cannot read from mutable source during the current render without tearing. This may be a bug in React. Please file an issue.',
);
}).toWarnDev([
// Warns twice because of the retry-on-error render pass. Should
// consider only warning during the first attempt, not during the
// retry. Or maybe vice versa.
'A mutable source was mutated while the MutateDuringRead component was rendering. This is not supported. ' +
'Move any mutations into event handlers or effects.\n' +
' in MutateDuringRead (at **)',
'A mutable source was mutated while the MutateDuringRead component was rendering. This is not supported. ' +
'Move any mutations into event handlers or effects.\n' +
' in MutateDuringRead (at **)',
]);

expect(Scheduler).toHaveYielded([
'MutateDuringRead:initial',
'MutateDuringRead:0',
]);
});

// @gate experimental
it('should throw if a mutable source is mutated during render (legacy mode)', () => {
const source = createSource('initial');
const mutableSource = createMutableSource(
source,
param => param.version,
);

function MutateDuringRead() {
const value = useMutableSource(
mutableSource,
Expand Down

0 comments on commit eab1bee

Please sign in to comment.