forked from benmag1/mdl-ext
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu-button.spec.js
More file actions
976 lines (808 loc) · 39.9 KB
/
menu-button.spec.js
File metadata and controls
976 lines (808 loc) · 39.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
'use strict';
import requireUncached from 'require-uncached';
import jsdomify from 'jsdomify';
import {patchJsDom} from '../testutils/patch-jsdom';
import { removeChildElements } from '../../src/utils/dom-utils';
import createMockRaf from '../testutils/mock-raf';
import {
VK_TAB,
VK_ENTER,
VK_ESC,
VK_SPACE,
VK_PAGE_UP,
VK_END,
VK_HOME,
VK_ARROW_LEFT,
VK_ARROW_UP,
VK_ARROW_RIGHT,
VK_ARROW_DOWN,
IS_FOCUSED,
} from '../../src/utils/constants';
const describe = require('mocha').describe;
const before = require('mocha').before;
const after = require('mocha').after;
const it = require('mocha').it;
const expect = require('chai').expect;
const assert = require('chai').assert;
const sinon = require('sinon');
import { shouldBehaveLikeAMdlComponent } from '../testutils/shared-component-behaviours';
const MENU_BUTTON = 'mdlext-js-menu-button';
const MENU_BUTTON_MENU = 'mdlext-menu';
const MENU_BUTTON_MENU_ITEM = 'mdlext-menu__item';
const MENU_BUTTON_MENU_ITEM_SEPARATOR = 'mdlext-menu__item-separator';
describe('MaterialExtMenuButton', () => {
const menu_button_fixture = `
<div role="presentation">
<button class="mdlext-js-menu-button">
<span class="mdlext-menu-button__label">I'm the label!</span>
</button>
<ul class="mdlext-menu">
<li class="mdlext-menu__item">Menu item #1</li>
<li class="mdlext-menu__item">Menu item #2</li>
<li class="mdlext-menu__item">Menu item #n</li>
</ul>
</div>`;
const menu_button_with_disabled_item_fixture = `
<div role="presentation">
<button class="mdlext-js-menu-button">
<span class="mdlext-menu-button__label">I'm the label!</span>
</button>
<ul class="mdlext-menu">
<li class="mdlext-menu__item">Menu item #1</li>
<li class="mdlext-menu__item">Menu item #2</li>
<li class="mdlext-menu__item-separator"></li>
<li class="mdlext-menu__item" disabled>Menu item #3</li>
<li class="mdlext-menu__item">Menu item #n</li>
</ul>
</div>`;
const disabled_menu_button_fixture = `
<div role="presentation">
<button class="mdlext-js-menu-button" disabled>
<span class="mdlext-menu-button__label">I'm disabled!</span>
</button>
<ul class="mdlext-menu">
<li class="mdlext-menu__item">Menu item #1</li>
</ul>
</div>`;
const menu_button_with_aria_fixture = `
<button class="mdlext-js-menu-button"
aria-controls="menu-example-dropdown"
role="button"
aria-haspopup="true"
aria-expanded="false">
<span class="mdlext-menu-button__label">I'm the label!</span>
</button>
<ul id="menu-example-dropdown"
class="mdlext-menu"
role="menu"
hidden>
<li class="mdlext-menu__item" role="menuitem">Menu item #1</li>
<li class="mdlext-menu__item" role="menuitem">Menu item #2</li>
<li class="mdlext-menu__item" role="menuitem">Menu item #n</li>
</ul>`;
const menu_button_with_embedded_focusable_element = `
<div role="presentation">
<div class="mdl-textfield mdl-js-textfield mdlext-js-menu-button">
<input class="mdl-textfield__input" type="text" readonly>
<label class="mdl-textfield__label">Text...</label>
</div>
<ul class="mdlext-menu">
<li class="mdlext-menu__item" data-value="twitter">
<span class="mdlext-menu__item__caption">Item #1</span>
</li>
<li class="mdlext-menu__item" data-value="github">
<span class="mdlext-menu__item__caption">Item #2</span>
</li>
<li class="mdlext-menu__item" data-value="github">
<span class="mdlext-menu__item__caption">Item #3</span>
</li>
</ul>
</div>`;
const menu_button_without_focusable_element = `
<div role="presentation">
<div class="mdl-textfield mdl-js-textfield mdlext-js-menu-button">
</div>
<ul class="mdlext-menu">
<li class="mdlext-menu__item" data-value="twitter">
<span class="mdlext-menu__item__caption">Item #1</span>
</li>
<li class="mdlext-menu__item" data-value="github">
<span class="mdlext-menu__item__caption">Item #2</span>
</li>
<li class="mdlext-menu__item" data-value="github">
<span class="mdlext-menu__item__caption">Item #3</span>
</li>
</ul>
</div>`;
const menu_buttons_with_shared_menu = `
<button class="mdl-button mdl-js-button mdlext-js-menu-button" aria-controls="shared-menu">
<span class="mdlext-menu-button__caption">A button</span>
</button>
<div class="mdl-textfield mdl-js-textfield mdlext-js-menu-button" aria-controls="shared-menu">
<input class="mdl-textfield__input" type="text" readonly>
<label class="mdl-textfield__label">A MDL textfield</label>
</div>
<ul id="shared-menu" class="mdlext-menu" hidden>
<li class="mdlext-menu__item" role="menuitem">Menu item #1</li>
<li class="mdlext-menu__item" role="menuitem">Menu item #2</li>
<li class="mdlext-menu__item" role="menuitem">Menu item #n</li>
</ul>`;
const fixture = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>menu Button Fixture</title>
</head>
<body>
<main>
<div class="mdl-layout__content">
<div id="default-fixture">
${menu_button_fixture}
</div>
<div id="aria-fixture">
${menu_button_with_aria_fixture}
</div>
<div id="mount">
</div>
</div>
</main>
</body>
</html>`;
before ( () => {
patchJsDom(fixture);
// Must load MDL after jsdom, see: https://github.com/mochajs/mocha/issues/1722
requireUncached( 'material-design-lite/material');
global.componentHandler = window.componentHandler;
assert.isObject(componentHandler, 'Expected global MDL component handler');
requireUncached('../../src/menu-button/menu-button');
assert.isNotNull(window.MaterialExtMenuButton, 'Expected MaterialExtMenuButton not to be null');
global.MaterialExtMenuButton = window.MaterialExtMenuButton;
});
after ( () => {
jsdomify.destroy();
});
describe('General behaviour', () => {
shouldBehaveLikeAMdlComponent({
componentName: 'MaterialExtMenuButton',
componentCssClass: 'mdlext-js-menu-button',
newComponenrMountNodeSelector: '#mount',
newComponentHtml: menu_button_fixture
});
it('should have public methods available via widget', () => {
const component = document.querySelector(`#default-fixture .${MENU_BUTTON}`);
const methods = [
'openMenu',
'closeMenu',
'getMenuElement',
'getSelectedMenuItem',
'setSelectedMenuItem'
];
methods.forEach( fn => {
expect(component.MaterialExtMenuButton[fn]).to.be.a('function');
});
});
it('should have "is-upgraded" class on menu when upgraded successfully', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_fixture);
const component = container.querySelector(`.${MENU_BUTTON}`);
componentHandler.upgradeElement(component, 'MaterialExtMenuButton');
const menu = document.querySelector(`#${component.getAttribute('aria-controls')}`);
assert.isTrue(menu.classList.contains('is-upgraded'), 'Expected menu element to have class "is-upgraded" after upgrade');
componentHandler.downgradeElements(component);
}
finally {
removeChildElements(container);
}
});
it('should move the menu to document.body when upgraded successfully, then move menu back to original parent after downgrade', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_fixture);
const component = container.querySelector(`.${MENU_BUTTON}`);
const menu = component.parentNode.querySelector(`.${MENU_BUTTON_MENU}`);
assert.isNotNull(menu, 'Expected a menu');
const parentNode = menu.parentNode;
assert.notEqual(parentNode, document.body, 'Did not expect menu to be a child of document.body before upgrade');
componentHandler.upgradeElement(component, 'MaterialExtMenuButton');
assert.equal(menu.parentNode, document.body, 'Expected menu to be a child of document.body after upgrade');
componentHandler.downgradeElements(component);
assert.equal(menu.parentNode, parentNode, 'Expected menu to be moved back to original parent node after downgrade');
}
finally {
removeChildElements(container);
}
});
it('receives a "mdl-componentdowngraded" custom event', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_fixture);
const component = container.querySelector(`.${MENU_BUTTON}`);
componentHandler.upgradeElement(component, 'MaterialExtMenuButton');
const spy = sinon.spy();
component.addEventListener('mdl-componentdowngraded', spy);
componentHandler.downgradeElements(component);
assert.isTrue(spy.calledOnce, 'Expected "mdl-componentdowngraded" event to fire after call to "componentHandler.downgradeElements"');
}
finally {
removeChildElements(container);
}
});
it('should return the menu element controlled by the button', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_fixture);
const component = container.querySelector(`.${MENU_BUTTON}`);
componentHandler.upgradeElement(component, 'MaterialExtMenuButton');
const menu = document.querySelector(`#${component.getAttribute('aria-controls')}`);
const menuReturnedByApi = component.MaterialExtMenuButton.getMenuElement();
assert.equal(menu, menuReturnedByApi, 'Expected menu element returned from api to be equal to queried menu element');
componentHandler.downgradeElements(component);
}
finally {
removeChildElements(container);
}
});
it('should share menu with another button', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_buttons_with_shared_menu);
const [...components] = container.querySelectorAll(`.${MENU_BUTTON}`);
assert.equal(components.length, 2, 'Expected two buttons');
componentHandler.upgradeElement(components[0], 'MaterialExtMenuButton');
componentHandler.upgradeElement(components[1], 'MaterialExtMenuButton');
components.forEach( c =>
assert.isTrue(c.classList.contains('is-upgraded'), `Expected "${MENU_BUTTON}" to have class "is-upgraded"`)
);
assert.equal(components[0].MaterialExtMenuButton.getMenuElement(),
components[1].MaterialExtMenuButton.getMenuElement(), 'Expected buttons to share a menu');
componentHandler.downgradeElements(components);
}
finally {
removeChildElements(container);
}
});
it('should not downgrade menu before last button sharing that menu is downgraded', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_buttons_with_shared_menu);
const components = container.querySelectorAll(`.${MENU_BUTTON}`);
const menu = document.querySelector('#shared-menu');
assert.isNotNull(menu, 'Expected a menu');
assert.equal(components.length, 2, 'Expected two buttons');
componentHandler.upgradeElement(components[0], 'MaterialExtMenuButton');
componentHandler.upgradeElement(components[1], 'MaterialExtMenuButton');
assert.isTrue(menu.classList.contains('is-upgraded'), `Expected menu to have class "is-upgraded"`);
componentHandler.downgradeElements(components[0]);
assert.isTrue(menu.classList.contains('is-upgraded'), `Expected menu to have class "is-upgraded" after downgrading first menu button`);
componentHandler.downgradeElements(components[1]);
assert.isFalse(menu.classList.contains('is-upgraded'), `Expected menu to not have class "is-upgraded" after downgrading second menu button`);
}
finally {
removeChildElements(container);
}
});
});
describe('WAI-ARIA', () => {
it('has appended all the required WAI-ARIA attributes', () => {
const button = document.querySelector(`#default-fixture .${MENU_BUTTON}`);
assert.isNotNull(button, 'Expected menu button not to be null');
const menu = button.MaterialExtMenuButton.getMenuElement();
assert.isNotNull(menu, 'Expected menu button menu not to be null');
assert.equal(button.getAttribute('role'), 'button', 'Expected menu button to have role="button"');
assert.equal(button.getAttribute('aria-haspopup'), 'true', 'Expected menu button to have aria-haspoput="true"');
assert.isTrue(button.hasAttribute('aria-controls'), 'Expected menu button to have attribute "aria-controls"');
assert.equal(button.getAttribute('aria-controls'), menu.id, 'Menu button aria-controls has wrong value');
assert.isTrue(button.hasAttribute('aria-expanded'), 'Expected menu button to have attribute "aria-expanded"');
assert.equal(menu.getAttribute('role'), 'menu', 'Expected menu button menu to have role="menu"');
[...menu.querySelectorAll('.mdlext-menu-button__menu__item')].forEach( menuitem => {
assert.equal(menuitem.getAttribute('role'), 'menuitem', 'Expected menu button menu item to have role="menuitem"');
});
const menuItems = menu.querySelectorAll(`.${MENU_BUTTON_MENU_ITEM}`);
assert.isAtLeast(menuItems.length, 1, 'Expected menu button menu to have at leaset one menu item');
});
it('should have "tabindex=0" if the menu button does not have a focusable element', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_without_focusable_element);
const button = container.querySelector(`.${MENU_BUTTON}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
assert.isTrue(button.hasAttribute('tabindex'), 'Expected menu button button to not have attribute "tabindex"');
}
finally {
removeChildElements(container);
}
});
it('should not have a tabindex if the menu button is a focusable element, or if an embedded element is focusable', () => {
let button = document.querySelector(`#default-fixture .${MENU_BUTTON}`);
assert.isFalse(button.hasAttribute('tabindex'), 'Expected menu button button to not have attribute "tabindex"');
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_with_embedded_focusable_element);
button = container.querySelector(`.${MENU_BUTTON}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
assert.isFalse(button.hasAttribute('tabindex'), 'Expected menu button button to not have attribute "tabindex"');
}
finally {
removeChildElements(container);
}
});
it('should have a menu separator with role="separator"', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_with_disabled_item_fixture);
const button = container.querySelector(`.${MENU_BUTTON}`);
const menu = container.querySelector(`.${MENU_BUTTON_MENU}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
const separatorItem = menu.querySelector(`.${MENU_BUTTON_MENU_ITEM_SEPARATOR}`);
assert.isNotNull(separatorItem, `Expected menu item with class ${MENU_BUTTON_MENU_ITEM_SEPARATOR}`);
assert.equal(separatorItem.getAttribute('role'), 'separator', 'Expected menu item to have role="separator"');
}
finally {
removeChildElements(container);
}
});
});
describe('Button interactions', () => {
let button;
let menu;
beforeEach( () => {
button = document.querySelector(`#default-fixture .${MENU_BUTTON}`);
assert.isNotNull(button, 'Expected menu button menu not to be null');
menu = button.MaterialExtMenuButton.getMenuElement();
assert.isNotNull(menu, 'Expected menu button menu not to be null');
[...menu.querySelectorAll(`.${MENU_BUTTON_MENU_ITEM}[aria-selected="true"]`)]
.forEach(selectedItem => selectedItem.removeAttribute('aria-selected'));
});
it('toggles the menu when button is clicked', () => {
button.MaterialExtMenuButton.closeMenu();
// Trigger click event to toggle menu
dispatchMouseEvent(button, 'click');
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Mouse click when menu is closed: Expected button to have aria-expanded=true');
assert.isFalse(menu.hasAttribute('hidden'), 'Mouse click when menu is closed: Expected menu to not have hidden attribute');
dispatchMouseEvent(button, 'click');
assert.equal(button.getAttribute('aria-expanded'), 'false', 'Mouse click when menu is open: Expected button to have aria-expanded=false');
assert.isTrue(menu.hasAttribute('hidden'), 'Mouse click when menu is closed: Expected menu to have hidden attribute');
});
it('opens the menu when button is clicked and move focus to the first menu item', () => {
button.MaterialExtMenuButton.closeMenu();
// Trigger click event to toggle menu
dispatchMouseEvent(button, 'click');
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Mouse click: Expected button to have aria-expanded=true');
assert.isFalse(menu.hasAttribute('hidden'), 'Mouse click: Expected menu to not have hidden attribute');
assert.equal(menu.firstElementChild, document.activeElement, 'Mouse click: Expected first menu item to have focus');
});
it('opens the menu when button is clicked and move focus to a previously selected menu item', () => {
button.MaterialExtMenuButton.closeMenu();
const selectedItem = menu.children[1];
button.MaterialExtMenuButton.setSelectedMenuItem(selectedItem);
// Trigger click event to toggle menu
dispatchMouseEvent(button, 'click');
const n = button.MaterialExtMenuButton.getSelectedMenuItem();
assert.equal(n, document.activeElement, 'Mouse click: Expected second menu item to have focus');
});
it('opens the menu when Enter or Space key is pressed and move focus to the first menu item', () => {
button.MaterialExtMenuButton.closeMenu();
button.MaterialExtMenuButton.setSelectedMenuItem(null);
dispatchKeyDownEvent(button, VK_SPACE);
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Space key: Expected button to have aria-expanded=true');
assert.isFalse(menu.hasAttribute('hidden'), 'Space key: Expected menu to not have hidden attribute');
assert.equal(menu.firstElementChild, document.activeElement, 'Space key: Expected first menu item to have focus');
button.MaterialExtMenuButton.closeMenu();
button.MaterialExtMenuButton.setSelectedMenuItem(null);
dispatchKeyDownEvent(button, VK_ENTER);
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Enter key: Expected button to have aria-expanded=true');
assert.isFalse(menu.hasAttribute('hidden'), 'Enter key: Expected menu to not have hidden attribute');
assert.equal(menu.firstElementChild, document.activeElement, 'Enter key: Expected first menu item to have focus');
});
it('opens the menu when Enter or Space key is pressed and move focus to the previously selected menu item', () => {
button.MaterialExtMenuButton.closeMenu();
const selectedItem = menu.children[1];
button.MaterialExtMenuButton.setSelectedMenuItem(selectedItem);
dispatchKeyDownEvent(button, VK_SPACE);
let n = button.MaterialExtMenuButton.getSelectedMenuItem();
assert.equal(n, document.activeElement, 'Space key: Expected second menu item to have focus');
button.MaterialExtMenuButton.closeMenu();
dispatchKeyDownEvent(button, VK_ENTER);
n = button.MaterialExtMenuButton.getSelectedMenuItem();
assert.equal(n, document.activeElement, 'Enter key: Expected second menu item to have focus');
});
it('opens the menu and move focus to the last menu item when arrow up key is pressed', () => {
button.MaterialExtMenuButton.closeMenu();
dispatchKeyDownEvent(button, VK_ARROW_UP);
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Arrow up key: Expected button to have aria-expanded=true');
assert.isFalse(menu.hasAttribute('hidden'), 'Arrow up key: Expected menu to not have hidden attribute');
assert.equal(menu.lastElementChild, document.activeElement, 'Arrow up key: Expected last menu item to have focus');
});
it('opens the menu and move focus to the first menu item when arrow down key is pressed', () => {
button.MaterialExtMenuButton.closeMenu();
dispatchKeyDownEvent(button, VK_ARROW_DOWN);
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Arrow down key: Expected button to have aria-expanded=true');
assert.isFalse(menu.hasAttribute('hidden'), 'Arrow down key: Expected menu to not have hidden attribute');
assert.equal(menu.firstElementChild, document.activeElement, 'Arrow down key: Expected first menu item to have focus');
});
it('closes the menu when tab key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
dispatchKeyDownEvent(button, VK_TAB);
assert.equal(button.getAttribute('aria-expanded'), 'false', 'Tab key: Expected button to have aria-expanded=false');
assert.isTrue(menu.hasAttribute('hidden'), 'Tab key: Expected menu to have hidden attribute');
});
it('closes the menu when esc key is pressed', () => {
button.MaterialExtMenuButton.openMenu('first');
dispatchKeyDownEvent(button, VK_ESC);
assert.equal(button.getAttribute('aria-expanded'), 'false', 'ESC key: Expected button to have aria-expanded=false');
assert.isTrue(menu.hasAttribute('hidden'), 'ESC key: Expected menu to have hidden attribute');
});
it('should close an already open menu when another menu button is clicked', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_buttons_with_shared_menu);
const [...buttons] = container.querySelectorAll(`.${MENU_BUTTON}`);
assert.equal(buttons.length, 2, 'Expected two buttons');
componentHandler.upgradeElement(buttons[0], 'MaterialExtMenuButton');
componentHandler.upgradeElement(buttons[1], 'MaterialExtMenuButton');
buttons[0].MaterialExtMenuButton.closeMenu();
buttons[1].MaterialExtMenuButton.closeMenu();
dispatchMouseEvent(buttons[0], 'click');
assert.equal(buttons[0].getAttribute('aria-expanded'), 'true', 'Expected button 1 to have an open menu');
assert.equal(buttons[1].getAttribute('aria-expanded'), 'false', 'Expected button 2 to be closed');
// Does not behave like in a real browser, must close programatically
const m = document.querySelector(`#${buttons[0].getAttribute('aria-controls')}`);
// 1. Reset document.activeElement
m.blur();
// 2. Send an event to call handlers
m.dispatchEvent( new Event( 'blur' ) );
// 3. Programmatically close
buttons[0].MaterialExtMenuButton.closeMenu();
// Then click
dispatchMouseEvent(buttons[1], 'click');
assert.equal(buttons[0].getAttribute('aria-expanded'), 'false', 'Expected button 1 to not have an open menu after clicking button 2');
assert.equal(buttons[1].getAttribute('aria-expanded'), 'true', 'Expected button 2 to be open after clicking that button');
componentHandler.downgradeElements(buttons);
}
finally {
removeChildElements(container);
}
});
it('does nothing when an "undefined" key i pressed', () => {
expect( () => {
dispatchKeyDownEvent(button, VK_PAGE_UP);
}).to.not.throw(Error);
});
it('does nothing when menu button is disabled', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', disabled_menu_button_fixture);
button = container.querySelector(`.${MENU_BUTTON}`);
menu = container.querySelector(`.${MENU_BUTTON_MENU}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
button.MaterialExtMenuButton.openMenu();
assert.equal(menu.hasAttribute('hidden'), true, 'Expected disabled menu button not to open the menu');
dispatchKeyDownEvent(button, VK_ARROW_DOWN);
assert.equal(menu.hasAttribute('hidden'), true, 'Expected disabled menu button not to open the menu');
dispatchKeyDownEvent(button, VK_ARROW_UP);
assert.equal(menu.hasAttribute('hidden'), true, 'Expected disabled menu button not to open the menu');
dispatchKeyDownEvent(button, VK_ENTER);
assert.equal(menu.hasAttribute('hidden'), true, 'Expected disabled menu button not to open the menu');
dispatchKeyDownEvent(button, VK_SPACE);
assert.equal(menu.hasAttribute('hidden'), true, 'Expected disabled menu button not to open the menu');
dispatchMouseEvent(button, 'click');
assert.equal(menu.hasAttribute('hidden'), true, 'Expected disabled menu button not to open the menu');
}
finally {
removeChildElements(container);
}
});
it('re-positions the menu when content scroll', () => {
let realRaf = window.requestAnimationFrame;
let realCaf = window.cancelAnimationFrame;
let mockRaf = createMockRaf();
window.requestAnimationFrame = mockRaf.raf;
window.cancelAnimationFrame = mockRaf.raf.cancel;
let rAFStub = sinon.stub(window, 'requestAnimationFrame', mockRaf.raf);
let clock = sinon.useFakeTimers(Date.now());
const interval = 1000/60;
let elementTop = 0;
let elementLeft = 0;
let getBoundingClientRectStub = sinon.stub(button, 'getBoundingClientRect', () => {
return {
top: elementTop,
left: elementLeft
};
});
try {
const content = document.querySelector('.mdl-layout__content');
content.scrollTop = 0;
content.style.height = '200px';
button.MaterialExtMenuButton.openMenu('first');
const menuTopBeforeScroll = menu.style.top;
elementTop = -100;
content.scrollTop = 100;
content.dispatchEvent(new Event('scroll'));
clock.tick(interval);
mockRaf.step();
assert.notEqual(menuTopBeforeScroll, menu.style.top, 'Expected menu to reposition');
}
finally {
getBoundingClientRectStub.restore();
clock.restore();
rAFStub.restore();
window.requestAnimationFrame = realRaf;
window.cancelAnimationFrame = realCaf;
}
});
});
describe('Menu interactions', () => {
let button;
let menu;
beforeEach( () => {
button = document.querySelector(`#default-fixture .${MENU_BUTTON}`);
assert.isNotNull(button, 'Expected menu button menu not to be null');
menu = button.MaterialExtMenuButton.getMenuElement();
assert.isNotNull(menu, 'Expected menu button menu not to be null');
[...menu.querySelectorAll(`.${MENU_BUTTON_MENU_ITEM}[aria-selected="true"]`)]
.forEach(selectedItem => selectedItem.removeAttribute('aria-selected'));
});
it('closes the menu when tab key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const item = menu.children[1];
dispatchKeyDownEvent(item, VK_TAB);
assert.isTrue(menu.hasAttribute('hidden'), 'Tab key: Expected menu to have hidden attribute');
});
it('closes the menu when ESC key is pressed and moves focus to button', () => {
button.MaterialExtMenuButton.openMenu();
const item = menu.children[0];
dispatchKeyDownEvent(item, VK_ESC);
assert.isTrue(menu.hasAttribute('hidden'), 'ESC key: Expected menu to have hidden attribute');
assert.equal(button, document.activeElement, 'ESC: Expected button to have focus');
});
it('moves focus to previous menu item when Arrow up or Arrow left key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[1];
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ARROW_UP);
assert.equal(menu.children[0], document.activeElement, 'Arrow Up: Expected previous menu item have focus');
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ARROW_LEFT);
assert.equal(menu.children[0], document.activeElement, 'Arrow Left: Expected previous menu item have focus');
});
it('moves focus to next menu item when Arrow down or Arrow right key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[1];
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ARROW_DOWN);
assert.equal(menu.children[2], document.activeElement, 'Arrow Down: Expected next menu item have focus');
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ARROW_RIGHT);
assert.equal(menu.children[2], document.activeElement, 'Arrow Right: Expected next menu item have focus');
});
it('moves focus to first menu item when focus is on last menu item and Arrow down is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[menu.children.length-1];
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ARROW_DOWN);
assert.equal(menu.firstElementChild, document.activeElement, 'Arrow Down: Expected first menu item have focus');
});
it('moves focus to first menu item when Home key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[menu.children.length-1];
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_HOME);
assert.equal(menu.firstElementChild, document.activeElement, 'Home key: Expected first menu item have focus');
});
it('moves focus to last menu item when focus is on first menu item and Arrow up key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.firstElementChild;
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ARROW_UP);
assert.equal(menu.children[menu.children.length-1], document.activeElement, 'Arrow Up: Expected last menu item have focus');
});
it('moves focus to last menu item when End key is pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.firstElementChild;
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_END);
assert.equal(menu.children[menu.children.length-1], document.activeElement, 'End key: Expected last menu item have focus');
});
it('does nothing when an "undefined" key i pressed', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.firstElementChild;
selectedItem.focus();
expect( () => {
dispatchKeyDownEvent(selectedItem, VK_PAGE_UP);
}).to.not.throw(Error);
});
it('closes the menu when Enter or Space key is pressed on a menu item', () => {
button.MaterialExtMenuButton.openMenu();
let selectedItem = menu.children[1];
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_ENTER);
assert.equal(menu.children[1].getAttribute('aria-selected'), 'true', 'Enter key: Expected menu item to have aria-selected="true"');
assert.isTrue(menu.hasAttribute('hidden'), 'ESC key: Expected menu to have hidden attribute');
button.MaterialExtMenuButton.openMenu();
selectedItem = menu.children[0];
selectedItem.focus();
dispatchKeyDownEvent(selectedItem, VK_SPACE);
assert.equal(menu.children[0].getAttribute('aria-selected'), 'true', 'Space key: Expected menu item to have aria-selected="true"');
assert.isTrue(menu.hasAttribute('hidden'), 'ESC key: Expected menu to have hidden attribute');
});
it('closes the menu when menu item is clicked', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[1];
selectedItem.focus();
dispatchMouseEvent(selectedItem, 'click');
assert.equal(menu.children[1].getAttribute('aria-selected'), 'true', 'Mouse cick: Expected menu item to have aria-selected="true"');
assert.isTrue(menu.hasAttribute('hidden'), 'Mouse click: Expected menu to have hidden attribute');
});
it('closes the menu and sets aria-expanded="false" for button and hidden attribute for menu', () => {
button.MaterialExtMenuButton.openMenu('first');
const item = menu.children[1];
item.focus();
assert.equal(button.getAttribute('aria-expanded'), 'true', 'Beforer closing menu: Expected button to have aria-expanded=false');
assert.isFalse(menu.hasAttribute('hidden'), 'Before closing menu: Expected menu to have hidden attribute');
dispatchKeyDownEvent(item, VK_ESC);
assert.equal(button.getAttribute('aria-expanded'), 'false', 'After closing menu: Expected button to have aria-expanded=false');
assert.isTrue(menu.hasAttribute('hidden'), 'After closing menu: Expected menu to have hidden attribute');
});
it('closes the menu when when clicking or touching outside the menu rect', () => {
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[1];
selectedItem.focus();
dispatchTouchEvent(document.documentElement, 'touchstart');
assert.isTrue(menu.hasAttribute('hidden'), 'Mouse down: Expected menu to have hidden attribute after clicking outside menu rect');
});
it('emits a custom select event when a menu item is clicked', () => {
button.MaterialExtMenuButton.setSelectedMenuItem(menu.children[0]);
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[1];
selectedItem.focus();
const spy = sinon.spy();
button.addEventListener('menuselect', spy);
const selectListener = event => {
assert.isDefined(event.detail, 'Expected detail to be defined in event');
assert.isDefined(event.detail.source, 'Expected detail.source to be defined in event');
assert.isTrue(event.detail.source.classList.contains('MENU_BUTTON_MENU_ITEM'), `Expected detail.source to have class "${MENU_BUTTON_MENU_ITEM}"`);
};
button.addEventListener('menuselect', selectListener);
try {
// Trigger click
dispatchMouseEvent(selectedItem, 'click');
const selected = button.MaterialExtMenuButton.getSelectedMenuItem();
assert.equal(selectedItem, selected, 'Expected "button.MaterialExtMenuButton.getSelectedMenuItem()" to return the slected menu item element');
}
finally {
button.removeEventListener('menuselect', spy);
button.removeEventListener('menuselect', selectListener);
}
assert.isTrue(spy.called, 'Expected "select" custom event to fire');
});
it('should not emit a custom select event when a previously selected menu item is clicked', () => {
button.MaterialExtMenuButton.setSelectedMenuItem(menu.children[1]);
button.MaterialExtMenuButton.openMenu();
const selectedItem = menu.children[1];
selectedItem.focus();
const spy = sinon.spy();
button.addEventListener('menuselect', spy);
try {
// Trigger click
dispatchMouseEvent(selectedItem, 'click');
}
finally {
button.removeEventListener('menuselect', spy);
}
assert.isFalse(spy.called, 'Expected "select" custom event NOT to fire for a previously selected menu item');
});
it('should not emit a custom select event when a disabled menu item is clicked', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_with_disabled_item_fixture);
button = container.querySelector(`.${MENU_BUTTON}`);
menu = container.querySelector(`.${MENU_BUTTON_MENU}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
button.MaterialExtMenuButton.openMenu();
const disabledItem = menu.children[3];
disabledItem.focus();
const spy = sinon.spy();
button.addEventListener('menuselect', spy);
try {
dispatchMouseEvent(disabledItem, 'click');
}
finally {
button.removeEventListener('menuselect', spy);
}
assert.isFalse(spy.called, 'Expected "select" custom event NOT to fire for a disabled menu item');
}
finally {
removeChildElements(container);
}
});
it('should not focus a disabled menu item', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_with_disabled_item_fixture);
button = container.querySelector(`.${MENU_BUTTON}`);
menu = container.querySelector(`.${MENU_BUTTON_MENU}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
button.MaterialExtMenuButton.openMenu();
const disabledItem = menu.children[3];
let item = menu.children[1];
item.focus();
dispatchKeyDownEvent(item, VK_ARROW_DOWN);
assert.notEqual(disabledItem, document.activeElement, 'Arrow down: Did not expect a disabled menu item have focus');
item = menu.children[4];
item.focus();
dispatchKeyDownEvent(item, VK_ARROW_UP);
assert.notEqual(disabledItem, document.activeElement, 'Arrow up: Did not expect a disabled menu item have focus');
menu.children[0].setAttribute('disabled', '');
dispatchKeyDownEvent(item, VK_HOME);
assert.notEqual(menu.children[0], document.activeElement, 'Home key: Did not expect a disabled menu item have focus');
menu.children[menu.children.length-1].setAttribute('disabled', '');
dispatchKeyDownEvent(item, VK_END);
assert.notEqual(menu.children[menu.children.length-1], document.activeElement, 'End key: Did not expect a disabled menu item have focus');
// Only one menu item is enabled
item = menu.children[1];
item.focus();
dispatchKeyDownEvent(item, VK_ARROW_DOWN);
assert.equal(item, document.activeElement, 'Arrow down: Expected second menu item to have focus');
// Only one menu item is enabled
item.focus();
dispatchKeyDownEvent(item, VK_ARROW_UP);
assert.equal(item, document.activeElement, 'Arrow up: Expected second menu item to have focus');
}
finally {
removeChildElements(container);
}
});
it('should not focus a menu separator', () => {
const container = document.querySelector('#mount');
try {
container.insertAdjacentHTML('beforeend', menu_button_with_disabled_item_fixture);
button = container.querySelector(`.${MENU_BUTTON}`);
menu = container.querySelector(`.${MENU_BUTTON_MENU}`);
componentHandler.upgradeElement(button, 'MaterialExtMenuButton');
button.MaterialExtMenuButton.openMenu();
const separatorItem = menu.children[2];
let item = menu.children[1];
item.focus();
dispatchKeyDownEvent(item, VK_ARROW_DOWN);
assert.notEqual(separatorItem, document.activeElement, 'Arrow down: Did not expect a menu separator item have focus');
item = menu.children[4];
item.focus();
dispatchKeyDownEvent(item, VK_ARROW_UP);
assert.notEqual(separatorItem, document.activeElement, 'Arrow up: Did not expect a menu separator item have focus');
}
finally {
removeChildElements(container);
}
});
});
function dispatchKeyDownEvent(target, keyCode, shiftKey=false) {
target.dispatchEvent(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
keyCode: keyCode,
shiftKey: shiftKey
})
);
}
function dispatchMouseEvent(target, name) {
target.dispatchEvent(
new MouseEvent(name, {
bubbles: true,
cancelable: true,
view: window
})
);
}
function dispatchTouchEvent(target, name) {
target.dispatchEvent(
new MouseEvent(name, {
bubbles: true,
cancelable: true,
view: window
})
);
}
});