Skip to content

Commit 39063f8

Browse files
committed
test(material-experimental/mdc-menu): add missing test coverage
Adds tests that were missing from the `mdc-menu` module.
1 parent 4ef3d3f commit 39063f8

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

scripts/check-mdc-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function getTestNames(files: string[]): string[] {
6464

6565
sourceFile.forEachChild(function walk(node: ts.Node) {
6666
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) &&
67-
node.expression.text === 'it') {
67+
(node.expression.text === 'it' || node.expression.text === 'xit')) {
6868
// Note that this is a little naive since it'll take the literal text of the test
6969
// name expression which could include things like string concatenation. It's fine
7070
// for the limited use cases of the script.

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
QueryList,
1515
Type,
1616
Provider,
17+
ChangeDetectionStrategy,
1718
} from '@angular/core';
1819
import {Direction, Directionality} from '@angular/cdk/bidi';
1920
import {OverlayContainer, Overlay} from '@angular/cdk/overlay';
@@ -243,6 +244,42 @@ describe('MDC-based MatMenu', () => {
243244
expect(backdrop.classList).toContain('custom-backdrop');
244245
}));
245246

247+
248+
it('should be able to set a custom class on the overlay panel', fakeAsync(() => {
249+
const optionsProvider = {
250+
provide: MAT_MENU_DEFAULT_OPTIONS,
251+
useValue: {overlayPanelClass: 'custom-panel-class'}
252+
};
253+
const fixture = createComponent(SimpleMenu, [optionsProvider], [FakeIcon]);
254+
255+
fixture.detectChanges();
256+
fixture.componentInstance.trigger.openMenu();
257+
fixture.detectChanges();
258+
tick(500);
259+
260+
const overlayPane = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-pane');
261+
262+
expect(overlayPane.classList).toContain('custom-panel-class');
263+
}));
264+
265+
it('should be able to set a custom classes on the overlay panel', fakeAsync(() => {
266+
const optionsProvider = {
267+
provide: MAT_MENU_DEFAULT_OPTIONS,
268+
useValue: {overlayPanelClass: ['custom-panel-class-1', 'custom-panel-class-2']}
269+
};
270+
const fixture = createComponent(SimpleMenu, [optionsProvider], [FakeIcon]);
271+
272+
fixture.detectChanges();
273+
fixture.componentInstance.trigger.openMenu();
274+
fixture.detectChanges();
275+
tick(500);
276+
277+
const overlayPane = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-pane');
278+
279+
expect(overlayPane.classList).toContain('custom-panel-class-1');
280+
expect(overlayPane.classList).toContain('custom-panel-class-2');
281+
}));
282+
246283
it('should restore focus to the root trigger when the menu was opened by mouse', fakeAsync(() => {
247284
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
248285
fixture.detectChanges();
@@ -906,6 +943,25 @@ describe('MDC-based MatMenu', () => {
906943
flush();
907944
}));
908945

946+
it('should open submenus when the menu is inside an OnPush component', fakeAsync(() => {
947+
const fixture = createComponent(LazyMenuWithOnPush);
948+
fixture.detectChanges();
949+
950+
// Open the top-level menu
951+
fixture.componentInstance.rootTrigger.nativeElement.click();
952+
fixture.detectChanges();
953+
flush();
954+
955+
// Dispatch a `mouseenter` on the menu item to open the submenu.
956+
// This will only work if the top-level menu is aware the this menu item exists.
957+
dispatchMouseEvent(fixture.componentInstance.menuItemWithSubmenu.nativeElement, 'mouseenter');
958+
fixture.detectChanges();
959+
flush();
960+
961+
expect(overlayContainerElement.querySelectorAll('.mat-mdc-menu-item').length)
962+
.toBe(2, 'Expected two open menus');
963+
}));
964+
909965
it('should focus the menu panel if all items are disabled', fakeAsync(() => {
910966
const fixture = createComponent(SimpleMenuWithRepeater, [], [FakeIcon]);
911967
fixture.componentInstance.items.forEach(item => item.disabled = true);
@@ -2588,6 +2644,30 @@ class SimpleMenuWithRepeaterInLazyContent {
25882644
}
25892645

25902646

2647+
@Component({
2648+
template: `
2649+
<button [matMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
2650+
2651+
<mat-menu #menu="matMenu">
2652+
<ng-template matMenuContent>
2653+
<button [matMenuTriggerFor]="menu2" mat-menu-item #menuItem>Item</button>
2654+
</ng-template>
2655+
</mat-menu>
2656+
2657+
<mat-menu #menu2="matMenu">
2658+
<ng-template matMenuContent>
2659+
<button mat-menu-item #subMenuItem>Sub item</button>
2660+
</ng-template>
2661+
</mat-menu>
2662+
`,
2663+
changeDetection: ChangeDetectionStrategy.OnPush,
2664+
})
2665+
class LazyMenuWithOnPush {
2666+
@ViewChild('triggerEl', {read: ElementRef}) rootTrigger: ElementRef;
2667+
@ViewChild('menuItem', {read: ElementRef}) menuItemWithSubmenu: ElementRef;
2668+
}
2669+
2670+
25912671
@Component({
25922672
template: `
25932673
<mat-menu #menu="matMenu">

0 commit comments

Comments
 (0)