Debian Codegraph @

packages/widgets/tests/src/tabbar.spec.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2017, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { expect } from 'chai';
11
12 import { range } from '@lumino/algorithm';
13
14 import { Message, MessageLoop } from '@lumino/messaging';
15
16 import { TabBar, Title, Widget } from '@lumino/widgets';
17
18 import { VirtualDOM, VirtualElement } from '@lumino/virtualdom';
19
20 class LogTabBar extends TabBar<Widget> {
21 events: string[] = [];
22
23 methods: string[] = [];
24
25 handleEvent(event: Event): void {
26 super.handleEvent(event);
27 this.events.push(event.type);
28 }
29
30 protected onBeforeAttach(msg: Message): void {
31 super.onBeforeAttach(msg);
32 this.methods.push('onBeforeAttach');
33 }
34
35 protected onAfterDetach(msg: Message): void {
36 super.onAfterDetach(msg);
37 this.methods.push('onAfterDetach');
38 }
39
40 protected onUpdateRequest(msg: Message): void {
41 super.onUpdateRequest(msg);
42 this.methods.push('onUpdateRequest');
43 }
44 }
45
46 function populateBar(bar: TabBar<Widget>): void {
47 // Add some tabs with labels.
48 for (const i of range(3)) {
49 let widget = new Widget();
50 widget.title.label = `Test - ${i}`;
51 widget.title.closable = true;
52 bar.addTab(widget.title);
53 }
54 // Force the tabs to render
55 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
56 // Add the close icon content.
57 for (const i of range(3)) {
58 let tab = bar.contentNode.children[i];
59 let icon = tab.querySelector(bar.renderer.closeIconSelector);
60 icon!.textContent = 'X';
61 }
62 }
63
64 type Action = 'pointerdown' | 'pointermove' | 'pointerup' | 'dblclick';
65
66 type Direction = 'left' | 'right' | 'up' | 'down';
67
68 function startDrag(
69 bar: LogTabBar,
70 index = 0,
71 direction: Direction = 'right'
72 ): void {
73 bar.tabsMovable = true;
74 let tab = bar.contentNode.children[index] as HTMLElement;
75 bar.currentIndex = index;
76 // Force an update.
77 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
78 let called = false;
79 bar.tabDetachRequested.connect((sender, args) => {
80 called = true;
81 });
82 let rect = bar.contentNode.getBoundingClientRect();
83 let args: any;
84 switch (direction) {
85 case 'left':
86 args = { clientX: rect.left - 200, clientY: rect.top };
87 break;
88 case 'up':
89 args = { clientX: rect.left, clientY: rect.top - 200 };
90 break;
91 case 'down':
92 args = { clientX: rect.left, clientY: rect.bottom + 200 };
93 break;
94 default:
95 args = { clientX: rect.right + 200, clientY: rect.top };
96 break;
97 }
98 simulateOnNode(tab, 'pointerdown');
99 document.body.dispatchEvent(
100 new PointerEvent('pointermove', {
101 ...args,
102 cancelable: true
103 })
104 );
105 expect(called).to.equal(true);
106 bar.events = [];
107 }
108
109 function simulateOnNode(node: Element, action: Action): void {
110 let rect = node.getBoundingClientRect();
111 node.dispatchEvent(
112 new PointerEvent(action, {
113 clientX: rect.left + 1,
114 clientY: rect.top + 1,
115 cancelable: true,
116 bubbles: true
117 })
118 );
119 }
120
121 describe('@lumino/widgets', () => {
122 describe('TabBar', () => {
123 let bar: LogTabBar;
124
125 beforeEach(() => {
126 bar = new LogTabBar();
127 Widget.attach(bar, document.body);
128 });
129
130 afterEach(() => {
131 bar.dispose();
132 });
133
134 describe('#constructor()', () => {
135 it('should take no arguments', () => {
136 let newBar = new TabBar<Widget>();
137 expect(newBar).to.be.an.instanceof(TabBar);
138 });
139
140 it('should take an options argument', () => {
141 let renderer = new TabBar.Renderer();
142 let newBar = new TabBar<Widget>({
143 orientation: 'horizontal',
144 tabsMovable: true,
145 allowDeselect: true,
146 addButtonEnabled: true,
147 insertBehavior: 'select-tab',
148 removeBehavior: 'select-previous-tab',
149 renderer
150 });
151 expect(newBar).to.be.an.instanceof(TabBar);
152 expect(newBar.tabsMovable).to.equal(true);
153 expect(newBar.renderer).to.equal(renderer);
154 expect(newBar.addButtonEnabled).to.equal(true);
155 });
156
157 it('should add the `lm-TabBar` class', () => {
158 let newBar = new TabBar<Widget>();
159 expect(newBar.hasClass('lm-TabBar')).to.equal(true);
160 });
161 });
162
163 describe('#dispose()', () => {
164 it('should dispose of the resources held by the widget', () => {
165 bar.dispose();
166 expect(bar.isDisposed).to.equal(true);
167 bar.dispose();
168 expect(bar.isDisposed).to.equal(true);
169 });
170 });
171
172 describe('#currentChanged', () => {
173 it('should be emitted when the current tab is changed', () => {
174 populateBar(bar);
175 let called = false;
176 let titles = bar.titles;
177 bar.currentChanged.connect((sender, args) => {
178 expect(sender).to.equal(bar);
179 expect(args.previousIndex).to.equal(0);
180 expect(args.previousTitle).to.equal(titles[0]);
181 expect(args.currentIndex).to.equal(1);
182 expect(args.currentTitle).to.equal(titles[1]);
183 called = true;
184 });
185 bar.currentTitle = titles[1];
186 expect(called).to.equal(true);
187 });
188
189 it('should not be emitted when another tab is inserted', () => {
190 populateBar(bar);
191 let called = false;
192 bar.currentChanged.connect((sender, args) => {
193 called = true;
194 });
195 let widget = new Widget();
196 bar.insertTab(0, widget.title);
197 expect(called).to.equal(false);
198 });
199
200 it('should not be emitted when another tab is removed', () => {
201 populateBar(bar);
202 let called = false;
203 bar.currentIndex = 1;
204 bar.currentChanged.connect((sender, args) => {
205 called = true;
206 });
207 bar.removeTab(bar.titles[0]);
208 expect(called).to.equal(false);
209 });
210
211 it('should not be emitted when the current tab is moved', () => {
212 populateBar(bar);
213 let called = false;
214 bar.currentChanged.connect((sender, args) => {
215 called = true;
216 });
217 bar.insertTab(2, bar.titles[0]);
218 expect(called).to.equal(false);
219 });
220 });
221
222 describe('#tabMoved', () => {
223 it('should be emitted when a tab is moved right by the user', done => {
224 populateBar(bar);
225 let titles = bar.titles.slice();
226 bar.tabMoved.connect((sender, args) => {
227 expect(sender).to.equal(bar);
228 expect(args.fromIndex).to.equal(0);
229 expect(args.toIndex).to.equal(2);
230 expect(args.title).to.equal(titles[0]);
231 done();
232 });
233 startDrag(bar);
234 document.body.dispatchEvent(
235 new PointerEvent('pointerup', {
236 cancelable: true
237 })
238 );
239 });
240
241 it('should be emitted when a tab is moved left by the user', done => {
242 populateBar(bar);
243 let titles = bar.titles.slice();
244 bar.tabMoved.connect((sender, args) => {
245 expect(sender).to.equal(bar);
246 expect(args.fromIndex).to.equal(2);
247 expect(args.toIndex).to.equal(0);
248 expect(args.title).to.equal(titles[2]);
249 done();
250 });
251 startDrag(bar, 2, 'left');
252 document.body.dispatchEvent(new PointerEvent('pointerup'));
253 });
254
255 it('should not be emitted when a tab is moved programmatically', () => {
256 populateBar(bar);
257 let called = false;
258 bar.tabMoved.connect((sender, args) => {
259 called = true;
260 });
261 bar.insertTab(2, bar.titles[0]);
262 expect(called).to.equal(false);
263 });
264 });
265
266 describe('#tabActivateRequested', () => {
267 let tab: HTMLElement;
268
269 beforeEach(() => {
270 populateBar(bar);
271 bar.tabsMovable = false;
272 tab = bar.contentNode.getElementsByClassName(
273 'lm-TabBar-tab'
274 )[2] as HTMLElement;
275 });
276
277 it('should be emitted when a tab is left pressed by the user', () => {
278 let called = false;
279 bar.currentIndex = 0;
280 // Force an update.
281 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
282 bar.tabActivateRequested.connect((sender, args) => {
283 expect(sender).to.equal(bar);
284 expect(args.index).to.equal(2);
285 expect(args.title).to.equal(bar.titles[2]);
286 called = true;
287 });
288 simulateOnNode(tab, 'pointerdown');
289 expect(called).to.equal(true);
290 });
291
292 it('should make the tab current and emit the `currentChanged` signal', () => {
293 let called = 0;
294 bar.currentIndex = 1;
295 // Force an update.
296 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
297 bar.tabActivateRequested.connect(() => {
298 called++;
299 });
300 bar.currentChanged.connect(() => {
301 called++;
302 });
303 simulateOnNode(tab, 'pointerdown');
304 expect(bar.currentIndex).to.equal(2);
305 expect(called).to.equal(2);
306 });
307
308 it('should emit even if the pressed tab is the current tab', () => {
309 let called = false;
310 bar.currentIndex = 2;
311 // Force an update.
312 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
313 bar.tabActivateRequested.connect(() => {
314 called = true;
315 });
316 simulateOnNode(tab, 'pointerdown');
317 expect(bar.currentIndex).to.equal(2);
318 expect(called).to.equal(true);
319 });
320 });
321
322 describe('#tabCloseRequested', () => {
323 let tab: Element;
324 let closeIcon: Element;
325
326 beforeEach(() => {
327 populateBar(bar);
328 bar.currentIndex = 0;
329 tab = bar.contentNode.children[0];
330 closeIcon = tab.querySelector(bar.renderer.closeIconSelector)!;
331 });
332
333 it('should be emitted when a tab close icon is left clicked', () => {
334 let called = false;
335 let rect = closeIcon.getBoundingClientRect();
336 bar.tabCloseRequested.connect((sender, args) => {
337 expect(sender).to.equal(bar);
338 expect(args.index).to.equal(0);
339 expect(args.title).to.equal(bar.titles[0]);
340 called = true;
341 });
342 closeIcon.dispatchEvent(
343 new PointerEvent('pointerdown', {
344 clientX: rect.left + 1,
345 clientY: rect.top + 1,
346 button: 0,
347 bubbles: true
348 })
349 );
350 closeIcon.dispatchEvent(
351 new PointerEvent('pointerup', {
352 clientX: rect.left + 1,
353 clientY: rect.top + 1,
354 button: 0
355 })
356 );
357 expect(called).to.equal(true);
358 });
359
360 it('should be emitted when a tab is middle clicked', () => {
361 let called = false;
362 let rect = tab.getBoundingClientRect();
363 bar.tabCloseRequested.connect((sender, args) => {
364 expect(sender).to.equal(bar);
365 expect(args.index).to.equal(0);
366 expect(args.title).to.equal(bar.titles[0]);
367 called = true;
368 });
369 tab.dispatchEvent(
370 new PointerEvent('pointerdown', {
371 clientX: rect.left + 1,
372 clientY: rect.top + 1,
373 button: 1,
374 bubbles: true
375 })
376 );
377 tab.dispatchEvent(
378 new PointerEvent('pointerup', {
379 clientX: rect.left + 1,
380 clientY: rect.top + 1,
381 button: 1
382 })
383 );
384 expect(called).to.equal(true);
385 });
386
387 it('should not be emitted if the tab title is not `closable`', () => {
388 let called = false;
389 let title = bar.titles[0];
390 title.closable = false;
391 bar.tabCloseRequested.connect((sender, args) => {
392 expect(sender).to.equal(bar);
393 expect(args.index).to.equal(0);
394 expect(args.title).to.equal(bar.titles[0]);
395 called = true;
396 });
397 let rect1 = closeIcon.getBoundingClientRect();
398 let rect2 = tab.getBoundingClientRect();
399 closeIcon.dispatchEvent(
400 new PointerEvent('pointerdown', {
401 clientX: rect1.left + 1,
402 clientY: rect1.top + 1,
403 button: 0,
404 cancelable: true
405 })
406 );
407 closeIcon.dispatchEvent(
408 new PointerEvent('pointerup', {
409 clientX: rect1.left + 1,
410 clientY: rect1.top + 1,
411 button: 0,
412 cancelable: true
413 })
414 );
415 tab.dispatchEvent(
416 new PointerEvent('pointerdown', {
417 clientX: rect2.left + 1,
418 clientY: rect2.top + 1,
419 button: 1,
420 cancelable: true
421 })
422 );
423 tab.dispatchEvent(
424 new PointerEvent('pointerup', {
425 clientX: rect2.left + 1,
426 clientY: rect2.top + 1,
427 button: 1,
428 cancelable: true
429 })
430 );
431 expect(called).to.equal(false);
432 });
433 });
434
435 describe('#addRequested', () => {
436 let addButton: Element;
437
438 beforeEach(() => {
439 populateBar(bar);
440 bar.currentIndex = 0;
441 addButton = bar.addButtonNode;
442 });
443
444 it('should be emitted when the add button is clicked', () => {
445 bar.addButtonEnabled = true;
446 let called = false;
447 let rect = addButton.getBoundingClientRect();
448 bar.addRequested.connect((sender, args) => {
449 expect(sender).to.equal(bar);
450 expect(args).to.equal(undefined);
451 called = true;
452 });
453 addButton.dispatchEvent(
454 new PointerEvent('pointerdown', {
455 clientX: rect.left + 1,
456 clientY: rect.top + 1,
457 button: 0,
458 bubbles: true
459 })
460 );
461 addButton.dispatchEvent(
462 new PointerEvent('pointerup', {
463 clientX: rect.left + 1,
464 clientY: rect.top + 1,
465 button: 0
466 })
467 );
468 expect(called).to.equal(true);
469 });
470
471 it('should not be emitted if addButtonEnabled is `false`', () => {
472 bar.addButtonEnabled = false;
473 let called = false;
474 let rect = addButton.getBoundingClientRect();
475 bar.addRequested.connect((sender, args) => {
476 expect(sender).to.equal(bar);
477 expect(args).to.equal(undefined);
478 called = true;
479 });
480 addButton.dispatchEvent(
481 new PointerEvent('pointerdown', {
482 clientX: rect.left + 1,
483 clientY: rect.top + 1,
484 button: 0,
485 cancelable: true
486 })
487 );
488 addButton.dispatchEvent(
489 new PointerEvent('pointerup', {
490 clientX: rect.left + 1,
491 clientY: rect.top + 1,
492 button: 0,
493 cancelable: true
494 })
495 );
496 expect(called).to.equal(false);
497 });
498 });
499
500 describe('#tabDetachRequested', () => {
501 let tab: HTMLElement;
502
503 beforeEach(() => {
504 populateBar(bar);
505 bar.tabsMovable = true;
506 tab = bar.contentNode.children[bar.currentIndex] as HTMLElement;
507 });
508
509 it('should be emitted when a tab is dragged beyond the detach threshold', () => {
510 simulateOnNode(tab, 'pointerdown');
511 let called = false;
512 bar.tabDetachRequested.connect((sender, args) => {
513 expect(sender).to.equal(bar);
514 expect(args.index).to.equal(0);
515 expect(args.title).to.equal(bar.titles[0]);
516 expect(args.clientX).to.equal(rect.right + 200);
517 // Firefox on macos sometimes returns floats for rect.top
518 expect(args.clientY).to.equal(Math.round(rect.top));
519 called = true;
520 });
521 let rect = bar.contentNode.getBoundingClientRect();
522 document.body.dispatchEvent(
523 new PointerEvent('pointermove', {
524 clientX: rect.right + 200,
525 clientY: Math.round(rect.top),
526 cancelable: true
527 })
528 );
529 expect(called).to.equal(true);
530 });
531
532 it('should be handled by calling `releaseMouse` and removing the tab', () => {
533 simulateOnNode(tab, 'pointerdown');
534 let called = false;
535 bar.tabDetachRequested.connect((sender, args) => {
536 bar.releaseMouse();
537 bar.removeTabAt(args.index);
538 called = true;
539 });
540 let rect = bar.contentNode.getBoundingClientRect();
541 document.body.dispatchEvent(
542 new PointerEvent('pointermove', {
543 clientX: rect.right + 200,
544 clientY: rect.top,
545 cancelable: true
546 })
547 );
548 expect(called).to.equal(true);
549 });
550
551 it('should only be emitted once per drag cycle', () => {
552 simulateOnNode(tab, 'pointerdown');
553 let called = 0;
554 bar.tabDetachRequested.connect((sender, args) => {
555 bar.releaseMouse();
556 bar.removeTabAt(args.index);
557 called++;
558 });
559 let rect = bar.contentNode.getBoundingClientRect();
560 document.body.dispatchEvent(
561 new PointerEvent('pointermove', {
562 clientX: rect.right + 200,
563 clientY: rect.top,
564 cancelable: true
565 })
566 );
567 expect(called).to.equal(1);
568 document.body.dispatchEvent(
569 new PointerEvent('pointermove', {
570 clientX: rect.right + 201,
571 clientY: rect.top,
572 cancelable: true
573 })
574 );
575 expect(called).to.equal(1);
576 });
577
578 it('should add the `lm-mod-dragging` class to the tab and the bar', () => {
579 simulateOnNode(tab, 'pointerdown');
580 let called = false;
581 bar.tabDetachRequested.connect((sender, args) => {
582 expect(tab.classList.contains('lm-mod-dragging')).to.equal(true);
583 expect(bar.hasClass('lm-mod-dragging')).to.equal(true);
584 called = true;
585 });
586 let rect = bar.contentNode.getBoundingClientRect();
587 document.body.dispatchEvent(
588 new PointerEvent('pointermove', {
589 clientX: rect.right + 200,
590 clientY: rect.top,
591 cancelable: true
592 })
593 );
594 expect(called).to.equal(true);
595 });
596 });
597
598 describe('#renderer', () => {
599 it('should be the tab bar renderer', () => {
600 let renderer = Object.create(TabBar.defaultRenderer);
601 let bar = new TabBar<Widget>({ renderer });
602 expect(bar.renderer).to.equal(renderer);
603 });
604
605 it('should default to the default renderer', () => {
606 let bar = new TabBar<Widget>();
607 expect(bar.renderer).to.equal(TabBar.defaultRenderer);
608 });
609 });
610
611 describe('#tabsMovable', () => {
612 it('should get whether the tabs are movable by the user', () => {
613 let bar = new TabBar<Widget>();
614 expect(bar.tabsMovable).to.equal(false);
615 });
616
617 it('should set whether the tabs are movable by the user', () => {
618 let bar = new TabBar<Widget>();
619 bar.tabsMovable = true;
620 expect(bar.tabsMovable).to.equal(true);
621 });
622
623 it('should still allow programmatic moves', () => {
624 populateBar(bar);
625 let titles = bar.titles.slice();
626 bar.insertTab(2, titles[0]);
627 expect(bar.titles[2]).to.equal(titles[0]);
628 });
629 });
630
631 describe('#addButtonEnabled', () => {
632 it('should get whether the add button is enabled', () => {
633 let bar = new TabBar<Widget>();
634 expect(bar.addButtonEnabled).to.equal(false);
635 });
636
637 it('should set whether the add button is enabled', () => {
638 let bar = new TabBar<Widget>();
639 bar.addButtonEnabled = true;
640 expect(bar.addButtonEnabled).to.equal(true);
641 });
642
643 it('should not show the add button if not set', () => {
644 populateBar(bar);
645 expect(bar.addButtonNode.classList.contains('lm-mod-hidden')).to.equal(
646 true
647 );
648
649 bar.addButtonEnabled = true;
650 expect(bar.addButtonNode.classList.contains('lm-mod-hidden')).to.equal(
651 false
652 );
653 });
654 });
655
656 describe('#allowDeselect', () => {
657 it('should determine whether a tab can be deselected by the user', () => {
658 populateBar(bar);
659 bar.allowDeselect = false;
660 bar.tabsMovable = false;
661 bar.currentIndex = 2;
662 // Force the tabs to render
663 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
664 let tab = bar.contentNode.getElementsByClassName(
665 'lm-TabBar-tab'
666 )[2] as HTMLElement;
667 simulateOnNode(tab, 'pointerdown');
668 expect(bar.currentIndex).to.equal(2);
669 simulateOnNode(tab, 'pointerup');
670
671 bar.allowDeselect = true;
672 simulateOnNode(tab, 'pointerdown');
673 expect(bar.currentIndex).to.equal(-1);
674 simulateOnNode(tab, 'pointerup');
675 });
676
677 it('should always allow programmatic deselection', () => {
678 populateBar(bar);
679 bar.allowDeselect = false;
680 bar.currentIndex = -1;
681 expect(bar.currentIndex).to.equal(-1);
682 });
683
684 it('focus should work if there is no current tab', () => {
685 populateBar(bar);
686 bar.allowDeselect = true;
687 const tab = bar.contentNode.firstChild as HTMLElement;
688 expect(bar.currentIndex).to.equal(0);
689 expect(tab.getAttribute('tabindex')).to.equal('0');
690 simulateOnNode(tab, 'pointerdown');
691 expect(bar.currentIndex).to.equal(-1);
692 expect(tab.getAttribute('tabindex')).to.equal('0');
693 });
694 });
695
696 describe('#insertBehavior', () => {
697 it('should not change the selection', () => {
698 populateBar(bar);
699 bar.insertBehavior = 'none';
700 bar.currentIndex = 0;
701 bar.insertTab(2, new Widget().title);
702 expect(bar.currentIndex).to.equal(0);
703 });
704
705 it('should select the tab', () => {
706 populateBar(bar);
707 bar.insertBehavior = 'select-tab';
708 bar.currentIndex = 0;
709 bar.insertTab(2, new Widget().title);
710 expect(bar.currentIndex).to.equal(2);
711
712 bar.currentIndex = -1;
713 bar.insertTab(1, new Widget().title);
714 expect(bar.currentIndex).to.equal(1);
715 });
716
717 it('should select the tab if needed', () => {
718 populateBar(bar);
719 bar.insertBehavior = 'select-tab-if-needed';
720 bar.currentIndex = 0;
721 bar.insertTab(2, new Widget().title);
722 expect(bar.currentIndex).to.equal(0);
723
724 bar.currentIndex = -1;
725 bar.insertTab(1, new Widget().title);
726 expect(bar.currentIndex).to.equal(1);
727 });
728 });
729
730 describe('#removeBehavior', () => {
731 it('should select no tab', () => {
732 populateBar(bar);
733 bar.removeBehavior = 'none';
734 bar.currentIndex = 2;
735 bar.removeTabAt(2);
736 expect(bar.currentIndex).to.equal(-1);
737 });
738
739 it('should select the tab after the removed tab if possible', () => {
740 populateBar(bar);
741 bar.removeBehavior = 'select-tab-after';
742 bar.currentIndex = 0;
743 bar.removeTabAt(0);
744 expect(bar.currentIndex).to.equal(0);
745
746 bar.currentIndex = 1;
747 bar.removeTabAt(1);
748 expect(bar.currentIndex).to.equal(0);
749 });
750
751 it('should select the tab before the removed tab if possible', () => {
752 populateBar(bar);
753 bar.removeBehavior = 'select-tab-before';
754 bar.currentIndex = 1;
755 bar.removeTabAt(1);
756 expect(bar.currentIndex).to.equal(0);
757 bar.removeTabAt(0);
758 expect(bar.currentIndex).to.equal(0);
759 });
760
761 it('should select the previously selected tab if possible', () => {
762 populateBar(bar);
763 bar.removeBehavior = 'select-previous-tab';
764 bar.currentIndex = 0;
765 bar.currentIndex = 2;
766 bar.removeTabAt(2);
767 expect(bar.currentIndex).to.equal(0);
768
769 // Reset the bar.
770 bar.removeTabAt(0);
771 bar.removeTabAt(0);
772 populateBar(bar);
773
774 bar.currentIndex = 1;
775 bar.removeTabAt(1);
776 expect(bar.currentIndex).to.equal(0);
777 });
778 });
779
780 describe('#currentTitle', () => {
781 it('should get the currently selected title', () => {
782 populateBar(bar);
783 bar.currentIndex = 0;
784 expect(bar.currentTitle).to.equal(bar.titles[0]);
785 });
786
787 it('should be `null` if no tab is selected', () => {
788 populateBar(bar);
789 bar.currentIndex = -1;
790 expect(bar.currentTitle).to.equal(null);
791 });
792
793 it('should set the currently selected title', () => {
794 populateBar(bar);
795 bar.currentTitle = bar.titles[1];
796 expect(bar.currentTitle).to.equal(bar.titles[1]);
797 });
798
799 it('should set the title to `null` if the title does not exist', () => {
800 populateBar(bar);
801 bar.currentTitle = new Widget().title;
802 expect(bar.currentTitle).to.equal(null);
803 });
804 });
805
806 describe('#currentIndex', () => {
807 it('should get index of the currently selected tab', () => {
808 populateBar(bar);
809 expect(bar.currentIndex).to.equal(0);
810 });
811
812 it('should be `null` if no tab is selected', () => {
813 expect(bar.currentIndex).to.equal(-1);
814 });
815
816 it('should set index of the currently selected tab', () => {
817 populateBar(bar);
818 bar.currentIndex = 1;
819 expect(bar.currentIndex).to.equal(1);
820 });
821
822 it('should set the index to `-1` if the value is out of range', () => {
823 populateBar(bar);
824 bar.currentIndex = -1;
825 expect(bar.currentIndex).to.equal(-1);
826 bar.currentIndex = 10;
827 expect(bar.currentIndex).to.equal(-1);
828 });
829
830 it('should emit the `currentChanged` signal', () => {
831 populateBar(bar);
832 let titles = bar.titles;
833 let called = false;
834 bar.currentChanged.connect((sender, args) => {
835 expect(sender).to.equal(bar);
836 expect(args.previousIndex).to.equal(0);
837 expect(args.previousTitle).to.equal(titles[0]);
838 expect(args.currentIndex).to.equal(1);
839 expect(args.currentTitle).to.equal(titles[1]);
840 called = true;
841 });
842 bar.currentIndex = 1;
843 expect(called).to.equal(true);
844 });
845
846 it('should schedule an update of the tabs', done => {
847 populateBar(bar);
848 requestAnimationFrame(() => {
849 bar.currentIndex = 1;
850 bar.methods = [];
851 requestAnimationFrame(() => {
852 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
853 done();
854 });
855 });
856 });
857
858 it('should be a no-op if the index does not change', done => {
859 populateBar(bar);
860 requestAnimationFrame(() => {
861 bar.currentIndex = 0;
862 bar.methods = [];
863 requestAnimationFrame(() => {
864 expect(bar.methods.indexOf('onUpdateRequest')).to.equal(-1);
865 done();
866 });
867 });
868 });
869 });
870
871 describe('#orientation', () => {
872 it('should be the orientation of the tab bar', () => {
873 expect(bar.orientation).to.equal('horizontal');
874 bar.orientation = 'vertical';
875 expect(bar.orientation).to.equal('vertical');
876 });
877
878 it('should set the orientation attribute of the tab bar', () => {
879 bar.orientation = 'horizontal';
880 expect(bar.node.getAttribute('data-orientation')).to.equal(
881 'horizontal'
882 );
883 bar.orientation = 'vertical';
884 expect(bar.node.getAttribute('data-orientation')).to.equal('vertical');
885 });
886 });
887
888 describe('#titles', () => {
889 it('should get the read-only array of titles in the tab bar', () => {
890 let bar = new TabBar<Widget>();
891 let widgets = [new Widget(), new Widget(), new Widget()];
892 widgets.forEach(widget => {
893 bar.addTab(widget.title);
894 });
895 expect(bar.titles.length).to.equal(3);
896 for (const [i, title] of bar.titles.entries()) {
897 expect(title.owner).to.equal(widgets[i]);
898 }
899 });
900 });
901
902 describe('#contentNode', () => {
903 it('should get the tab bar content node', () => {
904 expect(
905 bar.contentNode.classList.contains('lm-TabBar-content')
906 ).to.equal(true);
907 });
908 });
909
910 describe('#addTab()', () => {
911 it('should add a tab to the end of the tab bar', () => {
912 populateBar(bar);
913 let title = new Widget().title;
914 bar.addTab(title);
915 expect(bar.titles[3]).to.equal(title);
916 });
917
918 it('should accept a title options object', () => {
919 let owner = new Widget();
920 bar.addTab({ owner, label: 'foo' });
921 expect(bar.titles[0]).to.be.an.instanceof(Title);
922 expect(bar.titles[0].label).to.equal('foo');
923 });
924
925 it('should move an existing title to the end', () => {
926 populateBar(bar);
927 let titles = bar.titles.slice();
928 bar.addTab(titles[0]);
929 expect(bar.titles[2]).to.equal(titles[0]);
930 });
931 });
932
933 describe('#insertTab()', () => {
934 it('should insert a tab into the tab bar at the specified index', () => {
935 populateBar(bar);
936 let title = new Widget().title;
937 bar.insertTab(1, title);
938 expect(bar.titles[1]).to.equal(title);
939 });
940
941 it('should accept a title options object', () => {
942 populateBar(bar);
943 let title = bar.insertTab(1, { owner: new Widget(), label: 'foo' });
944 expect(title).to.be.an.instanceof(Title);
945 expect(title.label).to.equal('foo');
946 });
947
948 it('should clamp the index to the bounds of the tabs', () => {
949 populateBar(bar);
950 let title = new Widget().title;
951 bar.insertTab(-1, title);
952 expect(bar.titles[0]).to.equal(title);
953 title = new Widget().title;
954 bar.insertTab(10, title);
955 expect(bar.titles[4]).to.equal(title);
956 });
957
958 it('should move an existing tab', () => {
959 populateBar(bar);
960 let titles = bar.titles.slice();
961 bar.insertTab(1, titles[0]);
962 expect(bar.titles[1]).to.equal(titles[0]);
963 });
964
965 it('should schedule an update of the tabs', done => {
966 let bar = new LogTabBar();
967 bar.insertTab(0, new Widget().title);
968 requestAnimationFrame(() => {
969 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
970 done();
971 });
972 });
973
974 it('should schedule an update if the title changes', done => {
975 let bar = new LogTabBar();
976 let title = new Widget().title;
977 bar.insertTab(0, title);
978 requestAnimationFrame(() => {
979 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
980 bar.methods = [];
981 title.label = 'foo';
982 requestAnimationFrame(() => {
983 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
984 done();
985 });
986 });
987 });
988 });
989
990 describe('#removeTab()', () => {
991 it('should remove a tab from the tab bar by value', () => {
992 populateBar(bar);
993 let titles = bar.titles.slice();
994 bar.removeTab(titles[0]);
995 expect(bar.titles[0]).to.equal(titles[1]);
996 });
997
998 it('should return be a no-op if the title is not in the tab bar', () => {
999 populateBar(bar);
1000 bar.removeTab(new Widget().title);
1001 });
1002
1003 it('should schedule an update of the tabs', done => {
1004 let bar = new LogTabBar();
1005 bar.insertTab(0, new Widget().title);
1006 requestAnimationFrame(() => {
1007 bar.removeTab(bar.titles[0]);
1008 bar.methods = [];
1009 requestAnimationFrame(() => {
1010 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
1011 done();
1012 });
1013 });
1014 });
1015 });
1016
1017 describe('#removeTabAt()', () => {
1018 it('should remove a tab at a specific index', () => {
1019 populateBar(bar);
1020 let titles = bar.titles.slice();
1021 bar.removeTabAt(0);
1022 expect(bar.titles[0]).to.equal(titles[1]);
1023 });
1024
1025 it('should return be a no-op if the index is out of range', () => {
1026 populateBar(bar);
1027 bar.removeTabAt(9);
1028 });
1029
1030 it('should schedule an update of the tabs', done => {
1031 let bar = new LogTabBar();
1032 bar.insertTab(0, new Widget().title);
1033 requestAnimationFrame(() => {
1034 bar.removeTabAt(0);
1035 bar.methods = [];
1036 requestAnimationFrame(() => {
1037 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
1038 done();
1039 });
1040 });
1041 });
1042 });
1043
1044 describe('#clearTabs()', () => {
1045 it('should remove all tabs from the tab bar', () => {
1046 populateBar(bar);
1047 bar.clearTabs();
1048 expect(bar.titles.length).to.equal(0);
1049 });
1050
1051 it('should be a no-op if there are no tabs', () => {
1052 let bar = new TabBar<Widget>();
1053 bar.clearTabs();
1054 expect(bar.titles.length).to.equal(0);
1055 });
1056
1057 it('should emit the `currentChanged` signal if there was a selected tab', () => {
1058 populateBar(bar);
1059 let called = false;
1060 bar.currentIndex = 0;
1061 bar.currentChanged.connect((sender, args) => {
1062 expect(sender).to.equal(bar);
1063 expect(args.previousIndex).to.equal(0);
1064 called = true;
1065 });
1066 bar.clearTabs();
1067 expect(called).to.equal(true);
1068 });
1069
1070 it('should not emit the `currentChanged` signal if there was no selected tab', () => {
1071 populateBar(bar);
1072 let called = false;
1073 bar.currentIndex = -1;
1074 bar.currentChanged.connect((sender, args) => {
1075 called = true;
1076 });
1077 bar.clearTabs();
1078 expect(called).to.equal(false);
1079 });
1080 });
1081
1082 describe('#releaseMouse()', () => {
1083 it('should release the mouse and restore the non-dragged tab positions', () => {
1084 populateBar(bar);
1085 startDrag(bar, 0, 'left');
1086 bar.releaseMouse();
1087 document.body.dispatchEvent(new PointerEvent('pointermove'));
1088 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1089 });
1090 });
1091
1092 describe('#handleEvent()', () => {
1093 let tab: Element;
1094 let closeIcon: Element;
1095
1096 beforeEach(() => {
1097 bar.tabsMovable = true;
1098 populateBar(bar);
1099 bar.currentIndex = 0;
1100 tab = bar.contentNode.children[0];
1101 closeIcon = tab.querySelector(bar.renderer.closeIconSelector)!;
1102 });
1103
1104 context('left click', () => {
1105 it('should emit a tab close requested signal', () => {
1106 let called = false;
1107 let rect = closeIcon.getBoundingClientRect();
1108 bar.tabCloseRequested.connect((sender, args) => {
1109 expect(sender).to.equal(bar);
1110 expect(args.index).to.equal(0);
1111 expect(args.title).to.equal(bar.titles[0]);
1112 called = true;
1113 });
1114 closeIcon.dispatchEvent(
1115 new PointerEvent('pointerdown', {
1116 clientX: rect.left + 1,
1117 clientY: rect.top + 1,
1118 button: 0,
1119 bubbles: true
1120 })
1121 );
1122 closeIcon.dispatchEvent(
1123 new PointerEvent('pointerup', {
1124 clientX: rect.left + 1,
1125 clientY: rect.top + 1,
1126 button: 0,
1127 cancelable: true
1128 })
1129 );
1130 expect(called).to.equal(true);
1131 });
1132
1133 it('should do nothing if a drag is in progress', () => {
1134 startDrag(bar, 1, 'up');
1135 let called = false;
1136 let rect = closeIcon.getBoundingClientRect();
1137 bar.tabCloseRequested.connect((sender, args) => {
1138 called = true;
1139 });
1140 closeIcon.dispatchEvent(
1141 new PointerEvent('pointerdown', {
1142 clientX: rect.left + 1,
1143 clientY: rect.top + 1,
1144 button: 0,
1145 cancelable: true
1146 })
1147 );
1148 closeIcon.dispatchEvent(
1149 new PointerEvent('pointerup', {
1150 clientX: rect.left + 1,
1151 clientY: rect.top + 1,
1152 button: 0,
1153 cancelable: true
1154 })
1155 );
1156 expect(called).to.equal(false);
1157 });
1158
1159 it('should do nothing if the click is not on a close icon', () => {
1160 let called = false;
1161 let rect = closeIcon.getBoundingClientRect();
1162 bar.tabCloseRequested.connect((sender, args) => {
1163 called = true;
1164 });
1165 closeIcon.dispatchEvent(
1166 new PointerEvent('pointerdown', {
1167 clientX: rect.left + 1,
1168 clientY: rect.top + 1,
1169 button: 0,
1170 cancelable: true
1171 })
1172 );
1173 closeIcon.dispatchEvent(
1174 new PointerEvent('pointerup', {
1175 clientX: rect.left - 1,
1176 clientY: rect.top - 1,
1177 button: 0,
1178 cancelable: true
1179 })
1180 );
1181 expect(called).to.equal(false);
1182 expect(called).to.equal(false);
1183 });
1184
1185 it('should do nothing if the tab is not closable', () => {
1186 let called = false;
1187 bar.titles[0].closable = false;
1188 let rect = closeIcon.getBoundingClientRect();
1189 bar.tabCloseRequested.connect((sender, args) => {
1190 called = true;
1191 });
1192 closeIcon.dispatchEvent(
1193 new PointerEvent('pointerdown', {
1194 clientX: rect.left + 1,
1195 clientY: rect.top + 1,
1196 button: 0,
1197 cancelable: true
1198 })
1199 );
1200 closeIcon.dispatchEvent(
1201 new PointerEvent('pointerup', {
1202 clientX: rect.left + 1,
1203 clientY: rect.top + 1,
1204 button: 0,
1205 cancelable: true
1206 })
1207 );
1208 expect(called).to.equal(false);
1209 });
1210 });
1211
1212 context('middle click', () => {
1213 it('should emit a tab close requested signal', () => {
1214 let called = false;
1215 let rect = tab.getBoundingClientRect();
1216 bar.tabCloseRequested.connect((sender, args) => {
1217 expect(sender).to.equal(bar);
1218 expect(args.index).to.equal(0);
1219 expect(args.title).to.equal(bar.titles[0]);
1220 called = true;
1221 });
1222 tab.dispatchEvent(
1223 new PointerEvent('pointerdown', {
1224 clientX: rect.left + 1,
1225 clientY: rect.top + 1,
1226 button: 1,
1227 bubbles: true
1228 })
1229 );
1230 tab.dispatchEvent(
1231 new PointerEvent('pointerup', {
1232 clientX: rect.left + 1,
1233 clientY: rect.top + 1,
1234 button: 1,
1235 cancelable: true
1236 })
1237 );
1238 expect(called).to.equal(true);
1239 });
1240
1241 it('should do nothing if a drag is in progress', () => {
1242 startDrag(bar, 1, 'up');
1243 let called = false;
1244 let rect = tab.getBoundingClientRect();
1245 bar.tabCloseRequested.connect((sender, args) => {
1246 called = true;
1247 });
1248 tab.dispatchEvent(
1249 new PointerEvent('pointerdown', {
1250 clientX: rect.left + 1,
1251 clientY: rect.top + 1,
1252 button: 1,
1253 cancelable: true
1254 })
1255 );
1256 tab.dispatchEvent(
1257 new PointerEvent('pointerup', {
1258 clientX: rect.left + 1,
1259 clientY: rect.top + 1,
1260 button: 1,
1261 cancelable: true
1262 })
1263 );
1264 expect(called).to.equal(false);
1265 });
1266
1267 it('should do nothing if the click is not on the tab', () => {
1268 let called = false;
1269 let rect = tab.getBoundingClientRect();
1270 bar.tabCloseRequested.connect((sender, args) => {
1271 called = true;
1272 });
1273 tab.dispatchEvent(
1274 new PointerEvent('pointerdown', {
1275 clientX: rect.left + 1,
1276 clientY: rect.top + 1,
1277 button: 1,
1278 cancelable: true
1279 })
1280 );
1281 tab.dispatchEvent(
1282 new PointerEvent('pointerup', {
1283 clientX: rect.left - 1,
1284 clientY: rect.top - 1,
1285 button: 1,
1286 cancelable: true
1287 })
1288 );
1289 expect(called).to.equal(false);
1290 expect(called).to.equal(false);
1291 });
1292
1293 it('should do nothing if the tab is not closable', () => {
1294 let called = false;
1295 bar.titles[0].closable = false;
1296 let rect = tab.getBoundingClientRect();
1297 bar.tabCloseRequested.connect((sender, args) => {
1298 called = true;
1299 });
1300 tab.dispatchEvent(
1301 new PointerEvent('pointerdown', {
1302 clientX: rect.left + 1,
1303 clientY: rect.top + 1,
1304 button: 1,
1305 cancelable: true
1306 })
1307 );
1308 tab.dispatchEvent(
1309 new PointerEvent('pointerup', {
1310 clientX: rect.left + 1,
1311 clientY: rect.top + 1,
1312 button: 1,
1313 cancelable: true
1314 })
1315 );
1316 expect(called).to.equal(false);
1317 });
1318 });
1319
1320 context('pointerdown', () => {
1321 it('should add event listeners if the tabs are movable', () => {
1322 simulateOnNode(tab, 'pointerdown');
1323 document.body.dispatchEvent(new PointerEvent('pointermove'));
1324 expect(bar.events.indexOf('pointermove')).to.not.equal(-1);
1325 });
1326
1327 it('should do nothing if not a left mouse press', () => {
1328 let rect = tab.getBoundingClientRect();
1329 tab.dispatchEvent(
1330 new PointerEvent('pointerdown', {
1331 clientX: rect.left + 1,
1332 clientY: rect.top + 1,
1333 button: 1,
1334 cancelable: true
1335 })
1336 );
1337 document.body.dispatchEvent(new PointerEvent('pointermove'));
1338 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1339 });
1340
1341 it('should do nothing if the press is not on a tab', () => {
1342 let rect = tab.getBoundingClientRect();
1343 tab.dispatchEvent(
1344 new PointerEvent('pointerdown', {
1345 clientX: rect.left - 1,
1346 clientY: rect.top,
1347 cancelable: true
1348 })
1349 );
1350 document.body.dispatchEvent(new PointerEvent('pointermove'));
1351 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1352 });
1353
1354 it('should do nothing if the press is on a close icon', () => {
1355 simulateOnNode(closeIcon, 'pointerdown');
1356 document.body.dispatchEvent(new PointerEvent('pointermove'));
1357 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1358 });
1359
1360 it('should do nothing if the tabs are not movable', () => {
1361 bar.tabsMovable = false;
1362 simulateOnNode(tab, 'pointerdown');
1363 document.body.dispatchEvent(new PointerEvent('pointermove'));
1364 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1365 });
1366
1367 it('should do nothing if there is a drag in progress', () => {
1368 startDrag(bar, 2, 'down');
1369 let rect = tab.getBoundingClientRect();
1370 let event = new PointerEvent('pointerdown', {
1371 clientX: rect.left + 1,
1372 clientY: rect.top + 1,
1373 cancelable: true
1374 });
1375 let cancelled = !tab.dispatchEvent(event);
1376 expect(cancelled).to.equal(false);
1377 });
1378 });
1379
1380 context('pointermove', () => {
1381 it('should do nothing if there is a drag in progress', () => {
1382 simulateOnNode(tab, 'pointerdown');
1383 let called = 0;
1384 bar.tabDetachRequested.connect((sender, args) => {
1385 called++;
1386 });
1387 let rect = bar.contentNode.getBoundingClientRect();
1388 document.body.dispatchEvent(
1389 new PointerEvent('pointermove', {
1390 clientX: rect.right + 200,
1391 clientY: rect.top,
1392 cancelable: true
1393 })
1394 );
1395 expect(called).to.equal(1);
1396 document.body.dispatchEvent(
1397 new PointerEvent('pointermove', {
1398 clientX: rect.right + 200,
1399 clientY: rect.top,
1400 cancelable: true
1401 })
1402 );
1403 expect(called).to.equal(1);
1404 });
1405
1406 it('should bail if the drag threshold is not exceeded', () => {
1407 simulateOnNode(tab, 'pointerdown');
1408 let called = false;
1409 bar.tabDetachRequested.connect((sender, args) => {
1410 bar.releaseMouse();
1411 called = true;
1412 });
1413 let rect = bar.contentNode.getBoundingClientRect();
1414 document.body.dispatchEvent(
1415 new PointerEvent('pointermove', {
1416 clientX: rect.right + 1,
1417 clientY: rect.top,
1418 cancelable: true
1419 })
1420 );
1421 expect(called).to.equal(false);
1422 });
1423
1424 it('should emit the detach requested signal if the threshold is exceeded', () => {
1425 simulateOnNode(tab, 'pointerdown');
1426 let called = false;
1427 bar.tabDetachRequested.connect((sender, args) => {
1428 expect(sender).to.equal(bar);
1429 expect(args.index).to.equal(0);
1430 expect(args.title).to.equal(bar.titles[0]);
1431 expect(args.clientX).to.equal(rect.right + 200);
1432 // Firefox on macos sometimes returns floats for rect.top
1433 expect(args.clientY).to.equal(Math.round(rect.top));
1434 called = true;
1435 });
1436 let rect = bar.contentNode.getBoundingClientRect();
1437 document.body.dispatchEvent(
1438 new PointerEvent('pointermove', {
1439 clientX: rect.right + 200,
1440 clientY: Math.round(rect.top),
1441 cancelable: true
1442 })
1443 );
1444 expect(called).to.equal(true);
1445 });
1446
1447 it('should bail if the signal handler aborted the drag', () => {
1448 simulateOnNode(tab, 'pointerdown');
1449 let called = false;
1450 bar.tabDetachRequested.connect((sender, args) => {
1451 bar.releaseMouse();
1452 called = true;
1453 });
1454 let rect = bar.contentNode.getBoundingClientRect();
1455 document.body.dispatchEvent(
1456 new PointerEvent('pointermove', {
1457 clientX: rect.right + 200,
1458 clientY: rect.top,
1459 cancelable: true
1460 })
1461 );
1462 expect(called).to.equal(true);
1463 let left = rect.left;
1464 rect = tab.getBoundingClientRect();
1465 expect(left).to.equal(rect.left);
1466 });
1467
1468 it('should update the positions of the tabs', () => {
1469 simulateOnNode(tab, 'pointerdown');
1470 let called = false;
1471 bar.tabDetachRequested.connect((sender, args) => {
1472 called = true;
1473 });
1474 let rect = bar.contentNode.getBoundingClientRect();
1475 document.body.dispatchEvent(
1476 new PointerEvent('pointermove', {
1477 clientX: rect.right + 200,
1478 clientY: rect.top,
1479 cancelable: true
1480 })
1481 );
1482 expect(called).to.equal(true);
1483 let left = rect.left;
1484 rect = tab.getBoundingClientRect();
1485 expect(left).to.not.equal(rect.left);
1486 });
1487 });
1488
1489 context('pointerup', () => {
1490 it('should emit the `tabMoved` signal', done => {
1491 startDrag(bar);
1492 document.body.dispatchEvent(new PointerEvent('pointerup'));
1493 bar.tabMoved.connect(() => {
1494 done();
1495 });
1496 });
1497
1498 it('should move the tab to its final position', done => {
1499 startDrag(bar);
1500 document.body.dispatchEvent(new PointerEvent('pointerup'));
1501 let title = bar.titles[0];
1502 bar.tabMoved.connect(() => {
1503 expect(bar.titles[2]).to.equal(title);
1504 done();
1505 });
1506 });
1507
1508 it('should cancel a middle mouse release', () => {
1509 startDrag(bar);
1510 let event = new PointerEvent('pointerup', {
1511 button: 1,
1512 cancelable: true
1513 });
1514 let cancelled = !document.body.dispatchEvent(event);
1515 expect(cancelled).to.equal(true);
1516 });
1517 });
1518
1519 context('keydown', () => {
1520 it('should prevent default', () => {
1521 startDrag(bar);
1522 let event = new KeyboardEvent('keydown', { cancelable: true });
1523 let cancelled = !document.body.dispatchEvent(event);
1524 expect(cancelled).to.equal(true);
1525 });
1526
1527 it('should release the mouse if `Escape` is pressed', () => {
1528 startDrag(bar);
1529 document.body.dispatchEvent(
1530 new KeyboardEvent('keydown', {
1531 keyCode: 27,
1532 cancelable: true
1533 })
1534 );
1535 simulateOnNode(tab, 'pointerdown');
1536 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1537 });
1538
1539 it('should activate the focused title on Enter', () => {
1540 // Focus 3rd tab
1541 (bar.contentNode.children[2] as HTMLElement).focus();
1542
1543 bar.node.dispatchEvent(
1544 new KeyboardEvent('keydown', {
1545 key: 'Enter',
1546 cancelable: true,
1547 bubbles: true
1548 })
1549 );
1550
1551 expect(bar.currentIndex).to.equal(2);
1552 });
1553
1554 it('should activate the focused title on Space', () => {
1555 // Focus 2nd tab
1556 (bar.contentNode.children[1] as HTMLElement).focus();
1557
1558 bar.node.dispatchEvent(
1559 new KeyboardEvent('keydown', {
1560 key: ' ',
1561 cancelable: true,
1562 bubbles: true
1563 })
1564 );
1565
1566 expect(bar.currentIndex).to.equal(1);
1567 });
1568
1569 it('should add a tab when Enter is pressed with focus on add button', () => {
1570 let addRequested = false;
1571 bar.addButtonEnabled = true;
1572 bar.addButtonNode.focus();
1573
1574 bar.addRequested.connect(() => {
1575 addRequested = true;
1576 });
1577
1578 bar.node.dispatchEvent(
1579 new KeyboardEvent('keydown', {
1580 key: 'Enter',
1581 cancelable: true,
1582 bubbles: true
1583 })
1584 );
1585
1586 expect(addRequested).to.be.true;
1587 });
1588
1589 it('should add a tab when Space is pressed with focus on add button', () => {
1590 let addRequested = false;
1591 bar.addButtonEnabled = true;
1592 bar.addButtonNode.focus();
1593
1594 bar.addRequested.connect(() => {
1595 addRequested = true;
1596 });
1597
1598 bar.node.dispatchEvent(
1599 new KeyboardEvent('keydown', {
1600 key: ' ',
1601 cancelable: true,
1602 bubbles: true
1603 })
1604 );
1605
1606 expect(addRequested).to.be.true;
1607 });
1608
1609 it('should have the tabindex="0" on the first tab by default', () => {
1610 populateBar(bar);
1611 const firstTab = bar.contentNode.firstChild as HTMLElement;
1612 expect(firstTab.getAttribute('tabindex')).to.equal('0');
1613 for (let i = 1; i < bar.titles.length; i++) {
1614 let tab = bar.contentNode.children[i] as HTMLElement;
1615 expect(tab.getAttribute('tabindex')).to.equal('-1');
1616 }
1617 expect(bar.addButtonNode.getAttribute('tabindex')).to.equal('-1');
1618 });
1619
1620 it('should have a role attribute of button', () => {
1621 populateBar(bar);
1622 expect(bar.addButtonNode.getAttribute('role')).to.equal('button');
1623 });
1624
1625 it('should focus the second tab on right arrow keydown', () => {
1626 populateBar(bar);
1627 const firstTab = bar.contentNode.firstChild as HTMLElement;
1628 firstTab.focus();
1629 bar.node.dispatchEvent(
1630 new KeyboardEvent('keydown', {
1631 key: 'ArrowRight',
1632 cancelable: true,
1633 bubbles: true
1634 })
1635 );
1636 expect(firstTab.getAttribute('tabindex')).to.equal('-1');
1637 const secondTab = bar.contentNode.children[1] as HTMLElement;
1638 expect(secondTab.getAttribute('tabindex')).to.equal('0');
1639 expect(document.activeElement).to.equal(secondTab);
1640 });
1641
1642 it('should focus the last tab on left arrow keydown', () => {
1643 populateBar(bar);
1644 const firstTab = bar.contentNode.firstChild as HTMLElement;
1645 firstTab.focus();
1646 bar.node.dispatchEvent(
1647 new KeyboardEvent('keydown', {
1648 key: 'ArrowLeft',
1649 cancelable: true,
1650 bubbles: true
1651 })
1652 );
1653 expect(firstTab.getAttribute('tabindex')).to.equal('-1');
1654 const lastTab = bar.contentNode.lastChild as HTMLElement;
1655 expect(lastTab.getAttribute('tabindex')).to.equal('0');
1656 expect(document.activeElement).to.equal(lastTab);
1657 });
1658
1659 it('should focus the add button on left arrow keydown', () => {
1660 bar.addButtonEnabled = true;
1661 populateBar(bar);
1662 const firstTab = bar.contentNode.firstChild as HTMLElement;
1663 firstTab.focus();
1664 bar.node.dispatchEvent(
1665 new KeyboardEvent('keydown', {
1666 key: 'ArrowLeft',
1667 cancelable: true,
1668 bubbles: true
1669 })
1670 );
1671 expect(firstTab.getAttribute('tabindex')).to.equal('-1');
1672 expect(bar.addButtonNode.getAttribute('tabindex')).to.equal('0');
1673 expect(document.activeElement).to.equal(bar.addButtonNode);
1674 });
1675
1676 it('should be no-op on up and down arrow keydown', () => {
1677 populateBar(bar);
1678 const firstTab = bar.contentNode.firstChild as HTMLElement;
1679 firstTab.focus();
1680 bar.node.dispatchEvent(
1681 new KeyboardEvent('keydown', {
1682 key: 'ArrowUp',
1683 cancelable: true,
1684 bubbles: true
1685 })
1686 );
1687 expect(firstTab.getAttribute('tabindex')).to.equal('0');
1688 expect(document.activeElement).to.equal(firstTab);
1689 bar.node.dispatchEvent(
1690 new KeyboardEvent('keydown', {
1691 key: 'ArrowDown',
1692 cancelable: true,
1693 bubbles: true
1694 })
1695 );
1696 expect(firstTab.getAttribute('tabindex')).to.equal('0');
1697 expect(document.activeElement).to.equal(firstTab);
1698 });
1699
1700 it('should focus the second tab on down arrow keydown', () => {
1701 bar.orientation = 'vertical';
1702 populateBar(bar);
1703 const firstTab = bar.contentNode.firstChild as HTMLElement;
1704 firstTab.focus();
1705 bar.node.dispatchEvent(
1706 new KeyboardEvent('keydown', {
1707 key: 'ArrowDown',
1708 cancelable: true,
1709 bubbles: true
1710 })
1711 );
1712 expect(firstTab.getAttribute('tabindex')).to.equal('-1');
1713 const secondTab = bar.contentNode.children[1] as HTMLElement;
1714 expect(secondTab.getAttribute('tabindex')).to.equal('0');
1715 expect(document.activeElement).to.equal(secondTab);
1716 });
1717
1718 it('should focus the last tab on up arrow keydown', () => {
1719 bar.orientation = 'vertical';
1720 populateBar(bar);
1721 const firstTab = bar.contentNode.firstChild as HTMLElement;
1722 firstTab.focus();
1723 bar.node.dispatchEvent(
1724 new KeyboardEvent('keydown', {
1725 key: 'ArrowUp',
1726 cancelable: true,
1727 bubbles: true
1728 })
1729 );
1730 expect(firstTab.getAttribute('tabindex')).to.equal('-1');
1731 const lastTab = bar.contentNode.lastChild as HTMLElement;
1732 expect(lastTab.getAttribute('tabindex')).to.equal('0');
1733 expect(document.activeElement).to.equal(lastTab);
1734 });
1735
1736 it('should be no-op on left and right arrow keydown', () => {
1737 bar.orientation = 'vertical';
1738 populateBar(bar);
1739 const firstTab = bar.contentNode.firstChild as HTMLElement;
1740 firstTab.focus();
1741 bar.node.dispatchEvent(
1742 new KeyboardEvent('keydown', {
1743 key: 'ArrowLeft',
1744 cancelable: true,
1745 bubbles: true
1746 })
1747 );
1748 expect(firstTab.getAttribute('tabindex')).to.equal('0');
1749 expect(document.activeElement).to.equal(firstTab);
1750 bar.node.dispatchEvent(
1751 new KeyboardEvent('keydown', {
1752 key: 'ArrowRight',
1753 cancelable: true,
1754 bubbles: true
1755 })
1756 );
1757 expect(firstTab.getAttribute('tabindex')).to.equal('0');
1758 expect(document.activeElement).to.equal(firstTab);
1759 });
1760
1761 it('should focus the first tab on "Home" keydown', () => {
1762 populateBar(bar);
1763 const firstTab = bar.contentNode.firstChild as HTMLElement;
1764 const lastTab = bar.contentNode.lastChild as HTMLElement;
1765 firstTab.setAttribute('tabindex', '-1');
1766 lastTab.setAttribute('tabindex', '0');
1767 lastTab.focus();
1768 bar.node.dispatchEvent(
1769 new KeyboardEvent('keydown', {
1770 key: 'Home',
1771 cancelable: true,
1772 bubbles: true
1773 })
1774 );
1775 expect(firstTab.getAttribute('tabindex')).to.equal('0');
1776 expect(document.activeElement).to.equal(firstTab);
1777 });
1778
1779 it('should focus the last tab on "End" keydown', () => {
1780 populateBar(bar);
1781 const lastTab = bar.contentNode.lastChild as HTMLElement;
1782 bar.node.dispatchEvent(
1783 new KeyboardEvent('keydown', {
1784 key: 'End',
1785 cancelable: true,
1786 bubbles: true
1787 })
1788 );
1789 expect(lastTab.getAttribute('tabindex')).to.equal('0');
1790 expect(document.activeElement).to.equal(lastTab);
1791 });
1792
1793 it('should not change the tabindex values when focusing another element', () => {
1794 const node = document.createElement('div');
1795 node.setAttribute('tabindex', '0');
1796 document.body.append(node);
1797 populateBar(bar);
1798 const firstTab = bar.contentNode.firstChild as HTMLElement;
1799 firstTab.focus();
1800 bar.node.dispatchEvent(
1801 new KeyboardEvent('keydown', {
1802 key: 'ArrowRight',
1803 cancelable: true,
1804 bubbles: true
1805 })
1806 );
1807 node.focus();
1808 const secondTab = bar.contentNode.children[1] as HTMLElement;
1809 expect(document.activeElement).not.to.equal(secondTab);
1810 expect(secondTab.getAttribute('tabindex')).to.equal('0');
1811 });
1812
1813 /**
1814 * This test is skipped as it seems there is no way to trigger a change of focus
1815 * when simulating tabulation keydown.
1816 *
1817 * TODO:
1818 * Find a way to trigger the change of focus.
1819 */
1820 /*
1821 it.skip('should keep focus on the second tab on tabulation', () => {
1822 const node = document.createElement('div');
1823 node.setAttribute('tabindex', '0');
1824 document.body.append(node);
1825 populateBar(bar);
1826 const firstTab = bar.contentNode.firstChild as HTMLElement;
1827 firstTab.focus();
1828 bar.node.dispatchEvent(
1829 new KeyboardEvent('keydown', {
1830 key: 'ArrowRight',
1831 cancelable: true,
1832 bubbles: true
1833 })
1834 );
1835 bar.node.dispatchEvent(
1836 new KeyboardEvent('keydown', {
1837 key: 'Tab'
1838 })
1839 );
1840 const secondTab = bar.contentNode.children[1] as HTMLElement;
1841 expect(document.activeElement).not.to.equal(secondTab);
1842 bar.node.dispatchEvent(
1843 new KeyboardEvent('keydown', {
1844 key: 'Tab',
1845 shiftKey: true
1846 })
1847 );
1848 expect(document.activeElement).to.equal(secondTab);
1849 });
1850 */
1851 });
1852
1853 context('contextmenu', () => {
1854 it('should prevent default', () => {
1855 startDrag(bar);
1856 let event = new MouseEvent('contextmenu', { cancelable: true });
1857 let cancelled = !document.body.dispatchEvent(event);
1858 expect(cancelled).to.equal(true);
1859 });
1860 });
1861 });
1862
1863 describe('editable title', () => {
1864 let title: Title<Widget>;
1865
1866 const triggerDblClick = (tab: HTMLElement) => {
1867 const tabLabel = tab.querySelector(
1868 '.lm-TabBar-tabLabel'
1869 ) as HTMLElement;
1870 expect(tab.querySelector('input')).to.be.null;
1871 simulateOnNode(tabLabel, 'dblclick');
1872 };
1873
1874 beforeEach(() => {
1875 bar.titlesEditable = true;
1876 let owner = new Widget();
1877 title = new Title({ owner, label: 'foo', closable: true });
1878 bar.addTab(title);
1879 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
1880 });
1881
1882 it('titles should be editable', () => {
1883 const tab = bar.contentNode.firstChild as HTMLElement;
1884 triggerDblClick(tab);
1885 const input = tab.querySelector(
1886 'input.lm-TabBar-tabInput'
1887 ) as HTMLInputElement;
1888 expect(input).not.to.be.null;
1889 expect(input.value).to.equal(title.label);
1890 expect(document.activeElement).to.equal(input);
1891 });
1892
1893 it('title should be edited', () => {
1894 const tab = bar.contentNode.firstChild as HTMLElement;
1895 triggerDblClick(tab);
1896 let input = tab.querySelector(
1897 'input.lm-TabBar-tabInput'
1898 ) as HTMLInputElement;
1899 input.value = 'bar';
1900 input.dispatchEvent(
1901 new KeyboardEvent('keydown', {
1902 key: 'Enter',
1903 cancelable: true,
1904 bubbles: true
1905 })
1906 );
1907 input = tab.querySelector(
1908 'input.lm-TabBar-tabInput'
1909 ) as HTMLInputElement;
1910 expect(input).to.be.null;
1911 expect(title.label).to.equal('bar');
1912 });
1913
1914 it('title edition should be canceled', () => {
1915 const tab = bar.contentNode.firstChild as HTMLElement;
1916 triggerDblClick(tab);
1917 let input = tab.querySelector(
1918 'input.lm-TabBar-tabInput'
1919 ) as HTMLInputElement;
1920 input.value = 'bar';
1921 input.dispatchEvent(
1922 new KeyboardEvent('keydown', {
1923 key: 'Escape',
1924 cancelable: true,
1925 bubbles: true
1926 })
1927 );
1928 input = tab.querySelector(
1929 'input.lm-TabBar-tabInput'
1930 ) as HTMLInputElement;
1931 expect(input).to.be.null;
1932 expect(title.label).to.equal('foo');
1933 });
1934
1935 it('Arrow keys should have no effect on focus during edition', () => {
1936 populateBar(bar);
1937 const tab = bar.contentNode.firstChild as HTMLElement;
1938 triggerDblClick(tab);
1939 const input = tab.querySelector(
1940 'input.lm-TabBar-tabInput'
1941 ) as HTMLInputElement;
1942 bar.node.dispatchEvent(
1943 new KeyboardEvent('keydown', {
1944 key: 'ArrowRight',
1945 cancelable: true,
1946 bubbles: true
1947 })
1948 );
1949 expect(document.activeElement).to.equal(input);
1950 });
1951 });
1952
1953 describe('#onBeforeAttach()', () => {
1954 it('should add event listeners to the node', () => {
1955 let bar = new LogTabBar();
1956 Widget.attach(bar, document.body);
1957 expect(bar.methods).to.contain('onBeforeAttach');
1958 bar.node.dispatchEvent(
1959 new PointerEvent('pointerdown', {
1960 cancelable: true
1961 })
1962 );
1963 expect(bar.events.indexOf('pointerdown')).to.not.equal(-1);
1964 bar.dispose();
1965 });
1966 });
1967
1968 describe('#onAfterDetach()', () => {
1969 it('should remove event listeners', () => {
1970 let bar = new LogTabBar();
1971 let owner = new Widget();
1972 bar.addTab(new Title({ owner, label: 'foo' }));
1973 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
1974 Widget.attach(bar, document.body);
1975 let tab = bar.contentNode.firstChild as HTMLElement;
1976 let rect = tab.getBoundingClientRect();
1977 tab.dispatchEvent(
1978 new PointerEvent('pointerdown', {
1979 clientX: rect.left + 1,
1980 clientY: rect.top + 1,
1981 cancelable: true
1982 })
1983 );
1984 Widget.detach(bar);
1985 expect(bar.methods).to.contain('onAfterDetach');
1986 document.body.dispatchEvent(
1987 new PointerEvent('pointermove', {
1988 cancelable: true
1989 })
1990 );
1991 expect(bar.events.indexOf('pointermove')).to.equal(-1);
1992 document.body.dispatchEvent(
1993 new PointerEvent('pointerup', {
1994 cancelable: true
1995 })
1996 );
1997 expect(bar.events.indexOf('pointerup')).to.equal(-1);
1998 });
1999 });
2000
2001 describe('#onUpdateRequest()', () => {
2002 it('should render tabs and set styles', () => {
2003 populateBar(bar);
2004 bar.currentIndex = 0;
2005 MessageLoop.sendMessage(bar, Widget.Msg.UpdateRequest);
2006 expect(bar.methods.indexOf('onUpdateRequest')).to.not.equal(-1);
2007 for (const [i, title] of bar.titles.entries()) {
2008 let tab = bar.contentNode.children[i] as HTMLElement;
2009 let label = tab.getElementsByClassName(
2010 'lm-TabBar-tabLabel'
2011 )[0] as HTMLElement;
2012 expect(label.textContent).to.equal(title.label);
2013 let current = i === 0;
2014 expect(tab.classList.contains('lm-mod-current')).to.equal(current);
2015 }
2016 });
2017 });
2018
2019 describe('.Renderer', () => {
2020 let title: Title<Widget>;
2021
2022 beforeEach(() => {
2023 let owner = new Widget();
2024 title = new Title({
2025 owner,
2026 label: 'foo',
2027 closable: true,
2028 iconClass: 'bar',
2029 className: 'fizz',
2030 caption: 'this is a caption'
2031 });
2032 });
2033
2034 describe('#closeIconSelector', () => {
2035 it('should be `.lm-TabBar-tabCloseIcon`', () => {
2036 let renderer = new TabBar.Renderer();
2037 expect(renderer.closeIconSelector).to.equal(
2038 '.lm-TabBar-tabCloseIcon'
2039 );
2040 });
2041 });
2042
2043 describe('#renderTab()', () => {
2044 it('should render a virtual node for a tab', () => {
2045 let renderer = new TabBar.Renderer();
2046 let vNode = renderer.renderTab({ title, current: true, zIndex: 1 });
2047 let node = VirtualDOM.realize(vNode);
2048
2049 expect(
2050 node.getElementsByClassName('lm-TabBar-tabIcon').length
2051 ).to.equal(1);
2052 expect(
2053 node.getElementsByClassName('lm-TabBar-tabLabel').length
2054 ).to.equal(1);
2055 expect(
2056 node.getElementsByClassName('lm-TabBar-tabCloseIcon').length
2057 ).to.equal(1);
2058
2059 expect(node.classList.contains('lm-TabBar-tab')).to.equal(true);
2060 expect(node.classList.contains(title.className)).to.equal(true);
2061 expect(node.classList.contains('lm-mod-current')).to.equal(true);
2062 expect(node.classList.contains('lm-mod-closable')).to.equal(true);
2063 expect(node.title).to.equal(title.caption);
2064
2065 let label = node.getElementsByClassName(
2066 'lm-TabBar-tabLabel'
2067 )[0] as HTMLElement;
2068 expect(label.textContent).to.equal(title.label);
2069
2070 let icon = node.getElementsByClassName(
2071 'lm-TabBar-tabIcon'
2072 )[0] as HTMLElement;
2073 expect(icon.classList.contains(title.iconClass)).to.equal(true);
2074 });
2075 });
2076
2077 describe('#renderIcon()', () => {
2078 it('should render the icon element for a tab', () => {
2079 let renderer = new TabBar.Renderer();
2080 let vNode = renderer.renderIcon({ title, current: true, zIndex: 1 });
2081 let node = VirtualDOM.realize(vNode as VirtualElement);
2082 expect(node.className).to.contain('lm-TabBar-tabIcon');
2083 expect(node.classList.contains(title.iconClass)).to.equal(true);
2084 });
2085 });
2086
2087 describe('#renderLabel()', () => {
2088 it('should render the label element for a tab', () => {
2089 let renderer = new TabBar.Renderer();
2090 let vNode = renderer.renderLabel({ title, current: true, zIndex: 1 });
2091 let label = VirtualDOM.realize(vNode);
2092 expect(label.className).to.contain('lm-TabBar-tabLabel');
2093 expect(label.textContent).to.equal(title.label);
2094 });
2095 });
2096
2097 describe('#renderCloseIcon()', () => {
2098 it('should render the close icon element for a tab', () => {
2099 let renderer = new TabBar.Renderer();
2100 let vNode = renderer.renderCloseIcon({
2101 title,
2102 current: true,
2103 zIndex: 1
2104 });
2105 let icon = VirtualDOM.realize(vNode);
2106 expect(icon.className).to.contain('lm-TabBar-tabCloseIcon');
2107 });
2108 });
2109
2110 describe('#createTabKey()', () => {
2111 it('should create a unique render key for the tab', () => {
2112 let renderer = new TabBar.Renderer();
2113 let key = renderer.createTabKey({ title, current: true, zIndex: 1 });
2114 let newKey = renderer.createTabKey({
2115 title,
2116 current: true,
2117 zIndex: 1
2118 });
2119 expect(key).to.equal(newKey);
2120 });
2121 });
2122
2123 describe('#createTabStyle()', () => {
2124 it('should create the inline style object for a tab', () => {
2125 let renderer = new TabBar.Renderer();
2126 let style = renderer.createTabStyle({
2127 title,
2128 current: true,
2129 zIndex: 1
2130 });
2131 expect(style['zIndex']).to.equal('1');
2132 });
2133 });
2134
2135 describe('#createTabClass()', () => {
2136 it('should create the class name for the tab', () => {
2137 let renderer = new TabBar.Renderer();
2138 let className = renderer.createTabClass({
2139 title,
2140 current: true,
2141 zIndex: 1
2142 });
2143 expect(className).to.contain('lm-TabBar-tab');
2144 expect(className).to.contain('lm-mod-closable');
2145 expect(className).to.contain('lm-mod-current');
2146 });
2147 });
2148
2149 describe('#createIconClass()', () => {
2150 it('should create class name for the tab icon', () => {
2151 let renderer = new TabBar.Renderer();
2152 let className = renderer.createIconClass({
2153 title,
2154 current: true,
2155 zIndex: 1
2156 });
2157 expect(className).to.contain('lm-TabBar-tabIcon');
2158 expect(className).to.contain(title.iconClass);
2159 });
2160 });
2161 });
2162
2163 describe('.defaultRenderer', () => {
2164 it('should be an instance of `Renderer`', () => {
2165 expect(TabBar.defaultRenderer).to.be.an.instanceof(TabBar.Renderer);
2166 });
2167 });
2168 });
2169 });