@@ -27,12 +27,15 @@ import domEvents from 'dom-events';
27
27
import td from 'testdouble' ;
28
28
29
29
import { MDCMenuSurface , MDCMenuSurfaceFoundation } from '../../../packages/mdc-menu-surface/index' ;
30
- import { strings , cssClasses , Corner } from '../../../packages/mdc-menu-surface/constants' ;
30
+ import { Corner , cssClasses , strings } from '../../../packages/mdc-menu-surface/constants' ;
31
31
import { getTransformPropertyName } from '../../../packages/mdc-menu-surface/util' ;
32
32
33
- function getFixture ( open ) {
33
+ function getFixture ( open , fixedPosition = false ) {
34
+ const openClass = open ? 'mdc-menu-surface--open' : '' ;
35
+ const fixedClass = fixedPosition ? 'mdc-menu-surface--fixed' : '' ;
36
+
34
37
return bel `
35
- <div class="mdc-menu-surface ${ open ? 'mdc-menu-surface--open' : '' } " tabindex="-1">
38
+ <div class="mdc-menu-surface ${ openClass } ${ fixedClass } " tabindex="-1">
36
39
<ul class="mdc-list" role="menu">
37
40
<li class="mdc-list-item" role="menuitem" tabindex="0">Item</a>
38
41
<li role="separator"></li>
@@ -42,8 +45,8 @@ function getFixture(open) {
42
45
` ;
43
46
}
44
47
45
- function setupTest ( open = false ) {
46
- const root = getFixture ( open ) ;
48
+ function setupTest ( open = false , fixedPosition = false ) {
49
+ const root = getFixture ( open , fixedPosition ) ;
47
50
const MockFoundationConstructor = td . constructor ( MDCMenuSurfaceFoundation ) ;
48
51
const mockFoundation = new MockFoundationConstructor ( ) ;
49
52
const component = new MDCMenuSurface ( root , mockFoundation ) ;
@@ -147,6 +150,11 @@ test('setIsHoisted', () => {
147
150
td . verify ( mockFoundation . setIsHoisted ( true ) ) ;
148
151
} ) ;
149
152
153
+ test ( 'setFixedPosition is called when CSS class is present' , ( ) => {
154
+ const { mockFoundation} = setupTest ( false , true ) ;
155
+ td . verify ( mockFoundation . setFixedPosition ( true ) ) ;
156
+ } ) ;
157
+
150
158
test ( 'setFixedPosition is true' , ( ) => {
151
159
const { root, component, mockFoundation} = setupTest ( ) ;
152
160
component . setFixedPosition ( true ) ;
@@ -174,12 +182,18 @@ test('setAnchorCorner', () => {
174
182
td . verify ( mockFoundation . setAnchorCorner ( Corner . TOP_START ) ) ;
175
183
} ) ;
176
184
177
- test ( 'setAnchorMargin' , ( ) => {
185
+ test ( 'setAnchorMargin with all object properties defined ' , ( ) => {
178
186
const { component, mockFoundation} = setupTest ( ) ;
179
187
component . setAnchorMargin ( { top : 0 , right : 0 , bottom : 0 , left : 0 } ) ;
180
188
td . verify ( mockFoundation . setAnchorMargin ( { top : 0 , right : 0 , bottom : 0 , left : 0 } ) ) ;
181
189
} ) ;
182
190
191
+ test ( 'setAnchorMargin with empty object' , ( ) => {
192
+ const { component, mockFoundation} = setupTest ( ) ;
193
+ component . setAnchorMargin ( { } ) ;
194
+ td . verify ( mockFoundation . setAnchorMargin ( { } ) ) ;
195
+ } ) ;
196
+
183
197
test ( 'setQuickOpen' , ( ) => {
184
198
const { component, mockFoundation} = setupTest ( ) ;
185
199
component . quickOpen = false ;
@@ -362,7 +376,7 @@ test('adapter#getAnchorDimensions returns undefined if there is no anchor elemen
362
376
const { root, component} = setupTest ( true ) ;
363
377
document . body . appendChild ( root ) ;
364
378
component . initialSyncWithDOM ( ) ;
365
- assert . isUndefined ( component . getDefaultFoundation ( ) . adapter_ . getAnchorDimensions ( ) ) ;
379
+ assert . isNull ( component . getDefaultFoundation ( ) . adapter_ . getAnchorDimensions ( ) ) ;
366
380
document . body . removeChild ( root ) ;
367
381
} ) ;
368
382
@@ -466,10 +480,10 @@ test('adapter#setTransformOrigin sets the correct transform origin on the menu s
466
480
467
481
test ( 'adapter#setPosition sets the correct position on the menu surface element' , ( ) => {
468
482
const { root, component} = setupTest ( ) ;
469
- component . getDefaultFoundation ( ) . adapter_ . setPosition ( { top : '10px' , left : '11px' } ) ;
483
+ component . getDefaultFoundation ( ) . adapter_ . setPosition ( { top : 10 , left : 11 } ) ;
470
484
assert . equal ( root . style . top , '10px' ) ;
471
485
assert . equal ( root . style . left , '11px' ) ;
472
- component . getDefaultFoundation ( ) . adapter_ . setPosition ( { bottom : '10px' , right : '11px' } ) ;
486
+ component . getDefaultFoundation ( ) . adapter_ . setPosition ( { bottom : 10 , right : 11 } ) ;
473
487
assert . equal ( root . style . bottom , '10px' ) ;
474
488
assert . equal ( root . style . right , '11px' ) ;
475
489
} ) ;
@@ -479,3 +493,35 @@ test('adapter#setMaxHeight sets the maxHeight style on the menu surface element'
479
493
component . getDefaultFoundation ( ) . adapter_ . setMaxHeight ( '100px' ) ;
480
494
assert . equal ( root . style . maxHeight , '100px' ) ;
481
495
} ) ;
496
+
497
+ test ( 'Pressing Shift+Tab on first element focuses the last menu surface element' , ( ) => {
498
+ const root = getFixture ( true ) ;
499
+ document . body . appendChild ( root ) ;
500
+ const firstItem = root . querySelectorAll ( strings . FOCUSABLE_ELEMENTS ) [ 0 ] ;
501
+ const lastItem = root . querySelectorAll ( strings . FOCUSABLE_ELEMENTS ) [ 1 ] ;
502
+ const component = new MDCMenuSurface ( root ) ;
503
+ component . open = true ;
504
+
505
+ firstItem . focus ( ) ;
506
+ component . getDefaultFoundation ( ) . handleKeydown ( { key : 'Tab' , shiftKey : true , preventDefault : ( ) => { } } ) ;
507
+ assert . equal ( document . activeElement , lastItem ) ;
508
+
509
+ component . open = false ;
510
+ document . body . removeChild ( root ) ;
511
+ } ) ;
512
+
513
+ test ( 'Pressing Tab on last element focuses the first menu surface element' , ( ) => {
514
+ const root = getFixture ( true ) ;
515
+ document . body . appendChild ( root ) ;
516
+ const firstItem = root . querySelectorAll ( strings . FOCUSABLE_ELEMENTS ) [ 0 ] ;
517
+ const lastItem = root . querySelectorAll ( strings . FOCUSABLE_ELEMENTS ) [ 1 ] ;
518
+ const component = new MDCMenuSurface ( root ) ;
519
+ component . open = true ;
520
+
521
+ lastItem . focus ( ) ;
522
+ component . getDefaultFoundation ( ) . handleKeydown ( { key : 'Tab' , shiftKey : false , preventDefault : ( ) => { } } ) ;
523
+ assert . equal ( document . activeElement , firstItem ) ;
524
+
525
+ component . open = false ;
526
+ document . body . removeChild ( root ) ;
527
+ } ) ;
0 commit comments