Add a regression test for switching from Fragment to a component (#17647)

* Add a regression test for switching from Fragment to a component

* Add a few more tests
This commit is contained in:
Dan Abramov
2019-12-18 15:21:42 +00:00
committed by GitHub
parent 9fe1031244
commit 6fef7c47a9
@@ -722,6 +722,149 @@ describe('ReactFragment', () => {
expect(ReactNoop.getChildren()).toEqual([div(div(), span())]);
});
it('should not preserve state when switching a nested unkeyed fragment to a passthrough component', function() {
const ops = [];
function Passthrough({children}) {
return children;
}
class Stateful extends React.Component {
componentDidUpdate() {
ops.push('Update Stateful');
}
render() {
return <div>Hello</div>;
}
}
function Foo({condition}) {
return condition ? (
<>
<>
<Stateful />
</>
</>
) : (
<>
<Passthrough>
<Stateful />
</Passthrough>
</>
);
}
ReactNoop.render(<Foo condition={true} />);
expect(Scheduler).toFlushWithoutYielding();
ReactNoop.render(<Foo condition={false} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ops).toEqual([]);
expect(ReactNoop.getChildren()).toEqual([div()]);
ReactNoop.render(<Foo condition={true} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ops).toEqual([]);
expect(ReactNoop.getChildren()).toEqual([div()]);
});
it('should not preserve state when switching a nested keyed fragment to a passthrough component', function() {
const ops = [];
function Passthrough({children}) {
return children;
}
class Stateful extends React.Component {
componentDidUpdate() {
ops.push('Update Stateful');
}
render() {
return <div>Hello</div>;
}
}
function Foo({condition}) {
return condition ? (
<>
<React.Fragment key="a">
<Stateful />
</React.Fragment>
</>
) : (
<>
<Passthrough>
<Stateful />
</Passthrough>
</>
);
}
ReactNoop.render(<Foo condition={true} />);
expect(Scheduler).toFlushWithoutYielding();
ReactNoop.render(<Foo condition={false} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ops).toEqual([]);
expect(ReactNoop.getChildren()).toEqual([div()]);
ReactNoop.render(<Foo condition={true} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ops).toEqual([]);
expect(ReactNoop.getChildren()).toEqual([div()]);
});
it('should not preserve state when switching a nested keyed array to a passthrough component', function() {
const ops = [];
function Passthrough({children}) {
return children;
}
class Stateful extends React.Component {
componentDidUpdate() {
ops.push('Update Stateful');
}
render() {
return <div>Hello</div>;
}
}
function Foo({condition}) {
return condition ? (
<>{[<Stateful key="a" />]}</>
) : (
<>
<Passthrough>
<Stateful />
</Passthrough>
</>
);
}
ReactNoop.render(<Foo condition={true} />);
expect(Scheduler).toFlushWithoutYielding();
ReactNoop.render(<Foo condition={false} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ops).toEqual([]);
expect(ReactNoop.getChildren()).toEqual([div()]);
ReactNoop.render(<Foo condition={true} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ops).toEqual([]);
expect(ReactNoop.getChildren()).toEqual([div()]);
});
it('should preserve state when it does not change positions', function() {
const ops = [];