|
| 1 | +import {TestBed, ComponentFixture, waitForAsync, inject} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatMenuHarness} from '@angular/material/menu/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatMenuModule} from '@angular/material/menu'; |
| 8 | +import {MenuHarnessExample} from './menu-harness-example'; |
| 9 | +import {OverlayContainer} from '@angular/cdk/overlay'; |
| 10 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 11 | + |
| 12 | +describe('MenuHarnessExample', () => { |
| 13 | + let fixture: ComponentFixture<MenuHarnessExample>; |
| 14 | + let loader: HarnessLoader; |
| 15 | + |
| 16 | + beforeAll(() => { |
| 17 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 18 | + }); |
| 19 | + |
| 20 | + beforeEach( |
| 21 | + waitForAsync(() => { |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + imports: [MatMenuModule, NoopAnimationsModule], |
| 24 | + declarations: [MenuHarnessExample] |
| 25 | + }).compileComponents(); |
| 26 | + })); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + fixture = TestBed.createComponent(MenuHarnessExample); |
| 30 | + fixture.detectChanges(); |
| 31 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 32 | + inject([OverlayContainer], () => {})(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should load all menu harnesses', async () => { |
| 36 | + const menues = await loader.getAllHarnesses(MatMenuHarness); |
| 37 | + expect(menues.length).toBe(2); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should get disabled state', async () => { |
| 41 | + const [enabledMenu, disabledMenu] = await loader.getAllHarnesses(MatMenuHarness); |
| 42 | + expect(await enabledMenu.isDisabled()).toBe(false); |
| 43 | + expect(await disabledMenu.isDisabled()).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should get menu text', async () => { |
| 47 | + const [firstMenu, secondMenu] = await loader.getAllHarnesses(MatMenuHarness); |
| 48 | + expect(await firstMenu.getTriggerText()).toBe('Settings'); |
| 49 | + expect(await secondMenu.getTriggerText()).toBe('Disabled menu'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should open and close', async () => { |
| 53 | + const menu = await loader.getHarness(MatMenuHarness.with({triggerText: 'Settings'})); |
| 54 | + expect(await menu.isOpen()).toBe(false); |
| 55 | + await menu.open(); |
| 56 | + expect(await menu.isOpen()).toBe(true); |
| 57 | + await menu.close(); |
| 58 | + expect(await menu.isOpen()).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should get all items', async () => { |
| 62 | + const menu = await loader.getHarness(MatMenuHarness.with({triggerText: 'Settings'})); |
| 63 | + await menu.open(); |
| 64 | + expect((await menu.getItems()).length).toBe(2); |
| 65 | + }); |
| 66 | +}); |
0 commit comments