@@ -44,6 +44,7 @@ describe('@lumino/widgets', () => {
44
44
beforeEach ( ( ) => {
45
45
commands = new CommandRegistry ( ) ;
46
46
palette = new CommandPalette ( { commands } ) ;
47
+ Widget . attach ( palette , document . body ) ;
47
48
} ) ;
48
49
49
50
afterEach ( ( ) => {
@@ -493,77 +494,134 @@ describe('@lumino/widgets', () => {
493
494
} ) ;
494
495
495
496
let palette = new CommandPalette ( { commands } ) ;
496
- palette . addItem ( { command : 'example:cut' , category : 'Edit' } ) ;
497
- palette . addItem ( { command : 'example:copy' , category : 'Edit' } ) ;
498
- palette . addItem ( { command : 'example:paste' , category : 'Edit' } ) ;
499
- palette . addItem ( { command : 'example:one' , category : 'Number' } ) ;
500
- palette . addItem ( { command : 'example:two' , category : 'Number' } ) ;
501
- palette . addItem ( { command : 'example:three' , category : 'Number' } ) ;
502
- palette . addItem ( { command : 'example:four' , category : 'Number' } ) ;
503
- palette . addItem ( { command : 'example:black' , category : 'Number' } ) ;
504
- palette . addItem ( { command : 'example:new-tab' , category : 'File' } ) ;
505
- palette . addItem ( { command : 'example:close-tab' , category : 'File' } ) ;
506
- palette . addItem ( { command : 'example:save-on-exit' , category : 'File' } ) ;
507
- palette . addItem ( {
508
- command : 'example:open-task-manager' ,
509
- category : 'File'
510
- } ) ;
511
- palette . addItem ( { command : 'example:close' , category : 'File' } ) ;
512
- palette . addItem ( {
513
- command : 'example:clear-cell' ,
514
- category : 'Notebook Cell Operations'
515
- } ) ;
516
- palette . addItem ( {
517
- command : 'example:cut-cells' ,
518
- category : 'Notebook Cell Operations'
519
- } ) ;
520
- palette . addItem ( {
521
- command : 'example:run-cell' ,
522
- category : 'Notebook Cell Operations'
523
- } ) ;
524
- palette . addItem ( { command : 'example:cell-test' , category : 'Console' } ) ;
525
- palette . addItem ( { command : 'notebook:new' , category : 'Notebook' } ) ;
526
- palette . id = 'palette' ;
527
-
528
- // Search the command palette: update the inputNode, then force a refresh
529
- palette . inputNode . value = 'clea' ;
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
+
530
576
palette . refresh ( ) ;
531
577
MessageLoop . flush ( ) ;
532
578
533
- // Expect that headers and items appear in descending score order,
534
- // even if the same header occurs multiple times.
535
- const children = palette . contentNode . children ;
536
- expect ( children . length ) . to . equal ( 7 ) ;
537
- expect ( children [ 0 ] . textContent ) . to . equal ( 'Notebook Cell Operations' ) ;
538
- expect ( children [ 1 ] . getAttribute ( 'data-command' ) ) . to . equal (
539
- 'example:clear-cell'
540
- ) ;
541
- // The next match should be from a different category
542
- expect ( children [ 2 ] . textContent ) . to . equal ( 'File' ) ;
543
- expect ( children [ 3 ] . getAttribute ( 'data-command' ) ) . to . equal (
544
- 'example:close-tab'
545
- ) ;
546
- // The next match should be the same as in a previous category
547
- expect ( children [ 4 ] . textContent ) . to . equal ( 'Notebook Cell Operations' ) ;
548
- expect ( children [ 5 ] . getAttribute ( 'data-command' ) ) . to . equal (
549
- 'example:cut-cells'
550
- ) ;
551
- // The next match has the same category as the previous one did, so the header is not repeated
552
- expect ( children [ 6 ] . getAttribute ( 'data-command' ) ) . to . equal (
553
- 'example:run-cell'
554
- ) ;
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
+ }
555
610
} ) ;
556
611
} ) ;
557
612
558
613
describe ( '#handleEvent()' , ( ) => {
559
614
it ( 'should handle click, keydown, and input events' , ( ) => {
560
615
let palette = new LogPalette ( { commands } ) ;
561
616
Widget . attach ( palette , document . body ) ;
562
- [ 'click' , 'keydown' , 'input' ] . forEach ( type => {
563
- palette . node . dispatchEvent ( new Event ( type , { bubbles } ) ) ;
564
- expect ( palette . events ) . to . contain ( type ) ;
565
- } ) ;
566
- palette . dispose ( ) ;
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
+ }
567
625
} ) ;
568
626
569
627
context ( 'click' , ( ) => {
@@ -572,7 +630,6 @@ describe('@lumino/widgets', () => {
572
630
commands . addCommand ( 'test' , { execute : ( ) => ( called = true ) } ) ;
573
631
574
632
palette . addItem ( defaultOptions ) ;
575
- Widget . attach ( palette , document . body ) ;
576
633
MessageLoop . flush ( ) ;
577
634
578
635
let node = palette . contentNode . querySelector (
@@ -587,7 +644,6 @@ describe('@lumino/widgets', () => {
587
644
commands . addCommand ( 'test' , { execute : ( ) => ( called = true ) } ) ;
588
645
589
646
palette . addItem ( defaultOptions ) ;
590
- Widget . attach ( palette , document . body ) ;
591
647
MessageLoop . flush ( ) ;
592
648
593
649
let node = palette . contentNode . querySelector (
@@ -604,7 +660,6 @@ describe('@lumino/widgets', () => {
604
660
let content = palette . contentNode ;
605
661
606
662
palette . addItem ( defaultOptions ) ;
607
- Widget . attach ( palette , document . body ) ;
608
663
MessageLoop . flush ( ) ;
609
664
610
665
let node = content . querySelector ( '.lm-mod-active' ) ;
@@ -625,7 +680,6 @@ describe('@lumino/widgets', () => {
625
680
let content = palette . contentNode ;
626
681
627
682
palette . addItem ( defaultOptions ) ;
628
- Widget . attach ( palette , document . body ) ;
629
683
MessageLoop . flush ( ) ;
630
684
631
685
let node = content . querySelector ( '.lm-mod-active' ) ;
@@ -647,7 +701,6 @@ describe('@lumino/widgets', () => {
647
701
let content = palette . contentNode ;
648
702
649
703
palette . addItem ( defaultOptions ) ;
650
- Widget . attach ( palette , document . body ) ;
651
704
MessageLoop . flush ( ) ;
652
705
653
706
let node = content . querySelector ( '.lm-mod-active' ) ;
@@ -677,7 +730,6 @@ describe('@lumino/widgets', () => {
677
730
let content = palette . contentNode ;
678
731
679
732
palette . addItem ( defaultOptions ) ;
680
- Widget . attach ( palette , document . body ) ;
681
733
MessageLoop . flush ( ) ;
682
734
683
735
expect ( content . querySelector ( '.lm-mod-active' ) ) . to . equal ( null ) ;
@@ -704,7 +756,6 @@ describe('@lumino/widgets', () => {
704
756
palette . addItem ( { command : name , category : 'test' } ) ;
705
757
} ) ;
706
758
707
- Widget . attach ( palette , document . body ) ;
708
759
MessageLoop . flush ( ) ;
709
760
710
761
let content = palette . contentNode ;
@@ -734,7 +785,6 @@ describe('@lumino/widgets', () => {
734
785
} ) ;
735
786
} ) ;
736
787
737
- Widget . attach ( palette , document . body ) ;
738
788
MessageLoop . flush ( ) ;
739
789
740
790
let headers = ( ) =>
0 commit comments