|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatDialogHarness} from '@angular/material/dialog/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatDialogModule} from '@angular/material/dialog'; |
| 8 | +import {DialogHarnessExample} from './dialog-harness-example'; |
| 9 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 10 | + |
| 11 | +describe('DialogHarnessExample', () => { |
| 12 | + let fixture: ComponentFixture<DialogHarnessExample>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach( |
| 20 | + waitForAsync(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + imports: [MatDialogModule, NoopAnimationsModule], |
| 23 | + declarations: [DialogHarnessExample] |
| 24 | + }).compileComponents(); |
| 25 | + })); |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + fixture = TestBed.createComponent(DialogHarnessExample); |
| 29 | + fixture.detectChanges(); |
| 30 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should load harness for dialog', async () => { |
| 34 | + fixture.componentInstance.open(); |
| 35 | + const dialogs = await loader.getAllHarnesses(MatDialogHarness); |
| 36 | + expect(dialogs.length).toBe(1); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should load harness for dialog with specific id', async () => { |
| 40 | + fixture.componentInstance.open({id: 'my-dialog'}); |
| 41 | + fixture.componentInstance.open({id: 'other'}); |
| 42 | + let dialogs = await loader.getAllHarnesses(MatDialogHarness); |
| 43 | + expect(dialogs.length).toBe(2); |
| 44 | + |
| 45 | + dialogs = await loader.getAllHarnesses(MatDialogHarness.with({selector: '#my-dialog'})); |
| 46 | + expect(dialogs.length).toBe(1); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should be able to get role of dialog', async () => { |
| 50 | + fixture.componentInstance.open({role: 'alertdialog'}); |
| 51 | + fixture.componentInstance.open({role: 'dialog'}); |
| 52 | + const dialogs = await loader.getAllHarnesses(MatDialogHarness); |
| 53 | + expect(await dialogs[0].getRole()).toBe('alertdialog'); |
| 54 | + expect(await dialogs[1].getRole()).toBe('dialog'); |
| 55 | + }); |
| 56 | + |
| 57 | + |
| 58 | + it('should be able to close dialog', async () => { |
| 59 | + fixture.componentInstance.open({disableClose: true}); |
| 60 | + fixture.componentInstance.open(); |
| 61 | + let dialogs = await loader.getAllHarnesses(MatDialogHarness); |
| 62 | + |
| 63 | + expect(dialogs.length).toBe(2); |
| 64 | + await dialogs[0].close(); |
| 65 | + |
| 66 | + dialogs = await loader.getAllHarnesses(MatDialogHarness); |
| 67 | + expect(dialogs.length).toBe(1); |
| 68 | + |
| 69 | + // should be a noop since "disableClose" is set to "true". |
| 70 | + await dialogs[0].close(); |
| 71 | + dialogs = await loader.getAllHarnesses(MatDialogHarness); |
| 72 | + expect(dialogs.length).toBe(1); |
| 73 | + }); |
| 74 | +}); |
0 commit comments