Skip to content

Commit

Permalink
Add a regression test for switching from Fragment to a component (#17647
Browse files Browse the repository at this point in the history
)

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

* Add a few more tests
  • Loading branch information
gaearon committed Dec 18, 2019
1 parent 9fe1031 commit 6fef7c4
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactFragment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down

0 comments on commit 6fef7c4

Please sign in to comment.