Skip to content

Commit d2a0516

Browse files
committed
Add failing test for bailouted actions being preserved in the queue
1 parent 5c2b2c0 commit d2a0516

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,4 +2071,57 @@ describe('ReactHooksWithNoopRenderer', () => {
20712071
expect(Scheduler).toFlushAndYield(['Step: 5, Shadow: 5']);
20722072
expect(ReactNoop).toMatchRenderedOutput('5');
20732073
});
2074+
2075+
it('successful eager bailout should not store dispatched item in the queue', () => {
2076+
let setDisabled;
2077+
let increment;
2078+
2079+
function Counter({disabled}) {
2080+
const [count, dispatch] = useReducer((state, action) => {
2081+
if (disabled) {
2082+
return state;
2083+
}
2084+
if (action.type === 'increment') {
2085+
return state + 1;
2086+
}
2087+
return state;
2088+
}, 0);
2089+
2090+
increment = () => dispatch({type: 'increment'});
2091+
2092+
Scheduler.yieldValue('Render count: ' + count);
2093+
return count;
2094+
}
2095+
2096+
function App() {
2097+
const [disabled, _setDisabled] = useState(true);
2098+
setDisabled = _setDisabled;
2099+
Scheduler.yieldValue('Render disabled: ' + disabled);
2100+
return <Counter disabled={disabled} />;
2101+
}
2102+
2103+
ReactNoop.render(<App />);
2104+
expect(Scheduler).toFlushAndYield([
2105+
'Render disabled: true',
2106+
'Render count: 0',
2107+
]);
2108+
expect(ReactNoop).toMatchRenderedOutput('0');
2109+
2110+
act(() => {
2111+
increment();
2112+
increment();
2113+
increment();
2114+
});
2115+
expect(Scheduler).toFlushAndYield([]);
2116+
expect(ReactNoop).toMatchRenderedOutput('0');
2117+
2118+
act(() => {
2119+
setDisabled(false);
2120+
});
2121+
expect(Scheduler).toFlushAndYield([
2122+
'Render disabled: false',
2123+
'Render count: 0',
2124+
]);
2125+
expect(ReactNoop).toMatchRenderedOutput('0');
2126+
});
20742127
});

0 commit comments

Comments
 (0)