Debian Codegraph @

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

raw
1 /* eslint-disable @typescript-eslint/no-empty-function */
2 // Copyright (c) Jupyter Development Team.
3 // Distributed under the terms of the Modified BSD License.
4 /*-----------------------------------------------------------------------------
5 | Copyright (c) 2014-2017, PhosphorJS Contributors
6 |
7 | Distributed under the terms of the BSD 3-Clause License.
8 |
9 | The full license is in the file LICENSE, distributed with this software.
10 |----------------------------------------------------------------------------*/
11 import { expect } from 'chai';
12
13 import { CommandRegistry } from '@lumino/commands';
14
15 import { Platform } from '@lumino/domutils';
16
17 import { MessageLoop } from '@lumino/messaging';
18
19 import { h, VirtualDOM } from '@lumino/virtualdom';
20
21 import { CommandPalette, Widget } from '@lumino/widgets';
22
23 class LogPalette extends CommandPalette {
24 events: string[] = [];
25
26 handleEvent(event: Event): void {
27 super.handleEvent(event);
28 this.events.push(event.type);
29 }
30 }
31
32 const bubbles = true;
33 const defaultOptions: CommandPalette.IItemOptions = {
34 command: 'test',
35 category: 'Test Category',
36 args: { foo: 'bar' },
37 rank: 42
38 };
39
40 describe('@lumino/widgets', () => {
41 let commands: CommandRegistry;
42 let palette: CommandPalette;
43
44 beforeEach(() => {
45 commands = new CommandRegistry();
46 palette = new CommandPalette({ commands });
47 Widget.attach(palette, document.body);
48 });
49
50 afterEach(() => {
51 palette.dispose();
52 });
53
54 describe('CommandPalette', () => {
55 describe('#constructor()', () => {
56 it('should accept command palette instantiation options', () => {
57 expect(palette).to.be.an.instanceof(CommandPalette);
58 expect(palette.node.classList.contains('lm-CommandPalette')).to.equal(
59 true
60 );
61 });
62 });
63
64 describe('#dispose()', () => {
65 it('should dispose of the resources held by the command palette', () => {
66 palette.addItem(defaultOptions);
67 palette.dispose();
68 expect(palette.items.length).to.equal(0);
69 expect(palette.isDisposed).to.equal(true);
70 });
71 });
72
73 describe('#commands', () => {
74 it('should get the command registry for the command palette', () => {
75 expect(palette.commands).to.equal(commands);
76 });
77 });
78
79 describe('#renderer', () => {
80 it('should default to the default renderer', () => {
81 expect(palette.renderer).to.equal(CommandPalette.defaultRenderer);
82 });
83 });
84
85 describe('#searchNode', () => {
86 it('should return the search node of a command palette', () => {
87 expect(
88 palette.searchNode.classList.contains('lm-CommandPalette-search')
89 ).to.equal(true);
90 });
91 });
92
93 describe('#inputNode', () => {
94 it('should return the input node of a command palette', () => {
95 expect(
96 palette.inputNode.classList.contains('lm-CommandPalette-input')
97 ).to.equal(true);
98 });
99 });
100
101 describe('#contentNode', () => {
102 it('should return the content node of a command palette', () => {
103 expect(
104 palette.contentNode.classList.contains('lm-CommandPalette-content')
105 ).to.equal(true);
106 });
107 });
108
109 describe('#items', () => {
110 it('should be a read-only array of the palette items', () => {
111 expect(palette.items.length).to.equal(0);
112 palette.addItem(defaultOptions);
113 expect(palette.items.length).to.equal(1);
114 expect(palette.items[0].command).to.equal('test');
115 });
116 });
117
118 describe('#addItems()', () => {
119 it('should add items to a command palette using options', () => {
120 const item = {
121 command: 'test2',
122 category: 'Test Category',
123 args: { foo: 'bar' },
124 rank: 100
125 };
126
127 expect(palette.items.length).to.equal(0);
128 palette.addItems([defaultOptions, item]);
129 expect(palette.items.length).to.equal(2);
130 expect(palette.items[0].command).to.equal('test');
131 expect(palette.items[1].command).to.equal('test2');
132 });
133 });
134
135 describe('#addItem()', () => {
136 it('should add an item to a command palette using options', () => {
137 expect(palette.items.length).to.equal(0);
138 palette.addItem(defaultOptions);
139 expect(palette.items.length).to.equal(1);
140 expect(palette.items[0].command).to.equal('test');
141 });
142
143 context('CommandPalette.IItem', () => {
144 describe('#command', () => {
145 it('should return the command name of a command item', () => {
146 let item = palette.addItem(defaultOptions);
147 expect(item.command).to.equal('test');
148 });
149 });
150
151 describe('#args', () => {
152 it('should return the args of a command item', () => {
153 let item = palette.addItem(defaultOptions);
154 expect(item.args).to.deep.equal(defaultOptions.args);
155 });
156
157 it('should default to an empty object', () => {
158 let item = palette.addItem({ command: 'test', category: 'test' });
159 expect(item.args).to.deep.equal({});
160 });
161 });
162
163 describe('#category', () => {
164 it('should return the category of a command item', () => {
165 let item = palette.addItem(defaultOptions);
166 expect(item.category).to.equal(defaultOptions.category);
167 });
168 });
169
170 describe('#rank', () => {
171 it('should return the rank of a command item', () => {
172 let item = palette.addItem(defaultOptions);
173 expect(item.rank).to.deep.equal(defaultOptions.rank);
174 });
175
176 it('should default to `Infinity`', () => {
177 let item = palette.addItem({ command: 'test', category: 'test' });
178 expect(item.rank).to.equal(Infinity);
179 });
180 });
181
182 describe('#label', () => {
183 it('should return the label of a command item', () => {
184 let label = 'test label';
185 commands.addCommand('test', { execute: () => {}, label });
186 let item = palette.addItem(defaultOptions);
187 expect(item.label).to.equal(label);
188 });
189 });
190
191 describe('#caption', () => {
192 it('should return the caption of a command item', () => {
193 let caption = 'test caption';
194 commands.addCommand('test', { execute: () => {}, caption });
195 let item = palette.addItem(defaultOptions);
196 expect(item.caption).to.equal(caption);
197 });
198 });
199
200 describe('#className', () => {
201 it('should return the class name of a command item', () => {
202 let className = 'testClass';
203 commands.addCommand('test', { execute: () => {}, className });
204 let item = palette.addItem(defaultOptions);
205 expect(item.className).to.equal(className);
206 });
207 });
208
209 describe('#isEnabled', () => {
210 it('should return whether a command item is enabled', () => {
211 let called = false;
212 commands.addCommand('test', {
213 execute: () => {},
214 isEnabled: () => {
215 called = true;
216 return false;
217 }
218 });
219 let item = palette.addItem(defaultOptions);
220 expect(called).to.equal(false);
221 expect(item.isEnabled).to.equal(false);
222 expect(called).to.equal(true);
223 });
224 });
225
226 describe('#isToggled', () => {
227 it('should return whether a command item is toggled', () => {
228 let called = false;
229 commands.addCommand('test', {
230 execute: () => {},
231 isToggled: () => {
232 called = true;
233 return true;
234 }
235 });
236 let item = palette.addItem(defaultOptions);
237 expect(called).to.equal(false);
238 expect(item.isToggled).to.equal(true);
239 expect(called).to.equal(true);
240 });
241 });
242
243 describe('#isVisible', () => {
244 it('should return whether a command item is visible', () => {
245 let called = false;
246 commands.addCommand('test', {
247 execute: () => {},
248 isVisible: () => {
249 called = true;
250 return false;
251 }
252 });
253 let item = palette.addItem(defaultOptions);
254 expect(called).to.equal(false);
255 expect(item.isVisible).to.equal(false);
256 expect(called).to.equal(true);
257 });
258 });
259
260 describe('#keyBinding', () => {
261 it('should return the key binding of a command item', () => {
262 commands.addKeyBinding({
263 keys: ['Ctrl A'],
264 selector: 'body',
265 command: 'test',
266 args: defaultOptions.args
267 });
268 let item = palette.addItem(defaultOptions);
269 expect(item.keyBinding!.keys).to.deep.equal(['Ctrl A']);
270 });
271 });
272 });
273 });
274
275 describe('#removeItem()', () => {
276 it('should remove an item from a command palette by item', () => {
277 expect(palette.items.length).to.equal(0);
278 let item = palette.addItem(defaultOptions);
279 expect(palette.items.length).to.equal(1);
280 palette.removeItem(item);
281 expect(palette.items.length).to.equal(0);
282 });
283 });
284
285 describe('#removeItemAt()', () => {
286 it('should remove an item from a command palette by index', () => {
287 expect(palette.items.length).to.equal(0);
288 palette.addItem(defaultOptions);
289 expect(palette.items.length).to.equal(1);
290 palette.removeItemAt(0);
291 expect(palette.items.length).to.equal(0);
292 });
293 });
294
295 describe('#clearItems()', () => {
296 it('should remove all items from a command palette', () => {
297 expect(palette.items.length).to.equal(0);
298 palette.addItem({ command: 'test', category: 'one' });
299 palette.addItem({ command: 'test', category: 'two' });
300 expect(palette.items.length).to.equal(2);
301 palette.clearItems();
302 expect(palette.items.length).to.equal(0);
303 });
304 });
305
306 describe('#refresh()', () => {
307 it('should schedule a refresh of the search items', () => {
308 commands.addCommand('test', { execute: () => {}, label: 'test' });
309 palette.addItem(defaultOptions);
310
311 MessageLoop.flush();
312
313 let content = palette.contentNode;
314 let itemClass = '.lm-CommandPalette-item';
315 let items = () => content.querySelectorAll(itemClass);
316
317 expect(items()).to.have.length(1);
318 palette.inputNode.value = 'x';
319 palette.refresh();
320 MessageLoop.flush();
321 expect(items()).to.have.length(0);
322 });
323
324 it('should search a list of commands', () => {
325 // Add several commands to the command registry and the palette
326 commands.addCommand('example:cut', {
327 label: 'Cut',
328 mnemonic: 1,
329 iconClass: 'fa fa-cut',
330 execute: () => {
331 console.log('Cut');
332 }
333 });
334
335 commands.addCommand('example:copy', {
336 label: 'Copy File',
337 mnemonic: 0,
338 iconClass: 'fa fa-copy',
339 execute: () => {
340 console.log('Copy');
341 }
342 });
343
344 commands.addCommand('example:paste', {
345 label: 'Paste',
346 mnemonic: 0,
347 iconClass: 'fa fa-paste',
348 execute: () => {
349 console.log('Paste');
350 }
351 });
352
353 commands.addCommand('example:new-tab', {
354 label: 'New Tab',
355 mnemonic: 0,
356 caption: 'Open a new tab',
357 execute: () => {
358 console.log('New Tab');
359 }
360 });
361
362 commands.addCommand('example:close-tab', {
363 label: 'Close Tab',
364 mnemonic: 2,
365 caption: 'Close the current tab',
366 execute: () => {
367 console.log('Close Tab');
368 }
369 });
370
371 commands.addCommand('example:save-on-exit', {
372 label: 'Save on Exit',
373 mnemonic: 0,
374 caption: 'Toggle the save on exit flag',
375 execute: () => {
376 console.log('Save on Exit');
377 }
378 });
379
380 commands.addCommand('example:open-task-manager', {
381 label: 'Task Manager',
382 mnemonic: 5,
383 isEnabled: () => false,
384 execute: () => {}
385 });
386
387 commands.addCommand('example:close', {
388 label: 'Close',
389 mnemonic: 0,
390 iconClass: 'fa fa-close',
391 execute: () => {
392 console.log('Close');
393 }
394 });
395
396 commands.addCommand('example:one', {
397 label: 'One',
398 execute: () => {
399 console.log('One');
400 }
401 });
402
403 commands.addCommand('example:two', {
404 label: 'Two',
405 execute: () => {
406 console.log('Two');
407 }
408 });
409
410 commands.addCommand('example:three', {
411 label: 'Three',
412 execute: () => {
413 console.log('Three');
414 }
415 });
416
417 commands.addCommand('example:four', {
418 label: 'Four',
419 execute: () => {
420 console.log('Four');
421 }
422 });
423
424 commands.addCommand('example:black', {
425 label: 'Black',
426 execute: () => {
427 console.log('Black');
428 }
429 });
430
431 commands.addCommand('example:clear-cell', {
432 label: 'Clear Cell',
433 execute: () => {
434 console.log('Clear Cell');
435 }
436 });
437
438 commands.addCommand('example:cut-cells', {
439 label: 'Cut Cell(s)',
440 execute: () => {
441 console.log('Cut Cell(s)');
442 }
443 });
444
445 commands.addCommand('example:run-cell', {
446 label: 'Run Cell',
447 execute: () => {
448 console.log('Run Cell');
449 }
450 });
451
452 commands.addCommand('example:cell-test', {
453 label: 'Cell Test',
454 execute: () => {
455 console.log('Cell Test');
456 }
457 });
458
459 commands.addCommand('notebook:new', {
460 label: 'New Notebook',
461 execute: () => {
462 console.log('New Notebook');
463 }
464 });
465
466 commands.addKeyBinding({
467 keys: ['Accel X'],
468 selector: 'body',
469 command: 'example:cut'
470 });
471
472 commands.addKeyBinding({
473 keys: ['Accel C'],
474 selector: 'body',
475 command: 'example:copy'
476 });
477
478 commands.addKeyBinding({
479 keys: ['Accel V'],
480 selector: 'body',
481 command: 'example:paste'
482 });
483
484 commands.addKeyBinding({
485 keys: ['Accel J', 'Accel J'],
486 selector: 'body',
487 command: 'example:new-tab'
488 });
489
490 commands.addKeyBinding({
491 keys: ['Accel M'],
492 selector: 'body',
493 command: 'example:open-task-manager'
494 });
495
496 let palette = new CommandPalette({ commands });
497 Widget.attach(palette, document.body);
498 try {
499 palette.addItem({ command: 'example:cut', category: 'Edit' });
500 palette.addItem({ command: 'example:copy', category: 'Edit' });
501 palette.addItem({ command: 'example:paste', category: 'Edit' });
502 palette.addItem({ command: 'example:one', category: 'Number' });
503 palette.addItem({ command: 'example:two', category: 'Number' });
504 palette.addItem({ command: 'example:three', category: 'Number' });
505 palette.addItem({ command: 'example:four', category: 'Number' });
506 palette.addItem({ command: 'example:black', category: 'Number' });
507 palette.addItem({ command: 'example:new-tab', category: 'File' });
508 palette.addItem({ command: 'example:close-tab', category: 'File' });
509 palette.addItem({
510 command: 'example:save-on-exit',
511 category: 'File'
512 });
513 palette.addItem({
514 command: 'example:open-task-manager',
515 category: 'File'
516 });
517 palette.addItem({ command: 'example:close', category: 'File' });
518 palette.addItem({
519 command: 'example:clear-cell',
520 category: 'Notebook Cell Operations'
521 });
522 palette.addItem({
523 command: 'example:cut-cells',
524 category: 'Notebook Cell Operations'
525 });
526 palette.addItem({
527 command: 'example:run-cell',
528 category: 'Notebook Cell Operations'
529 });
530 palette.addItem({
531 command: 'example:cell-test',
532 category: 'Console'
533 });
534 palette.addItem({ command: 'notebook:new', category: 'Notebook' });
535 palette.id = 'palette';
536
537 // Search the command palette: update the inputNode, then force a refresh
538 palette.inputNode.value = 'clea';
539 palette.refresh();
540 MessageLoop.flush();
541
542 // Expect that headers and items appear in descending score order,
543 // even if the same header occurs multiple times.
544 const children = palette.contentNode.children;
545 expect(children.length).to.equal(7);
546 expect(children[0].textContent).to.equal('Notebook Cell Operations');
547 expect(children[1].getAttribute('data-command')).to.equal(
548 'example:clear-cell'
549 );
550 // The next match should be from a different category
551 expect(children[2].textContent).to.equal('File');
552 expect(children[3].getAttribute('data-command')).to.equal(
553 'example:close-tab'
554 );
555 // The next match should be the same as in a previous category
556 expect(children[4].textContent).to.equal('Notebook Cell Operations');
557 expect(children[5].getAttribute('data-command')).to.equal(
558 'example:cut-cells'
559 );
560 // The next match has the same category as the previous one did, so the header is not repeated
561 expect(children[6].getAttribute('data-command')).to.equal(
562 'example:run-cell'
563 );
564 } finally {
565 palette.dispose();
566 }
567 });
568
569 it('should clear the widget content if hidden', () => {
570 commands.addCommand('test', { execute: () => {}, label: 'test' });
571 palette.addItem({ command: 'test', category: 'test' });
572 const content = palette.contentNode;
573 const itemClass = '.lm-CommandPalette-item';
574 const items = () => content.querySelectorAll(itemClass);
575
576 palette.refresh();
577 MessageLoop.flush();
578
579 expect(items()).to.have.length(1);
580
581 palette.hide();
582 palette.refresh();
583 MessageLoop.flush();
584 expect(items()).to.have.length(0);
585 });
586
587 it('should clear the widget content if container is hidden', () => {
588 const parent = new Widget();
589 Widget.attach(parent, document.body);
590 try {
591 palette.parent = parent;
592 commands.addCommand('test', { execute: () => {}, label: 'test' });
593 palette.addItem({ command: 'test', category: 'test' });
594 const content = palette.contentNode;
595 const itemClass = '.lm-CommandPalette-item';
596 const items = () => content.querySelectorAll(itemClass);
597
598 palette.refresh();
599 MessageLoop.flush();
600
601 expect(items()).to.have.length(1);
602
603 parent.hide();
604 palette.refresh();
605 MessageLoop.flush();
606 expect(items()).to.have.length(0);
607 } finally {
608 parent.dispose();
609 }
610 });
611 });
612
613 describe('#handleEvent()', () => {
614 it('should handle click, keydown, and input events', () => {
615 let palette = new LogPalette({ commands });
616 Widget.attach(palette, document.body);
617 try {
618 ['click', 'keydown', 'input'].forEach(type => {
619 palette.node.dispatchEvent(new Event(type, { bubbles }));
620 expect(palette.events).to.contain(type);
621 });
622 } finally {
623 palette.dispose();
624 }
625 });
626
627 context('click', () => {
628 it('should trigger a command when its item is clicked', () => {
629 let called = false;
630 commands.addCommand('test', { execute: () => (called = true) });
631
632 palette.addItem(defaultOptions);
633 MessageLoop.flush();
634
635 let node = palette.contentNode.querySelector(
636 '.lm-CommandPalette-item'
637 )!;
638 node.dispatchEvent(new MouseEvent('click', { bubbles }));
639 expect(called).to.equal(true);
640 });
641
642 it('should ignore the event if it is not a left click', () => {
643 let called = false;
644 commands.addCommand('test', { execute: () => (called = true) });
645
646 palette.addItem(defaultOptions);
647 MessageLoop.flush();
648
649 let node = palette.contentNode.querySelector(
650 '.lm-CommandPalette-item'
651 )!;
652 node.dispatchEvent(new MouseEvent('click', { bubbles, button: 1 }));
653 expect(called).to.equal(false);
654 });
655 });
656
657 context('keydown', () => {
658 it('should navigate down if down arrow is pressed', () => {
659 commands.addCommand('test', { execute: () => {} });
660 let content = palette.contentNode;
661
662 palette.addItem(defaultOptions);
663 MessageLoop.flush();
664
665 let node = content.querySelector('.lm-mod-active');
666 expect(node).to.equal(null);
667 palette.node.dispatchEvent(
668 new KeyboardEvent('keydown', {
669 bubbles,
670 keyCode: 40 // Down arrow
671 })
672 );
673 MessageLoop.flush();
674 node = content.querySelector('.lm-CommandPalette-item.lm-mod-active');
675 expect(node).to.not.equal(null);
676 });
677
678 it('should navigate up if up arrow is pressed', () => {
679 commands.addCommand('test', { execute: () => {} });
680 let content = palette.contentNode;
681
682 palette.addItem(defaultOptions);
683 MessageLoop.flush();
684
685 let node = content.querySelector('.lm-mod-active');
686 expect(node).to.equal(null);
687 palette.node.dispatchEvent(
688 new KeyboardEvent('keydown', {
689 bubbles,
690 keyCode: 38 // Up arrow
691 })
692 );
693 MessageLoop.flush();
694 node = content.querySelector('.lm-CommandPalette-item.lm-mod-active');
695 expect(node).to.not.equal(null);
696 });
697
698 it('should ignore if modifier keys are pressed', () => {
699 let called = false;
700 commands.addCommand('test', { execute: () => (called = true) });
701 let content = palette.contentNode;
702
703 palette.addItem(defaultOptions);
704 MessageLoop.flush();
705
706 let node = content.querySelector('.lm-mod-active');
707
708 expect(node).to.equal(null);
709 ['altKey', 'ctrlKey', 'shiftKey', 'metaKey'].forEach(key => {
710 palette.node.dispatchEvent(
711 new KeyboardEvent('keydown', {
712 bubbles,
713 [key]: true,
714 keyCode: 38 // Up arrow
715 })
716 );
717 node = content.querySelector(
718 '.lm-CommandPalette-item.lm-mod-active'
719 );
720 expect(node).to.equal(null);
721 });
722 expect(called).to.be.false;
723 });
724
725 it('should trigger active item if enter is pressed', () => {
726 let called = false;
727 commands.addCommand('test', {
728 execute: () => (called = true)
729 });
730 let content = palette.contentNode;
731
732 palette.addItem(defaultOptions);
733 MessageLoop.flush();
734
735 expect(content.querySelector('.lm-mod-active')).to.equal(null);
736 palette.node.dispatchEvent(
737 new KeyboardEvent('keydown', {
738 bubbles,
739 keyCode: 40 // Down arrow
740 })
741 );
742 palette.node.dispatchEvent(
743 new KeyboardEvent('keydown', {
744 bubbles,
745 keyCode: 13 // Enter
746 })
747 );
748 expect(called).to.equal(true);
749 });
750 });
751
752 context('input', () => {
753 it('should filter the list of visible items', () => {
754 ['A', 'B', 'C', 'D', 'E'].forEach(name => {
755 commands.addCommand(name, { execute: () => {}, label: name });
756 palette.addItem({ command: name, category: 'test' });
757 });
758
759 MessageLoop.flush();
760
761 let content = palette.contentNode;
762 let itemClass = '.lm-CommandPalette-item';
763 let items = () => content.querySelectorAll(itemClass);
764
765 expect(items()).to.have.length(5);
766 palette.inputNode.value = 'A';
767 palette.refresh();
768 MessageLoop.flush();
769 expect(items()).to.have.length(1);
770 });
771
772 it('should filter by both text and category', () => {
773 let categories = ['Z', 'Y'];
774 let names = [
775 ['A1', 'B2', 'C3', 'D4', 'E5'],
776 ['F1', 'G2', 'H3', 'I4', 'J5']
777 ];
778 names.forEach((values, index) => {
779 values.forEach(command => {
780 palette.addItem({ command, category: categories[index] });
781 commands.addCommand(command, {
782 execute: () => {},
783 label: command
784 });
785 });
786 });
787
788 MessageLoop.flush();
789
790 let headers = () =>
791 palette.node.querySelectorAll('.lm-CommandPalette-header');
792 let items = () =>
793 palette.node.querySelectorAll('.lm-CommandPalette-item');
794 let input = (value: string) => {
795 palette.inputNode.value = value;
796 palette.refresh();
797 MessageLoop.flush();
798 };
799
800 expect(items()).to.have.length(10);
801 input(`${categories[1]}`); // Category match
802 expect(items()).to.have.length(5);
803 input(`${names[1][0]}`); // Label match
804 expect(items()).to.have.length(1);
805 input(`${categories[1]} B`); // No match
806 expect(items()).to.have.length(0);
807 input(`${categories[1]} I`); // Category and text match
808 expect(items()).to.have.length(1);
809
810 input('1'); // Multi-category match
811 expect(headers()).to.have.length(2);
812 expect(items()).to.have.length(2);
813 });
814 });
815 });
816
817 describe('.Renderer', () => {
818 let renderer = new CommandPalette.Renderer();
819 let item: CommandPalette.IItem = null!;
820 let enabledFlag = true;
821 let toggledFlag = false;
822
823 beforeEach(() => {
824 enabledFlag = true;
825 toggledFlag = false;
826 commands.addCommand('test', {
827 label: 'Test Command',
828 caption: 'A simple test command',
829 className: 'testClass',
830 isEnabled: () => enabledFlag,
831 isToggled: () => toggledFlag,
832 execute: () => {}
833 });
834 commands.addKeyBinding({
835 command: 'test',
836 keys: ['Ctrl A'],
837 selector: 'body'
838 });
839 item = palette.addItem({
840 command: 'test',
841 category: 'Test Category'
842 });
843 });
844
845 describe('#renderHeader()', () => {
846 it('should render a header node for the palette', () => {
847 let vNode = renderer.renderHeader({
848 category: 'Test Category',
849 indices: null
850 });
851 let node = VirtualDOM.realize(vNode);
852 expect(node.classList.contains('lm-CommandPalette-header')).to.equal(
853 true
854 );
855 expect(node.innerHTML).to.equal('Test Category');
856 });
857
858 it('should mark the matching indices', () => {
859 let vNode = renderer.renderHeader({
860 category: 'Test Category',
861 indices: [1, 2, 6, 7, 8]
862 });
863 let node = VirtualDOM.realize(vNode);
864 expect(node.classList.contains('lm-CommandPalette-header')).to.equal(
865 true
866 );
867 expect(node.innerHTML).to.equal(
868 'T<mark>es</mark>t C<mark>ate</mark>gory'
869 );
870 });
871 });
872
873 describe('#renderItem()', () => {
874 it('should render an item node for the palette', () => {
875 let vNode = renderer.renderItem({
876 item,
877 indices: null,
878 active: false
879 });
880 let node = VirtualDOM.realize(vNode);
881 expect(node.classList.contains('lm-CommandPalette-item')).to.equal(
882 true
883 );
884 expect(node.classList.contains('lm-mod-disabled')).to.equal(false);
885 expect(node.classList.contains('lm-mod-toggled')).to.equal(false);
886 expect(node.classList.contains('lm-mod-active')).to.equal(false);
887 expect(node.classList.contains('testClass')).to.equal(true);
888 expect(node.getAttribute('data-command')).to.equal('test');
889 expect(
890 node.querySelector('.lm-CommandPalette-itemShortcut')
891 ).to.not.equal(null);
892 expect(
893 node.querySelector('.lm-CommandPalette-itemLabel')
894 ).to.not.equal(null);
895 expect(
896 node.querySelector('.lm-CommandPalette-itemCaption')
897 ).to.not.equal(null);
898 });
899
900 it('should handle the disabled item state', () => {
901 enabledFlag = false;
902 let vNode = renderer.renderItem({
903 item,
904 indices: null,
905 active: false
906 });
907 let node = VirtualDOM.realize(vNode);
908 expect(node.classList.contains('lm-mod-disabled')).to.equal(true);
909 });
910
911 it('should handle the toggled item state', () => {
912 toggledFlag = true;
913 let vNode = renderer.renderItem({
914 item,
915 indices: null,
916 active: false
917 });
918 let node = VirtualDOM.realize(vNode);
919 expect(node.classList.contains('lm-mod-toggled')).to.equal(true);
920 });
921
922 it('should handle the active state', () => {
923 let vNode = renderer.renderItem({
924 item,
925 indices: null,
926 active: true
927 });
928 let node = VirtualDOM.realize(vNode);
929 expect(node.classList.contains('lm-mod-active')).to.equal(true);
930 });
931 });
932
933 describe('#renderEmptyMessage()', () => {
934 it('should render an empty message node for the palette', () => {
935 let vNode = renderer.renderEmptyMessage({ query: 'foo' });
936 let node = VirtualDOM.realize(vNode);
937 expect(
938 node.classList.contains('lm-CommandPalette-emptyMessage')
939 ).to.equal(true);
940 expect(node.innerHTML).to.equal("No commands found that match 'foo'");
941 });
942 });
943
944 describe('#renderItemShortcut()', () => {
945 it('should render an item shortcut node', () => {
946 let vNode = renderer.renderItemShortcut({
947 item,
948 indices: null,
949 active: false
950 });
951 let node = VirtualDOM.realize(vNode);
952 expect(
953 node.classList.contains('lm-CommandPalette-itemShortcut')
954 ).to.equal(true);
955 if (Platform.IS_MAC) {
956 expect(node.innerHTML).to.equal('\u2303 A');
957 } else {
958 expect(node.innerHTML).to.equal('Ctrl+A');
959 }
960 });
961 });
962
963 describe('#renderItemLabel()', () => {
964 it('should render an item label node', () => {
965 let vNode = renderer.renderItemLabel({
966 item,
967 indices: [1, 2, 3],
968 active: false
969 });
970 let node = VirtualDOM.realize(vNode);
971 expect(
972 node.classList.contains('lm-CommandPalette-itemLabel')
973 ).to.equal(true);
974 expect(node.innerHTML).to.equal('T<mark>est</mark> Command');
975 });
976 });
977
978 describe('#renderItemCaption()', () => {
979 it('should render an item caption node', () => {
980 let vNode = renderer.renderItemCaption({
981 item,
982 indices: null,
983 active: false
984 });
985 let node = VirtualDOM.realize(vNode);
986 expect(
987 node.classList.contains('lm-CommandPalette-itemCaption')
988 ).to.equal(true);
989 expect(node.innerHTML).to.equal('A simple test command');
990 });
991 });
992
993 describe('#createItemClass()', () => {
994 it('should create the full class name for the item node', () => {
995 let name = renderer.createItemClass({
996 item,
997 indices: null,
998 active: false
999 });
1000 let expected = 'lm-CommandPalette-item testClass';
1001 expect(name).to.equal(expected);
1002 });
1003
1004 it('should handle the boolean states', () => {
1005 enabledFlag = false;
1006 toggledFlag = true;
1007 let name = renderer.createItemClass({
1008 item,
1009 indices: null,
1010 active: true
1011 });
1012 let expected =
1013 'lm-CommandPalette-item lm-mod-disabled lm-mod-toggled lm-mod-active testClass';
1014 expect(name).to.equal(expected);
1015 });
1016 });
1017
1018 describe('#createItemDataset()', () => {
1019 it('should create the item dataset', () => {
1020 let dataset = renderer.createItemDataset({
1021 item,
1022 indices: null,
1023 active: false
1024 });
1025 expect(dataset).to.deep.equal({ command: 'test' });
1026 });
1027 });
1028
1029 describe('#formatHeader()', () => {
1030 it('should format unmatched header content', () => {
1031 let child1 = renderer.formatHeader({
1032 category: 'Test Category',
1033 indices: null
1034 });
1035 let child2 = renderer.formatHeader({
1036 category: 'Test Category',
1037 indices: []
1038 });
1039 expect(child1).to.equal('Test Category');
1040 expect(child2).to.equal('Test Category');
1041 });
1042
1043 it('should format matched header content', () => {
1044 let child = renderer.formatHeader({
1045 category: 'Test Category',
1046 indices: [1, 2, 6, 7, 8]
1047 });
1048 let node = VirtualDOM.realize(h.div(child));
1049 expect(node.innerHTML).to.equal(
1050 'T<mark>es</mark>t C<mark>ate</mark>gory'
1051 );
1052 });
1053 });
1054
1055 describe('#formatEmptyMessage()', () => {
1056 it('should format the empty message text', () => {
1057 let child = renderer.formatEmptyMessage({ query: 'foo' });
1058 expect(child).to.equal("No commands found that match 'foo'");
1059 });
1060 });
1061
1062 describe('#formatItemShortcut()', () => {
1063 it('should format the item shortcut text', () => {
1064 let child = renderer.formatItemShortcut({
1065 item,
1066 indices: null,
1067 active: false
1068 });
1069 if (Platform.IS_MAC) {
1070 expect(child).to.equal('\u2303 A');
1071 } else {
1072 expect(child).to.equal('Ctrl+A');
1073 }
1074 });
1075 });
1076
1077 describe('#formatItemLabel()', () => {
1078 it('should format unmatched label content', () => {
1079 let child1 = renderer.formatItemLabel({
1080 item,
1081 indices: null,
1082 active: false
1083 });
1084 let child2 = renderer.formatItemLabel({
1085 item,
1086 indices: [],
1087 active: false
1088 });
1089 expect(child1).to.equal('Test Command');
1090 expect(child2).to.equal('Test Command');
1091 });
1092
1093 it('should format matched label content', () => {
1094 let child = renderer.formatItemLabel({
1095 item,
1096 indices: [1, 2, 3],
1097 active: false
1098 });
1099 let node = VirtualDOM.realize(h.div(child));
1100 expect(node.innerHTML).to.equal('T<mark>est</mark> Command');
1101 });
1102 });
1103
1104 describe('#formatItemCaption()', () => {
1105 it('should format the item caption text', () => {
1106 let child = renderer.formatItemCaption({
1107 item,
1108 indices: null,
1109 active: false
1110 });
1111 expect(child).to.equal('A simple test command');
1112 });
1113 });
1114 });
1115 });
1116 });