-
Notifications
You must be signed in to change notification settings - Fork 91
/
index.spec.tsx
executable file
·220 lines (210 loc) · 7.06 KB
/
index.spec.tsx
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { mount } from 'enzyme';
import * as React from 'react';
import toJson from 'enzyme-to-json';
import Drawer from '../src';
function Div(props) {
const { show, ...otherProps } = props;
return (
<div className="div-wrapper">
{show && (
<Drawer wrapperClassName="drawer-wrapper" defaultOpen {...otherProps} />
)}
</div>
);
}
function DrawerComp(props: { open?: boolean }) {
return (
<div className="react-wrapper">
<div id="a" style={{ position: 'absolute', top: 0, left: 0 }}>
test1
</div>
<Drawer
getContainer={null}
open={props.open}
wrapperClassName="drawer-wrapper"
/>
</div>
);
}
function createClientXY(x: number, y: number) {
return { clientX: x, clientY: y };
}
function createStartTouchEventObject({ x = 0, y = 0 }) {
return { touches: [createClientXY(x, y)] };
}
function createMoveTouchEventObject({ x = 0, y = 0 }) {
return {
touches: [createClientXY(x, y)],
changedTouches: [createClientXY(x, y)],
};
}
describe('rc-drawer-menu', () => {
let instance;
it('single drawer', () => {
instance = mount(<Drawer onHandleClick={() => {}} />);
const drawer = instance.find('.drawer') as any;
const drawerContent = instance.find('.drawer-content-wrapper') as any;
expect(!!drawer).toBe(true);
expect(drawer.instance().parentNode.parentNode.tagName).toBe('BODY');
console.log('clientWidth:', drawerContent.instance().clientWidth);
expect(drawerContent.instance().style.transform).toBe('translateX(-100%)');
});
it('icon child is element', () => {
instance = mount(
<Drawer
handler={
<div className="a">
<i>a</i>
</div>
}
level={null}
/>,
);
const icon = instance.find('.a');
console.log('icon.length:', icon.length);
expect(icon.length).toBe(1);
});
it('default open drawer', () => {
instance = mount(
<Drawer handler={<i className="a">a</i>} defaultOpen level={[]} />,
);
const drawer = instance.find('.drawer-content-wrapper').instance() as any;
const content = instance.find('.drawer-content');
content.simulate(
'touchStart',
createStartTouchEventObject({ x: 100, y: 0 }),
);
content.simulate(
'touchMove',
createMoveTouchEventObject({ x: 150, y: 10 }),
);
content.simulate('touchEnd', createMoveTouchEventObject({ x: 200, y: 0 }));
content.simulate('touchStart', createStartTouchEventObject({ x: 0, y: 0 }));
content.simulate('touchMove', createMoveTouchEventObject({ x: 0, y: 10 }));
content.simulate('touchEnd', createMoveTouchEventObject({ x: 0, y: 10 }));
console.log('transform is empty:', drawer.style.transform);
expect(drawer.style.transform).toEqual('');
});
it('handler is null,open=true', () => {
instance = mount(<Drawer handler={null} open level={null} />);
expect(toJson(instance.render())).toMatchSnapshot();
});
it('handler is null,open=false', () => {
instance = mount(<Drawer handler={false} open={false} level={null} />);
expect(toJson(instance.render())).toMatchSnapshot();
});
it('switch open drawer', () => {
instance = mount(<DrawerComp />);
jest.useFakeTimers();
const drawer = instance.find('.drawer-content-wrapper').instance() as any;
console.log('第一次:', drawer.style.transform);
expect(drawer.style.transform).toBe('translateX(-100%)');
expect(toJson(instance.render())).toMatchSnapshot();
instance.setProps({
open: true,
});
jest.runAllTimers();
expect(toJson(instance.render())).toMatchSnapshot();
console.log(drawer.style.transform);
expect(drawer.style.transform).toEqual('');
instance.setProps({
open: false,
});
console.log('第二次:', drawer.style.transform);
jest.runAllTimers();
expect(drawer.style.transform).toBe('translateX(-100%)');
expect(toJson(instance.render())).toMatchSnapshot();
jest.useRealTimers();
});
it('getContainer is null', () => {
instance = mount(
<div className="react-wrapper">
<div
id="a"
className="a"
style={{ position: 'absolute', top: 0, left: 0 }}
>
test1
</div>
<Drawer
getContainer={null}
defaultOpen
level="#a"
wrapperClassName="drawer-wrapper"
/>
</div>,
);
const drawer = instance.find('.drawer').instance() as any;
const a = instance.find('#a').instance() as any;
console.log('a transform:', a.style.transform);
expect(a.style.transform).toBe('');
console.log(drawer.parentNode.parentNode.className);
expect(drawer.parentNode.className).toBe('drawer-wrapper');
expect(drawer.parentNode.parentNode.className).toBe('react-wrapper');
expect(toJson(instance.render())).toMatchSnapshot();
});
it('click open close', () => {
instance = mount(<Drawer level="b" levelMove={200} />);
const content = instance.find('.drawer-content-wrapper').instance() as any;
console.log(content.style.transform);
expect(content.style.transform).toBe('translateX(-100%)');
const handle = instance.find('.drawer-handle');
handle.simulate('click');
console.log(content.style.transform);
expect(content.style.transform).toBe('');
const mask = instance.find('.drawer-mask');
mask.simulate('click');
console.log(content.style.transform);
expect(content.style.transform).toBe('translateX(-100%)');
});
it('will unmount', () => {
instance = mount(<Div show />);
const divWrapper = instance.find('.div-wrapper').instance() as any;
const content = instance.find('.drawer-content-wrapper').instance() as any;
console.log(content.style.transform);
expect(content.style.transform).toBe('');
instance.setProps({
show: false,
});
expect(divWrapper.children.length).toBe(0);
});
it('placement change', () => {
instance = mount(<Drawer level={null} />);
const content = instance.find('.drawer-content-wrapper').instance() as any;
console.log(content.style.transform);
expect(content.style.transform).toBe('translateX(-100%)');
instance.setProps({
placement: 'top',
level: ['a', 'b'],
});
console.log(content.style.transform);
expect(content.style.transform).toBe('translateY(-100%)');
});
it('levelMove is Array', () => {
instance = mount(<Drawer handler={null} levelMove={[200, 0]} />);
expect(toJson(instance.render())).toMatchSnapshot();
instance.setProps({
open: true,
});
instance.setProps({
open: false,
levelMove: () => 200,
});
instance.setProps({
open: true,
levelMove: [200],
});
expect(toJson(instance.render())).toMatchSnapshot();
});
it('handler is null, render is null', () => {
instance = mount(<Drawer handler={null} levelMove={200} />);
expect(toJson(instance.render())).toMatchSnapshot();
});
it('getContainer', () => {
instance = mount(<Div show getContainer={false} />);
instance.setProps({
show: false,
});
expect(toJson(instance.render())).toMatchSnapshot();
});
});