|
| 1 | +import {QueryList, ViewChild, ViewChildren, Component} from '@angular/core'; |
| 2 | +import {CdkMenu} from './menu'; |
| 3 | +import {CdkMenuBar} from './menu-bar'; |
| 4 | +import {ComponentFixture, TestBed, async} from '@angular/core/testing'; |
| 5 | +import {CdkMenuItemTrigger} from './menu-item-trigger'; |
| 6 | +import {MenuStack} from './menu-stack'; |
| 7 | +import {CdkMenuModule} from './menu-module'; |
| 8 | + |
| 9 | +describe('MenuStack', () => { |
| 10 | + let fixture: ComponentFixture<MultiMenuWithSubmenu>; |
| 11 | + let menuStack: MenuStack; |
| 12 | + let triggers: CdkMenuItemTrigger[]; |
| 13 | + let menus: CdkMenu[]; |
| 14 | + |
| 15 | + /** Fetch triggers, menus and the menu stack from the test component. */ |
| 16 | + function getElementsForTesting() { |
| 17 | + fixture.detectChanges(); |
| 18 | + triggers = fixture.componentInstance.triggers.toArray(); |
| 19 | + menus = fixture.componentInstance.menus.toArray(); |
| 20 | + menuStack = fixture.componentInstance.menuBar._menuStack; |
| 21 | + } |
| 22 | + |
| 23 | + beforeEach(async(() => { |
| 24 | + TestBed.configureTestingModule({ |
| 25 | + imports: [CdkMenuModule], |
| 26 | + declarations: [MultiMenuWithSubmenu], |
| 27 | + }).compileComponents(); |
| 28 | + })); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + fixture = TestBed.createComponent(MultiMenuWithSubmenu); |
| 32 | + fixture.detectChanges(); |
| 33 | + |
| 34 | + getElementsForTesting(); |
| 35 | + }); |
| 36 | + |
| 37 | + /** Open up all of the menus in the test component. */ |
| 38 | + function openAllMenus() { |
| 39 | + triggers[0].openMenu(); |
| 40 | + getElementsForTesting(); |
| 41 | + triggers[1].openMenu(); |
| 42 | + getElementsForTesting(); |
| 43 | + triggers[2].openMenu(); |
| 44 | + getElementsForTesting(); |
| 45 | + } |
| 46 | + |
| 47 | + it( |
| 48 | + 'should fill the menu stack with the latest menu at the end of the stack and oldest at' + |
| 49 | + ' the start of the stack', |
| 50 | + () => { |
| 51 | + openAllMenus(); |
| 52 | + expect(menus.length).toBe(3); |
| 53 | + const spy = jasmine.createSpy('menu stack closed spy'); |
| 54 | + |
| 55 | + menuStack.closed.subscribe(spy); |
| 56 | + menuStack.closeAll(); |
| 57 | + |
| 58 | + expect(spy).toHaveBeenCalledTimes(3); |
| 59 | + const callArgs = spy.calls.all().map((v: jasmine.CallInfo<jasmine.Func>) => v.args[0]); |
| 60 | + expect(callArgs).toEqual(menus.reverse()); |
| 61 | + expect(menuStack.isEmpty()).toBeTrue(); |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + it('should close triggering menu and all menus below it', () => { |
| 66 | + openAllMenus(); |
| 67 | + expect(menus.length).toBe(3); |
| 68 | + |
| 69 | + triggers[1].toggle(); |
| 70 | + getElementsForTesting(); |
| 71 | + |
| 72 | + expect(menus.length).toBe(1); |
| 73 | + expect(menuStack.length()).withContext('menu stack should only have the single menu').toBe(1); |
| 74 | + expect(menuStack.peek()).toEqual(menus[0]); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +@Component({ |
| 79 | + template: ` |
| 80 | + <div> |
| 81 | + <div cdkMenuBar id="menu_bar"> |
| 82 | + <button cdkMenuItem [cdkMenuTriggerFor]="file">File</button> |
| 83 | + </div> |
| 84 | +
|
| 85 | + <ng-template cdkMenuPanel #file="cdkMenuPanel"> |
| 86 | + <div cdkMenu id="file_menu" [cdkMenuPanel]="file"> |
| 87 | + <button cdkMenuItem [cdkMenuTriggerFor]="share">Share</button> |
| 88 | + </div> |
| 89 | + </ng-template> |
| 90 | +
|
| 91 | + <ng-template cdkMenuPanel #share="cdkMenuPanel"> |
| 92 | + <div cdkMenu id="share_menu" [cdkMenuPanel]="share"> |
| 93 | + <button cdkMenuItem [cdkMenuTriggerFor]="chat">Chat</button> |
| 94 | + </div> |
| 95 | + </ng-template> |
| 96 | +
|
| 97 | + <ng-template cdkMenuPanel #chat="cdkMenuPanel"> |
| 98 | + <div cdkMenu id="chat_menu" [cdkMenuPanel]="chat"> |
| 99 | + <button cdkMenuItem>GVC</button> |
| 100 | + </div> |
| 101 | + </ng-template> |
| 102 | + </div> |
| 103 | + `, |
| 104 | +}) |
| 105 | +class MultiMenuWithSubmenu { |
| 106 | + @ViewChild(CdkMenuBar) menuBar: CdkMenuBar; |
| 107 | + |
| 108 | + @ViewChildren(CdkMenuItemTrigger) triggers: QueryList<CdkMenuItemTrigger>; |
| 109 | + @ViewChildren(CdkMenu) menus: QueryList<CdkMenu>; |
| 110 | +} |
0 commit comments