mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
@@ -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 = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user