1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/**
* @jest-environment jsdom
*/
let React;
let TransitionGroup;
let Transition;
Transition = require('../src/Transition').default;
TransitionGroup = require('../src/TransitionGroup');
const testUtils = require('./utils');
beforeEach(() => {
React = require('react');
act = testUtils.act;
render = testUtils.render;
renderStrict = (element, container) =>
render(<React.StrictMode>{element}</React.StrictMode>, { container });
container = document.createElement('div');
log = [];
let events = {
onEnter: (m) => log.push(m ? 'appear' : 'enter'),
onEntering: (m) => log.push(m ? 'appearing' : 'entering'),
onEntered: (m) => log.push(m ? 'appeared' : 'entered'),
onExit: () => log.push('exit'),
onExiting: () => log.push('exiting'),
onExited: () => log.push('exited'),
};
const nodeRef = React.createRef();
Child = function Child(props) {
return (
<Transition nodeRef={nodeRef} timeout={0} {...props} {...events}>
<span ref={nodeRef} />
</Transition>
);
};
});
// Most of the real functionality is covered in other unit tests, this just
// makes sure we're wired up correctly.
describe('TransitionGroup', () => {
let act, container, log, Child, renderStrict, render;
it('should allow null components', () => {
function FirstChild(props) {
const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null;
}
render(
<TransitionGroup component={FirstChild}>
<Child />
</TransitionGroup>
);
});
it('should allow callback refs', () => {
const ref = jest.fn();
class Child extends React.Component {
render() {
return <span />;
}
}
render(
<TransitionGroup>
<Child ref={ref} />
</TransitionGroup>
);
expect(ref).toHaveBeenCalled();
});
it('should work with no children', () => {
renderStrict(<TransitionGroup />, container);
});
it('should handle transitioning correctly', () => {
function Parent({ count = 1 }) {
let children = [];
for (let i = 0; i < count; i++) children.push(<Child key={i} />);
return (
<TransitionGroup appear enter exit>
{children}
</TransitionGroup>
);
}
jest.useFakeTimers();
renderStrict(<Parent />, container);
act(() => {
jest.runAllTimers();
});
expect(log).toEqual(
// React 18 StrictEffects will call `componentDidMount` twice causing two `onEnter` calls.
React.useTransition !== undefined
? ['appear', 'appear', 'appearing', 'appeared']
: ['appear', 'appearing', 'appeared']
);
log = [];
renderStrict(<Parent count={2} />, container);
act(() => {
jest.runAllTimers();
});
expect(log).toEqual(
// React 18 StrictEffects will call `componentDidMount` twice causing two `onEnter` calls.
React.useTransition !== undefined
? ['enter', 'enter', 'entering', 'entered']
: ['enter', 'entering', 'entered']
);
log = [];
renderStrict(<Parent count={1} />, container);
act(() => {
jest.runAllTimers();
});
expect(log).toEqual(['exit', 'exiting', 'exited']);
});
});
|