Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/SwitchTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class SwitchTransition extends React.Component {
static getDerivedStateFromProps(props, state) {
if (props.children == null) {
return {
status: ENTERED,
current: null,
};
}
Expand Down
43 changes: 43 additions & 0 deletions test/SwitchTransition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,47 @@ describe('SwitchTransition', () => {

expect(container.textContent).toBe('second');
});

it.each([
['out-in', 'exiting'],
['out-in', 'entering'],
['in-out', 'entering'],
['in-out', 'exiting'],
])('handles null children in %s mode while %s', (mode, status) => {
const firstRef = React.createRef();
const secondRef = React.createRef();
const first = (
<Transition nodeRef={firstRef} timeout={10} key="first">
{(state) => <span ref={firstRef}>first: {state}</span>}
</Transition>
);
const second = (
<Transition nodeRef={secondRef} timeout={10} key="second">
{(state) => <span ref={secondRef}>second: {state}</span>}
</Transition>
);
const { container, rerender } = render(
<SwitchTransition mode={mode}>{first}</SwitchTransition>
);

rerender(<SwitchTransition mode={mode}>{second}</SwitchTransition>);
if (status === (mode === 'out-in' ? 'entering' : 'exiting')) {
act(() => {
jest.runOnlyPendingTimers();
});
}
expect(container.textContent).toContain(status);

rerender(<SwitchTransition mode={mode}>{null}</SwitchTransition>);
act(() => {
jest.runAllTimers();
});
expect(container.textContent).toBe('');

rerender(<SwitchTransition mode={mode}>{first}</SwitchTransition>);
act(() => {
jest.runAllTimers();
});
expect(container.textContent).toBe('first: entered');
});
});