Skip to content

docs(material/menu): add harness example for menu #21451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/components-examples/material/menu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ ng_module(
]),
module_name = "@angular/components-examples/material/menu",
deps = [
"//src/cdk/overlay",
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/button",
"//src/material/icon",
"//src/material/menu",
"//src/material/menu/testing",
"@npm//@angular/platform-browser",
"@npm//@angular/platform-browser-dynamic",
"@npm//@types/jasmine",
],
)

Expand Down
3 changes: 3 additions & 0 deletions src/components-examples/material/menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import {MenuIconsExample} from './menu-icons/menu-icons-example';
import {MenuOverviewExample} from './menu-overview/menu-overview-example';
import {MenuPositionExample} from './menu-position/menu-position-example';
import {MenuNestedExample} from './menu-nested/menu-nested-example';
import {MenuHarnessExample} from './menu-harness/menu-harness-example';

export {
MenuHarnessExample,
MenuIconsExample,
MenuOverviewExample,
MenuPositionExample,
MenuNestedExample,
};

const EXAMPLES = [
MenuHarnessExample,
MenuIconsExample,
MenuOverviewExample,
MenuPositionExample,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<button type="button" [matMenuTriggerFor]="settingsMenu">Settings</button>
<button type="button" disabled [matMenuTriggerFor]="settingsMenu">Disabled menu</button>

<mat-menu #settingsMenu>
<menu mat-menu-item>Profile</menu>
<menu mat-menu-item>Account</menu>
</mat-menu>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {TestBed, ComponentFixture, waitForAsync, inject} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatMenuHarness} from '@angular/material/menu/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
from '@angular/platform-browser-dynamic/testing';
import {MatMenuModule} from '@angular/material/menu';
import {MenuHarnessExample} from './menu-harness-example';
import {OverlayContainer} from '@angular/cdk/overlay';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';

describe('MenuHarnessExample', () => {
let fixture: ComponentFixture<MenuHarnessExample>;
let loader: HarnessLoader;

beforeAll(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
});

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatMenuModule, NoopAnimationsModule],
declarations: [MenuHarnessExample]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(MenuHarnessExample);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
inject([OverlayContainer], () => {})();
});

it('should load all menu harnesses', async () => {
const menues = await loader.getAllHarnesses(MatMenuHarness);
expect(menues.length).toBe(2);
});

it('should get disabled state', async () => {
const [enabledMenu, disabledMenu] = await loader.getAllHarnesses(MatMenuHarness);
expect(await enabledMenu.isDisabled()).toBe(false);
expect(await disabledMenu.isDisabled()).toBe(true);
});

it('should get menu text', async () => {
const [firstMenu, secondMenu] = await loader.getAllHarnesses(MatMenuHarness);
expect(await firstMenu.getTriggerText()).toBe('Settings');
expect(await secondMenu.getTriggerText()).toBe('Disabled menu');
});

it('should open and close', async () => {
const menu = await loader.getHarness(MatMenuHarness.with({triggerText: 'Settings'}));
expect(await menu.isOpen()).toBe(false);
await menu.open();
expect(await menu.isOpen()).toBe(true);
await menu.close();
expect(await menu.isOpen()).toBe(false);
});

it('should get all items', async () => {
const menu = await loader.getHarness(MatMenuHarness.with({triggerText: 'Settings'}));
await menu.open();
expect((await menu.getItems()).length).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Component} from '@angular/core';

/**
* @title Testing with MatMenuHarness
*/
@Component({
selector: 'menu-harness-example',
templateUrl: 'menu-harness-example.html',
})
export class MenuHarnessExample {}