@@ -7,6 +7,7 @@ import {dispatchMouseEvent} from '@angular/cdk/testing/private';
7
7
import { By } from '@angular/platform-browser' ;
8
8
import { CdkMenuItem } from './menu-item' ;
9
9
import { CdkMenuItemTrigger } from './menu-item-trigger' ;
10
+ import { CdkMenuBar } from './menu-bar' ;
10
11
11
12
describe ( 'CdkContextMenuTrigger' , ( ) => {
12
13
describe ( 'with simple context menu trigger' , ( ) => {
@@ -246,6 +247,110 @@ describe('CdkContextMenuTrigger', () => {
246
247
expect ( instance . copyMenu ) . toBeDefined ( ) ;
247
248
} ) ;
248
249
} ) ;
250
+
251
+ describe ( 'with menubar and inline menu on page' , ( ) => {
252
+ let fixture : ComponentFixture < ContextMenuWithMenuBarAndInlineMenu > ;
253
+ let nativeMenuBar : HTMLElement ;
254
+ let nativeMenuBarTrigger : HTMLElement ;
255
+
256
+ beforeEach ( async ( ( ) => {
257
+ TestBed . configureTestingModule ( {
258
+ imports : [ CdkMenuModule ] ,
259
+ declarations : [ ContextMenuWithMenuBarAndInlineMenu ] ,
260
+ } ) . compileComponents ( ) ;
261
+ } ) ) ;
262
+
263
+ beforeEach ( ( ) => {
264
+ fixture = TestBed . createComponent ( ContextMenuWithMenuBarAndInlineMenu ) ;
265
+ fixture . detectChanges ( ) ;
266
+
267
+ nativeMenuBar = fixture . componentInstance . nativeMenuBar . nativeElement ;
268
+ nativeMenuBarTrigger = fixture . componentInstance . nativeMenuBarTrigger . nativeElement ;
269
+ } ) ;
270
+
271
+ /** Get the menu opened by the context menu trigger. */
272
+ function getContextMenu ( ) {
273
+ return fixture . componentInstance . contextMenu ;
274
+ }
275
+
276
+ /** Get the menu opened by the menu bar item trigger. */
277
+ function getFileMenu ( ) {
278
+ return fixture . componentInstance . fileMenu ;
279
+ }
280
+
281
+ /** Get the context in which the context menu should trigger. */
282
+ function getMenuContext ( ) {
283
+ return fixture . componentInstance . trigger . nativeElement ;
284
+ }
285
+
286
+ /** Get the inline menus trigger element. */
287
+ function getInlineMenuTrigger ( ) {
288
+ return fixture . componentInstance . nativeInlineMenuButton . nativeElement ;
289
+ }
290
+
291
+ /** Return the native element for the inline menu. */
292
+ function getInlineMenuElement ( ) {
293
+ return fixture . componentInstance . nativeInlineMenu . nativeElement ;
294
+ }
295
+
296
+ /** Open up the context menu and run change detection. */
297
+ function openContextMenu ( ) {
298
+ // right click triggers a context menu event
299
+ dispatchMouseEvent ( getMenuContext ( ) , 'contextmenu' ) ;
300
+ dispatchMouseEvent ( getMenuContext ( ) , 'mousedown' ) ;
301
+ fixture . detectChanges ( ) ;
302
+ }
303
+
304
+ /** Open up the file menu from the menu bar. */
305
+ function openFileMenu ( ) {
306
+ nativeMenuBarTrigger . click ( ) ;
307
+ fixture . detectChanges ( ) ;
308
+ }
309
+
310
+ it ( 'should close the open context menu when clicking on the menubar element' , ( ) => {
311
+ openContextMenu ( ) ;
312
+
313
+ dispatchMouseEvent ( nativeMenuBar , 'click' ) ;
314
+ fixture . detectChanges ( ) ;
315
+
316
+ expect ( getContextMenu ( ) ) . not . toBeDefined ( ) ;
317
+ } ) ;
318
+
319
+ it ( 'should close the open context menu when clicking on the menubar menu item' , ( ) => {
320
+ openContextMenu ( ) ;
321
+
322
+ nativeMenuBarTrigger . click ( ) ;
323
+ fixture . detectChanges ( ) ;
324
+
325
+ expect ( getContextMenu ( ) ) . not . toBeDefined ( ) ;
326
+ } ) ;
327
+
328
+ it ( 'should close the open context menu when clicking on the inline menu element' , ( ) => {
329
+ openContextMenu ( ) ;
330
+
331
+ getInlineMenuElement ( ) . click ( ) ;
332
+ fixture . detectChanges ( ) ;
333
+
334
+ expect ( getContextMenu ( ) ) . not . toBeDefined ( ) ;
335
+ } ) ;
336
+
337
+ it ( 'should close the open context menu when clicking on an inline menu item' , ( ) => {
338
+ openContextMenu ( ) ;
339
+
340
+ getInlineMenuTrigger ( ) . click ( ) ;
341
+ fixture . detectChanges ( ) ;
342
+
343
+ expect ( getContextMenu ( ) ) . not . toBeDefined ( ) ;
344
+ } ) ;
345
+
346
+ it ( 'should close the open menu when opening a context menu' , ( ) => {
347
+ openFileMenu ( ) ;
348
+
349
+ openContextMenu ( ) ;
350
+
351
+ expect ( getFileMenu ( ) ) . not . toBeDefined ( ) ;
352
+ } ) ;
353
+ } ) ;
249
354
} ) ;
250
355
251
356
@Component ( {
@@ -317,3 +422,38 @@ class ContextMenuWithSubmenu {
317
422
@ViewChild ( 'cut_menu' , { read : CdkMenu } ) cutMenu : CdkMenu ;
318
423
@ViewChild ( 'copy_menu' , { read : CdkMenu } ) copyMenu : CdkMenu ;
319
424
}
425
+
426
+ @Component ( {
427
+ template : `
428
+ <div cdkMenuBar id="menu_bar">
429
+ <button #trigger cdkMenuItem [cdkMenuTriggerFor]="file">File</button>
430
+ </div>
431
+
432
+ <ng-template cdkMenuPanel #file="cdkMenuPanel">
433
+ <div cdkMenu #file_menu id="file_menu" [cdkMenuPanel]="file"></div>
434
+ </ng-template>
435
+
436
+ <div [cdkContextMenuTriggerFor]="context"></div>
437
+ <ng-template cdkMenuPanel #context="cdkMenuPanel">
438
+ <div cdkMenu #context_menu [cdkMenuPanel]="context">
439
+ <button cdkMenuItem></button>
440
+ </div>
441
+ </ng-template>
442
+
443
+ <div #inline_menu cdkMenu>
444
+ <button #inline_menu_button cdkMenuItem></button>
445
+ </div>
446
+ ` ,
447
+ } )
448
+ class ContextMenuWithMenuBarAndInlineMenu {
449
+ @ViewChild ( CdkMenuBar , { read : ElementRef } ) nativeMenuBar : ElementRef ;
450
+ @ViewChild ( 'trigger' , { read : ElementRef } ) nativeMenuBarTrigger : ElementRef ;
451
+
452
+ @ViewChild ( 'context_menu' ) contextMenu ?: CdkMenu ;
453
+ @ViewChild ( CdkContextMenuTrigger , { read : ElementRef } ) trigger : ElementRef < HTMLElement > ;
454
+
455
+ @ViewChild ( 'file_menu' ) fileMenu ?: CdkMenu ;
456
+
457
+ @ViewChild ( 'inline_menu' ) nativeInlineMenu : ElementRef ;
458
+ @ViewChild ( 'inline_menu_button' ) nativeInlineMenuButton : ElementRef ;
459
+ }
0 commit comments