Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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