File size: 4,128 Bytes
1e92f2d |
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import React from 'react';
import { act, render } from './utils';
import Transition, { ENTERED } from '../src/Transition';
import SwitchTransition from '../src/SwitchTransition';
describe('SwitchTransition', () => {
let log, Parent;
beforeEach(() => {
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();
Parent = function Parent({ on, rendered = true }) {
return (
<SwitchTransition>
{rendered ? (
<Transition
nodeRef={nodeRef}
timeout={0}
key={on ? 'first' : 'second'}
{...events}
>
<span ref={nodeRef}>{on ? 'first' : 'second'}</span>
</Transition>
) : null}
</SwitchTransition>
);
};
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('should have default status ENTERED', () => {
const nodeRef = React.createRef();
render(
<SwitchTransition>
<Transition nodeRef={nodeRef} timeout={0} key="first">
{(status) => {
return <span ref={nodeRef}>status: {status}</span>;
}}
</Transition>
</SwitchTransition>
);
expect(nodeRef.current.textContent).toBe(`status: ${ENTERED}`);
});
it('should have default mode: out-in', () => {
const firstNodeRef = React.createRef();
const secondNodeRef = React.createRef();
const { rerender } = render(
<SwitchTransition>
<Transition nodeRef={firstNodeRef} timeout={0} key="first">
{(status) => {
return <span ref={firstNodeRef}>first status: {status}</span>;
}}
</Transition>
</SwitchTransition>
);
rerender(
<SwitchTransition>
<Transition nodeRef={secondNodeRef} timeout={0} key="second">
{(status) => {
return <span ref={secondNodeRef}>second status: {status}</span>;
}}
</Transition>
</SwitchTransition>
);
expect(firstNodeRef.current.textContent).toBe('first status: exiting');
expect(secondNodeRef.current).toBe(null);
});
it('should work without childs', () => {
const nodeRef = React.createRef();
expect(() => {
render(
<SwitchTransition>
<Transition nodeRef={nodeRef} timeout={0} key="first">
<span ref={nodeRef} />
</Transition>
</SwitchTransition>
);
}).not.toThrow();
});
it('should switch between components on change state', () => {
const { container, setProps } = render(<Parent on={true} />);
expect(container.textContent).toBe('first');
setProps({ on: false });
expect(log).toEqual(['exit', 'exiting']);
act(() => {
jest.runAllTimers();
});
act(() => {
jest.runAllTimers();
});
expect(log).toEqual([
'exit',
'exiting',
'exited',
'enter',
'entering',
'entered',
]);
expect(container.textContent).toBe('second');
});
it('should switch between null and component', () => {
const { container, setProps } = render(
<Parent on={true} rendered={false} />
);
expect(container.textContent).toBe('');
jest.useFakeTimers();
setProps({ rendered: true });
act(() => {
jest.runAllTimers();
});
expect(log).toEqual(['enter', 'entering', 'entered']);
expect(container.textContent).toBe('first');
setProps({ on: false, rendered: true });
act(() => {
jest.runAllTimers();
});
act(() => {
jest.runAllTimers();
});
expect(log).toEqual([
'enter',
'entering',
'entered',
'exit',
'exiting',
'exited',
'enter',
'entering',
'entered',
]);
expect(container.textContent).toBe('second');
});
});
|