/
Tabs.spec.js
549 lines (496 loc) · 15.5 KB
/
Tabs.spec.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// @flow
import React from 'react';
import { assert } from 'chai';
import { spy, stub, useFakeTimers } from 'sinon';
import scroll from 'scroll';
import { createShallow, createMount, getClasses, unwrap } from '../test-utils';
import consoleErrorMock from '../../test/utils/consoleErrorMock';
import Tabs from './Tabs';
import TabScrollButton from './TabScrollButton';
import TabIndicator from './TabIndicator';
import Tab from './Tab';
const TabsNaked = unwrap(Tabs);
const noop = () => {};
const fakeTabs = {
getBoundingClientRect: () => ({}),
children: [
{
children: [
{
getBoundingClientRect: () => ({}),
},
],
},
],
};
describe('<Tabs />', () => {
let mount;
let shallow;
let classes;
before(() => {
shallow = createShallow({ untilSelector: 'Tabs' });
classes = getClasses(<Tabs onChange={noop} value={0} />);
mount = createMount();
});
after(() => {
mount.cleanUp();
});
it('should render with the root class', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0}>
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper.name(), 'div');
assert.strictEqual(wrapper.hasClass(classes.root), true);
});
describe('prop: className', () => {
it('should render with the user and root classes', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} className="woofTabs">
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper.hasClass('woofTabs'), true);
assert.strictEqual(wrapper.hasClass(classes.root), true);
});
});
describe('prop: centered', () => {
it('should render with the centered class', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} centered>
<Tab />
<Tab />
</Tabs>,
);
const selector = `.${classes.flexContainer}.${classes.centered}`;
assert.strictEqual(wrapper.find(selector).is('div'), true, 'should be a div');
assert.strictEqual(wrapper.find(selector).length, 1, 'should only be one');
});
});
describe('prop: children', () => {
it('should accept an invalid child', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0}>
{null}
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper.find(Tab).length, 1);
});
it('should support empty children', () => {
const wrapper = mount(<Tabs width="md" onChange={noop} value={1} />);
assert.strictEqual(wrapper.find('EventListener').length, 1);
});
});
describe('prop: value', () => {
let wrapper;
before(() => {
wrapper = shallow(
<Tabs width="md" onChange={noop} value={1}>
<Tab />
<Tab />
</Tabs>,
);
});
after(() => {
consoleErrorMock.reset();
});
it('should pass selected prop to children', () => {
assert.strictEqual(
wrapper
.find(Tab)
.at(0)
.props().selected,
false,
'should have selected to false',
);
assert.strictEqual(
wrapper
.find(Tab)
.at(1)
.props().selected,
true,
'should have selected',
);
});
it('should switch from the original value', () => {
wrapper.setProps({ value: 0 });
assert.strictEqual(
wrapper
.find(Tab)
.at(0)
.props().selected,
true,
'should have switched to true',
);
assert.strictEqual(
wrapper
.find(Tab)
.at(1)
.props().selected,
false,
'should have switched to false',
);
});
describe('indicator', () => {
it('should accept a false value', () => {
const wrapper2 = mount(
<Tabs width="md" onChange={noop} value={false}>
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper2.find(TabIndicator).props().style.width, 0);
});
it('should let the selected <Tab /> render the indicator', () => {
const wrapper2 = shallow(
<Tabs width="md" onChange={noop} value={1}>
<Tab />
<Tab />
</Tabs>,
{ disableLifecycleMethods: true },
);
assert.strictEqual(
wrapper2
.find(Tab)
.at(0)
.props().indicator,
false,
);
assert.strictEqual(
wrapper2
.find(Tab)
.at(1)
.props().indicator.type,
TabIndicator,
);
});
it('should render the indicator', () => {
const wrapper2 = mount(
<Tabs width="md" onChange={noop} value={1}>
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(
wrapper2
.find(Tab)
.at(0)
.props().indicator,
false,
);
assert.strictEqual(
wrapper2
.find(Tab)
.at(1)
.props().indicator,
false,
);
assert.strictEqual(wrapper2.find(TabIndicator).length, 1);
});
it('should update the indicator state no matter what', () => {
const wrapper2 = mount(
<TabsNaked width="md" onChange={noop} value={1} classes={{}} theme={{}}>
<Tab />
<Tab />
</TabsNaked>,
);
const instance = wrapper2.instance();
stub(instance, 'scrollSelectedIntoView');
wrapper2.setState({
indicatorStyle: {
left: 10,
width: 40,
},
});
wrapper2.setProps({
value: 0,
});
assert.strictEqual(
instance.scrollSelectedIntoView.callCount >= 2,
true,
'should have called scrollSelectedIntoView',
);
});
});
it('should warn when the value is invalid', () => {
consoleErrorMock.spy();
mount(
<Tabs width="md" onChange={noop} value={2}>
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(consoleErrorMock.callCount(), 2);
assert.strictEqual(
consoleErrorMock.args()[0][0],
'Warning: Material-UI: the value provided `2` is invalid',
);
});
});
describe('prop: onChange', () => {
it('should call onChange when clicking', () => {
const handleChange = spy();
// use mount to ensure that click event on Tab can be fired
const wrapper = mount(
<Tabs width="md" value={0} onChange={handleChange}>
<Tab />
<Tab />
</Tabs>,
);
wrapper
.find(Tab)
.at(1)
.simulate('click');
wrapper.setProps({ value: 1 });
assert.strictEqual(handleChange.callCount, 1, 'should have been called once');
assert.strictEqual(handleChange.args[0][1], 1, 'should have been called with value 1');
wrapper.unmount();
});
});
describe('prop: scrollable', () => {
let clock;
let wrapper;
before(() => {
clock = useFakeTimers();
wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable>
<Tab />
<Tab />
</Tabs>,
);
});
after(() => {
clock.restore();
});
it('should render with the scrollable class', () => {
const selector = `.${classes.scrollingContainer}.${classes.scrollable}`;
assert.strictEqual(wrapper.find(selector).is('div'), true, 'should be a div');
assert.strictEqual(wrapper.find(selector).length, 1, 'should only be one');
});
it('should response to scroll events', () => {
const instance = wrapper.instance();
instance.tabs = { scrollLeft: 100, ...fakeTabs };
spy(instance, 'updateScrollButtonState');
const selector = `.${classes.scrollingContainer}.${classes.scrollable}`;
wrapper.find(selector).simulate('scroll');
clock.tick(166);
assert.strictEqual(
instance.updateScrollButtonState.called,
true,
'should have called updateScrollButtonState',
);
});
it('should get a scrollbar size listener', () => {
// use mount to ensure that handleScrollbarSizeChange gets covered
const mountWrapper = mount(
<Tabs width="md" onChange={noop} value={0} scrollable>
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(mountWrapper.find('ScrollbarSize').length, 1, 'should be one');
mountWrapper.unmount();
});
});
describe('prop: !scrollable', () => {
it('should not render with the scrollable class', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0}>
<Tab />
<Tab />
</Tabs>,
);
const baseSelector = `.${classes.scrollingContainer}`;
const selector = `.${classes.scrollingContainer}.${classes.scrollable}`;
assert.strictEqual(wrapper.find(baseSelector).length, 1, 'base selector should exist');
assert.strictEqual(wrapper.find(selector).length, 0, 'scrolling selector should not exist');
});
});
describe('prop: scrollButtons', () => {
let clock;
before(() => {
clock = useFakeTimers();
});
after(() => {
clock.restore();
});
it('should render scroll buttons', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable scrollButtons="on">
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper.find(TabScrollButton).length, 2, 'should be two');
});
it('should render scroll buttons automatically', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable scrollButtons="auto">
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper.find(TabScrollButton).length, 2, 'should be two');
});
it('should should not render scroll buttons automatically', () => {
const wrapper = shallow(
<Tabs width="sm" onChange={noop} value={0} scrollable scrollButtons="auto">
<Tab />
<Tab />
</Tabs>,
);
assert.strictEqual(wrapper.find(TabScrollButton).length, 2, 'should be zero');
assert.strictEqual(
wrapper.find(TabScrollButton).everyWhere(node => node.hasClass(classes.buttonAuto)),
true,
);
});
it('should handle window resize event', () => {
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable scrollButtons="on">
<Tab />
<Tab />
</Tabs>,
);
const instance = wrapper.instance();
stub(instance, 'updateScrollButtonState');
stub(instance, 'updateIndicatorState');
wrapper
.find('EventListener')
.at(0)
.simulate('resize');
clock.tick(166);
assert.strictEqual(
instance.updateScrollButtonState.called,
true,
'should have called updateScrollButtonState',
);
assert.strictEqual(
instance.updateIndicatorState.called,
true,
'should have called updateIndicatorState',
);
});
describe('scroll button visibility states', () => {
let wrapper;
let instance;
before(() => {
wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable scrollButtons="on">
<Tab />
<Tab />
</Tabs>,
);
instance = wrapper.instance();
});
it('should set neither left nor right scroll button state', () => {
instance.tabs = { scrollLeft: 0, scrollWidth: 90, clientWidth: 100, ...fakeTabs };
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, false, 'left scroll should be false');
assert.strictEqual(wrapper.state().showRightScroll, false, 'right scroll should be false');
});
it('should set only left scroll button state', () => {
instance.tabs = { scrollLeft: 1, ...fakeTabs };
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, true, 'left scroll should be true');
assert.strictEqual(wrapper.state().showRightScroll, false, 'right scroll should be false');
});
it('should set only right scroll button state', () => {
instance.tabs = { scrollLeft: 0, scrollWidth: 110, clientWidth: 100, ...fakeTabs };
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, false, 'left scroll should be false');
assert.strictEqual(wrapper.state().showRightScroll, true, 'right scroll should be true');
});
it('should set both left and right scroll button state', () => {
instance.tabs = { scrollLeft: 1, scrollWidth: 110, clientWidth: 100, ...fakeTabs };
instance.updateScrollButtonState();
assert.strictEqual(wrapper.state().showLeftScroll, true, 'left scroll should be true');
assert.strictEqual(wrapper.state().showRightScroll, true, 'right scroll should be true');
});
});
});
describe('scroll button behavior', () => {
let instance;
let wrapper;
let scrollSpy;
const dimensions = { scrollLeft: 100, clientWidth: 200, scrollWidth: 1000 };
before(() => {
wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable scrollButtons="on">
<Tab />
<Tab />
</Tabs>,
);
instance = wrapper.instance();
instance.tabs = dimensions;
scrollSpy = spy(instance, 'moveTabsScroll');
});
it('should call moveTabsScroll', () => {
wrapper
.find(TabScrollButton)
.at(0)
.simulate('click');
assert.strictEqual(
scrollSpy.args[0][0],
-dimensions.clientWidth,
`should be called with -${dimensions.clientWidth}`,
);
wrapper
.find(TabScrollButton)
.at(1)
.simulate('click');
assert.strictEqual(
scrollSpy.args[1][0],
dimensions.clientWidth,
`should be called with ${dimensions.clientWidth}`,
);
});
});
describe('scroll into view behavior', () => {
let scrollStub;
let instance;
let metaStub;
beforeEach(() => {
scrollStub = stub(scroll, 'left');
const wrapper = shallow(
<Tabs width="md" onChange={noop} value={0} scrollable>
<Tab />
<Tab />
</Tabs>,
);
instance = wrapper.instance();
metaStub = stub(instance, 'getTabsMeta');
});
afterEach(() => {
scroll.left.restore();
});
it('should scroll left tab into view', () => {
metaStub.returns({
tabsMeta: { left: 0, right: 100, scrollLeft: 10 },
tabMeta: { left: -10, right: 10 },
});
instance.scrollSelectedIntoView();
assert.strictEqual(scrollStub.args[0][1], 0, 'should scroll to 0 position');
});
it('should scroll right tab into view', () => {
metaStub.returns({
tabsMeta: { left: 0, right: 100, scrollLeft: 0 },
tabMeta: { left: 90, right: 110 },
});
instance.scrollSelectedIntoView();
assert.strictEqual(scrollStub.args[0][1], 10, 'should scroll to 10 position');
});
it('should support value=false', () => {
metaStub.returns({
tabsMeta: { left: 0, right: 100, scrollLeft: 0 },
tabMeta: undefined,
});
instance.scrollSelectedIntoView();
assert.strictEqual(scrollStub.callCount, 0, 'should not scroll');
});
});
});