-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathindex.spec.js
More file actions
executable file
·288 lines (261 loc) · 8.83 KB
/
Copy pathindex.spec.js
File metadata and controls
executable file
·288 lines (261 loc) · 8.83 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* eslint-disable no-undef, react/prop-types, react/no-string-refs */
import React, { Component } from 'react';
import { mount, shallow, render } from 'enzyme';
import { renderToJson } from 'enzyme-to-json';
import Tabs, { TabPane } from '../src';
import TabContent from '../src/TabContent';
import ScrollableInkTabBar from '../src/ScrollableInkTabBar';
import InkTabBar from '../src/InkTabBar';
class NormoalTabs extends Component {
getRoot() {
return this.refs.root;
}
render() {
return (
<Tabs
ref="root"
defaultActiveKey="2"
renderTabBar={() => <ScrollableInkTabBar/>}
renderTabContent={() => <TabContent/>}
>
<TabPane tab="tab 1" key="1">first</TabPane>
<TabPane tab="tab 2" key="2">second</TabPane>
<TabPane tab="tab 3" key="3">third</TabPane>
</Tabs>
);
}
}
describe('rc-tabs', () => {
it('should render Tabs with correct DOM structure', () => {
const wrapper = render(<NormoalTabs/>);
expect(renderToJson(wrapper)).toMatchSnapshot();
});
it('create and nav should work', () => {
const wrapper = mount(<NormoalTabs/>);
expect(wrapper.find('.rc-tabs').length).toBe(1);
expect(wrapper.find('.rc-tabs-tab').length).toBe(3);
});
it('default active should work', () => {
const wrapper = mount(<NormoalTabs/>);
expect(wrapper.find('.rc-tabs-tab').length).toBe(3);
expect(wrapper.instance().getRoot().state.activeKey).toBe('2');
expect(wrapper.find('.rc-tabs-tab').at(1).hasClass('rc-tabs-tab-active')).toBe(true);
});
it('onChange and onTabClick should work', () => {
const handleChange = jest.fn();
const handleTabClick = jest.fn();
const wrapper = mount(
<Tabs
defaultActiveKey="1"
renderTabBar={() => <ScrollableInkTabBar onTabClick={handleTabClick} />}
renderTabContent={() => <TabContent/>}
onChange={handleChange}
>
<TabPane tab="tab 1" key="1">first</TabPane>
<TabPane tab="tab 2" key="2">second</TabPane>
<TabPane tab="tab 3" key="3">third</TabPane>
</Tabs>
);
const targetTab = wrapper.find('.rc-tabs-tab').at(2);
targetTab.simulate('click');
expect(handleTabClick).toHaveBeenCalledWith('3', expect.anything());
expect(handleChange).toHaveBeenCalledWith('3');
});
it('should support custom next icon', () => {
const handleChange = jest.fn();
const handleTabClick = jest.fn();
const renderTabBar = () => (
<ScrollableInkTabBar
onTabClick={handleTabClick}
nextIcon={<span className="next-icon">next-icon</span>}
prevIcon={<span className="prev-icon">prev-icon</span>}
/>
);
const wrapper = mount(
<Tabs
defaultActiveKey="1"
renderTabBar={renderTabBar}
renderTabContent={() => <TabContent/>}
onChange={handleChange}
>
<TabPane tab="tab 1" key="1">first</TabPane>
<TabPane tab="tab 2" key="2">second</TabPane>
<TabPane tab="tab 3" key="3">third</TabPane>
</Tabs>
);
const nextIcon = wrapper.find('.next-icon');
const prevIcon = wrapper.find('.prev-icon');
expect(nextIcon.length).toBe(1);
expect(prevIcon.length).toBe(1);
expect(nextIcon.at(0).text()).toBe('next-icon');
expect(prevIcon.at(0).text()).toBe('prev-icon');
});
it('`onPrevClick` and `onNextClick` should work', (done) => {
const onPrevClick = jest.fn();
const onNextClick = jest.fn();
const wrapper = mount(
<Tabs
defaultActiveKey="1"
style={{ width: 100 }}
renderTabBar={() => (
<ScrollableInkTabBar onPrevClick={onPrevClick} onNextClick={onNextClick} />
)}
renderTabContent={() => <TabContent/>}
>
<TabPane tab="tab 1" key="1">first</TabPane>
<TabPane tab="tab 2" key="2">second</TabPane>
<TabPane tab="tab 3" key="3">third</TabPane>
</Tabs>
);
// To force Tabs show prev/next button
const scrollableTabBarNode = wrapper.find('ScrollableTabBarNode').instance();
scrollableTabBarNode.offset = -1;
jest.spyOn(scrollableTabBarNode, 'getScrollWH').mockImplementation(() => {
return 200;
});
jest.spyOn(scrollableTabBarNode, 'getOffsetWH').mockImplementation((node) => {
if (node.className.indexOf('rc-tabs-nav-container') !== -1) {
return 100;
}
if (node.className.indexOf('rc-tabs-nav-wrap') !== -1) {
return 100;
}
return 0;
});
wrapper.update();
setTimeout(() => {
wrapper.find('.rc-tabs-tab-next').simulate('click');
expect(onNextClick).toHaveBeenCalled();
wrapper.find('.rc-tabs-tab-prev').simulate('click');
expect(onPrevClick).toHaveBeenCalled();
done();
}, 50);
});
it('active first tab when children is changed', () => {
const children = [1, 2, 3]
.map(number => <TabPane tab={number} key={number.toString()}>{number}</TabPane>);
const wrapper = shallow(
<Tabs
renderTabBar={() => <ScrollableInkTabBar />}
renderTabContent={() => <TabContent/>}
>
{children}
</Tabs>
);
expect(wrapper.state().activeKey).toBe('1');
const newChildren = [...children];
newChildren.shift();
wrapper.setProps({
children: newChildren,
});
expect(wrapper.state().activeKey).toBe('2');
});
it('active first tab when children is not changed at controlled mode', () => {
const children = [1, 2, 3]
.map(number => <TabPane tab={number} key={number.toString()}>{number}</TabPane>);
const wrapper = shallow(
<Tabs
activeKey="1"
renderTabBar={() => <ScrollableInkTabBar />}
renderTabContent={() => <TabContent/>}
>
{children}
</Tabs>
);
expect(wrapper.state().activeKey).toBe('1');
const newChildren = [...children];
newChildren.shift();
wrapper.setProps({
children: newChildren,
});
expect(wrapper.state().activeKey).toBe('1');
});
it('activate tab on click should show inkbar', () => {
const children = [1, 2]
.map(number => <TabPane tab={number} key={number.toString()}>{number}</TabPane>);
const wrapper = mount(
<Tabs
renderTabBar={() => <InkTabBar />}
renderTabContent={() => <TabContent/>}
>
{children}
</Tabs>
);
wrapper.find('TabBarTabsNode').find('.rc-tabs-tab').at(1).simulate('click', {});
expect(wrapper.find('InkTabBarNode').html().indexOf('display: block;') !== -1).toBe(true);
});
it('un-activate tab should not show inkbar', (done) => {
const children = [1, 2]
.map(number => <TabPane tab={number} key={number.toString()}>{number}</TabPane>);
const wrapper = mount(
<Tabs
renderTabBar={() => <InkTabBar />}
renderTabContent={() => <TabContent/>}
activeKey="-1"
>
{children}
</Tabs>
);
setTimeout(() => {
expect(wrapper.find('InkTabBarNode').html().indexOf('display: none;') !== -1).toBe(true);
done();
}, 0);
});
it('tabBarGutter should work', () => {
const generateTabBarGutter = tabBarPosition => (
<Tabs
defaultActiveKey="3"
tabBarPosition={tabBarPosition}
renderTabBar={() => (
<InkTabBar
tabBarGutter={40}
/>
)}
renderTabContent={() => <TabContent />}
>
<TabPane tab="tab 1" key="1">first</TabPane>
<TabPane tab="tab 2" key="2">second</TabPane>
</Tabs>
);
let wrapper = mount(generateTabBarGutter('top'));
expect(wrapper.find('.rc-tabs-tab').at(0).instance().style.marginRight).toBe('40px');
wrapper = mount(generateTabBarGutter('left'));
expect(wrapper.find('.rc-tabs-tab').at(0).instance().style.marginBottom).toBe('40px');
});
it('destroy not re-render', (done) => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const Test = ({ visible }) => {
if (!visible) return false;
return (
<Tabs
renderTabBar={() => <InkTabBar />}
renderTabContent={() => <TabContent />}
>
<TabPane tab="tab 1" key="1">first</TabPane>
</Tabs>
);
};
const wrapper = mount(<Test visible />);
wrapper.setProps({ visible: false });
setTimeout(() => {
expect(errorSpy).not.toBeCalled();
errorSpy.mockRestore();
done();
}, 1000);
});
it('string type tab should have [data-tab] attribute', () => {
const wrapper = mount(
<Tabs
defaultActiveKey="2"
renderTabBar={() => <ScrollableInkTabBar/>}
renderTabContent={() => <TabContent/>}
>
<TabPane tab="tab 1" key="1">first</TabPane>
<TabPane tab="tab 2" key="2">second</TabPane>
<TabPane tab="tab 3" key="3">third</TabPane>
<TabPane tab={<div>tab 4</div>} key="4">fourth</TabPane>
</Tabs>
);
expect(wrapper.find('[data-tab]').length).toBe(3);
})
});