-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchTransition-test.js
129 lines (113 loc) · 3.22 KB
/
SwitchTransition-test.js
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
import React from 'react';
import { mount } from 'enzyme';
import Transition, { ENTERED } from '../src/Transition';
import SwitchTransition, { modes } 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')
};
Parent = function Parent({ on, rendered = true }) {
return (
<SwitchTransition>
{rendered ? (
<Transition timeout={0} key={on ? 'first' : 'second'} {...events}>
<span />
</Transition>
) : null}
</SwitchTransition>
);
};
});
it('should have default status ENTERED', () => {
const wrapper = mount(
<SwitchTransition>
<Transition timeout={0} key="first">
<span />
</Transition>
</SwitchTransition>
);
expect(wrapper.state('status')).toBe(ENTERED);
});
it('should have default mode: out-in', () => {
const wrapper = mount(
<SwitchTransition>
<Transition timeout={0} key="first">
<span />
</Transition>
</SwitchTransition>
);
expect(wrapper.prop('mode')).toBe(modes.out);
});
it('should work without childs', () => {
expect(() => {
mount(
<SwitchTransition>
<Transition timeout={0} key="first">
<span />
</Transition>
</SwitchTransition>
);
}).not.toThrow();
});
it('should switch between components on change state', () => {
const wrapper = mount(<Parent on={true} />);
jest.useFakeTimers();
expect(wrapper.find(SwitchTransition).getElement().props.children.key).toBe(
'first'
);
wrapper.setProps({ on: false });
expect(log).toEqual(['exit', 'exiting']);
jest.runAllTimers();
expect(log).toEqual([
'exit',
'exiting',
'exited',
'enter',
'entering',
'entered'
]);
expect(wrapper.find(SwitchTransition).getElement().props.children.key).toBe(
'second'
);
});
it('should switch between null and component', () => {
const wrapper = mount(<Parent on={true} rendered={false} />);
expect(
wrapper.find(SwitchTransition).getElement().props.children
).toBeFalsy();
jest.useFakeTimers();
wrapper.setProps({ rendered: true });
jest.runAllTimers();
expect(log).toEqual(['enter', 'entering', 'entered']);
expect(
wrapper.find(SwitchTransition).getElement().props.children
).toBeTruthy();
expect(wrapper.find(SwitchTransition).getElement().props.children.key).toBe(
'first'
);
wrapper.setProps({ on: false, rendered: true });
jest.runAllTimers();
expect(log).toEqual([
'enter',
'entering',
'entered',
'exit',
'exiting',
'exited',
'enter',
'entering',
'entered'
]);
expect(wrapper.find(SwitchTransition).getElement().props.children.key).toBe(
'second'
);
});
});