Skip to content

Commit aeee2bf

Browse files
committed
Add regression test for update queue bug
This test passes in the old legacy update queue implementation. I'm adding this before the refactor to prevent a regression.
1 parent 89ca3cf commit aeee2bf

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -717,4 +717,52 @@ describe('ReactIncrementalUpdates', () => {
717717
]);
718718
expect(root).toMatchRenderedOutput('ABCD');
719719
});
720+
721+
it("base state of update queue is initialized to its fiber's memoized state", async () => {
722+
// This test is very weird because it tests an implementation detail but
723+
// is tested in terms of public APIs. When it was originally written, the
724+
// test failed because the update queue was initialized to the state of
725+
// the alternate fiber.
726+
let app;
727+
class App extends React.Component {
728+
state = {prevProp: 'A', count: 0};
729+
static getDerivedStateFromProps(props, state) {
730+
// Add 100 whenever the label prop changes. The prev label is stored
731+
// in state. If the state is dropped incorrectly, we'll fail to detect
732+
// prop changes.
733+
if (props.prop !== state.prevProp) {
734+
return {
735+
prevProp: props.prop,
736+
count: state.count + 100,
737+
};
738+
}
739+
return null;
740+
}
741+
render() {
742+
app = this;
743+
return this.state.count;
744+
}
745+
}
746+
747+
const root = ReactNoop.createRoot();
748+
await ReactNoop.act(async () => {
749+
root.render(<App prop="A" />);
750+
});
751+
expect(root).toMatchRenderedOutput('0');
752+
753+
// Changing the prop causes the count to increase by 100
754+
await ReactNoop.act(async () => {
755+
root.render(<App prop="B" />);
756+
});
757+
expect(root).toMatchRenderedOutput('100');
758+
759+
// Now increment the count by 1 with a state update. And, in the same
760+
// batch, change the prop back to its original value.
761+
await ReactNoop.act(async () => {
762+
root.render(<App prop="A" />);
763+
app.setState(state => ({count: state.count + 1}));
764+
});
765+
// There were two total prop changes, plus an increment.
766+
expect(root).toMatchRenderedOutput('201');
767+
});
720768
});

0 commit comments

Comments
 (0)