Skip to content

Commit eab1bee

Browse files
committed
Concurrent Mode test for uMS render mutation
Same test as the one added in facebook#20665, but for Concurrent Mode.
1 parent bb1b795 commit eab1bee

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,53 @@ describe('useMutableSource', () => {
17301730
param => param.version,
17311731
);
17321732

1733+
let mutatedValueInRender = 0;
1734+
function MutateDuringRead() {
1735+
const value = useMutableSource(
1736+
mutableSource,
1737+
defaultGetSnapshot,
1738+
defaultSubscribe,
1739+
);
1740+
Scheduler.unstable_yieldValue('MutateDuringRead:' + value);
1741+
// Note that mutating an exeternal value during render is a side effect and is not supported.
1742+
source.value = mutatedValueInRender++;
1743+
return null;
1744+
}
1745+
1746+
expect(() => {
1747+
expect(() => {
1748+
act(() => {
1749+
ReactNoop.render(<MutateDuringRead />);
1750+
});
1751+
}).toThrow(
1752+
'Cannot read from mutable source during the current render without tearing. This may be a bug in React. Please file an issue.',
1753+
);
1754+
}).toWarnDev([
1755+
// Warns twice because of the retry-on-error render pass. Should
1756+
// consider only warning during the first attempt, not during the
1757+
// retry. Or maybe vice versa.
1758+
'A mutable source was mutated while the MutateDuringRead component was rendering. This is not supported. ' +
1759+
'Move any mutations into event handlers or effects.\n' +
1760+
' in MutateDuringRead (at **)',
1761+
'A mutable source was mutated while the MutateDuringRead component was rendering. This is not supported. ' +
1762+
'Move any mutations into event handlers or effects.\n' +
1763+
' in MutateDuringRead (at **)',
1764+
]);
1765+
1766+
expect(Scheduler).toHaveYielded([
1767+
'MutateDuringRead:initial',
1768+
'MutateDuringRead:0',
1769+
]);
1770+
});
1771+
1772+
// @gate experimental
1773+
it('should throw if a mutable source is mutated during render (legacy mode)', () => {
1774+
const source = createSource('initial');
1775+
const mutableSource = createMutableSource(
1776+
source,
1777+
param => param.version,
1778+
);
1779+
17331780
function MutateDuringRead() {
17341781
const value = useMutableSource(
17351782
mutableSource,

0 commit comments

Comments
 (0)