|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Component, TemplateRef, ViewChild} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {MatDialog, MatDialogConfig, MatDialogModule} from '@angular/material/dialog'; |
| 6 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 7 | +import {MatDialogHarness} from './dialog-harness'; |
| 8 | + |
| 9 | +let fixture: ComponentFixture<DialogHarnessTest>; |
| 10 | +let loader: HarnessLoader; |
| 11 | +let dialogHarness: typeof MatDialogHarness; |
| 12 | + |
| 13 | +describe('MatDialogHarness', () => { |
| 14 | + describe('non-MDC-based', () => { |
| 15 | + beforeEach(async () => { |
| 16 | + await TestBed |
| 17 | + .configureTestingModule({ |
| 18 | + imports: [MatDialogModule, NoopAnimationsModule], |
| 19 | + declarations: [DialogHarnessTest], |
| 20 | + }) |
| 21 | + .compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(DialogHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = new TestbedHarnessEnvironment(document.body, fixture); |
| 26 | + dialogHarness = MatDialogHarness; |
| 27 | + }); |
| 28 | + |
| 29 | + runTests(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe( |
| 33 | + 'MDC-based', |
| 34 | + () => { |
| 35 | + // TODO: run tests for MDC based radio-button once implemented. |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +/** Shared tests to run on both the original and MDC-based radio-button's. */ |
| 40 | +function runTests() { |
| 41 | + it('should load harness for dialog', async () => { |
| 42 | + fixture.componentInstance.open(); |
| 43 | + const dialogs = await loader.getAllHarnesses(dialogHarness); |
| 44 | + expect(dialogs.length).toBe(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should load harness for dialog with specific id', async () => { |
| 48 | + fixture.componentInstance.open({id: 'my-dialog'}); |
| 49 | + fixture.componentInstance.open({id: 'other'}); |
| 50 | + let dialogs = await loader.getAllHarnesses(dialogHarness); |
| 51 | + expect(dialogs.length).toBe(2); |
| 52 | + |
| 53 | + dialogs = await loader.getAllHarnesses(dialogHarness.with({id: 'my-dialog'})); |
| 54 | + expect(dialogs.length).toBe(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should be able to get id of dialog', async () => { |
| 58 | + fixture.componentInstance.open({id: 'my-dialog'}); |
| 59 | + fixture.componentInstance.open({id: 'other'}); |
| 60 | + const dialogs = await loader.getAllHarnesses(dialogHarness); |
| 61 | + expect(await dialogs[0].getId()).toBe('my-dialog'); |
| 62 | + expect(await dialogs[1].getId()).toBe('other'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should be able to get role of dialog', async () => { |
| 66 | + fixture.componentInstance.open({role: 'alertdialog'}); |
| 67 | + fixture.componentInstance.open({role: 'dialog'}); |
| 68 | + fixture.componentInstance.open({role: undefined}); |
| 69 | + const dialogs = await loader.getAllHarnesses(dialogHarness); |
| 70 | + expect(await dialogs[0].getRole()).toBe('alertdialog'); |
| 71 | + expect(await dialogs[1].getRole()).toBe('dialog'); |
| 72 | + expect(await dialogs[2].getRole()).toBe(null); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should be able to get aria-label of dialog', async () => { |
| 76 | + fixture.componentInstance.open(); |
| 77 | + fixture.componentInstance.open({ariaLabel: 'Confirm purchase.'}); |
| 78 | + const dialogs = await loader.getAllHarnesses(dialogHarness); |
| 79 | + expect(await dialogs[0].getAriaLabel()).toBe(null); |
| 80 | + expect(await dialogs[1].getAriaLabel()).toBe('Confirm purchase.'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should be able to get aria-labelledby of dialog', async () => { |
| 84 | + fixture.componentInstance.open(); |
| 85 | + fixture.componentInstance.open({ariaLabelledBy: 'dialog-label'}); |
| 86 | + const dialogs = await loader.getAllHarnesses(dialogHarness); |
| 87 | + expect(await dialogs[0].getAriaLabelledby()).toBe(null); |
| 88 | + expect(await dialogs[1].getAriaLabelledby()).toBe('dialog-label'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should be able to get aria-describedby of dialog', async () => { |
| 92 | + fixture.componentInstance.open(); |
| 93 | + fixture.componentInstance.open({ariaDescribedBy: 'dialog-description'}); |
| 94 | + const dialogs = await loader.getAllHarnesses(dialogHarness); |
| 95 | + expect(await dialogs[0].getAriaDescribedby()).toBe(null); |
| 96 | + expect(await dialogs[1].getAriaDescribedby()).toBe('dialog-description'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should be able to close dialog', async () => { |
| 100 | + fixture.componentInstance.open({disableClose: true}); |
| 101 | + fixture.componentInstance.open(); |
| 102 | + let dialogs = await loader.getAllHarnesses(dialogHarness); |
| 103 | + |
| 104 | + expect(dialogs.length).toBe(2); |
| 105 | + await dialogs[0].close(); |
| 106 | + |
| 107 | + dialogs = await loader.getAllHarnesses(dialogHarness); |
| 108 | + expect(dialogs.length).toBe(1); |
| 109 | + |
| 110 | + // should be a noop since "disableClose" is set to "true". |
| 111 | + await dialogs[0].close(); |
| 112 | + dialogs = await loader.getAllHarnesses(dialogHarness); |
| 113 | + expect(dialogs.length).toBe(1); |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +@Component({ |
| 118 | + template: ` |
| 119 | + <ng-template> |
| 120 | + Hello from the dialog! |
| 121 | + </ng-template> |
| 122 | + ` |
| 123 | +}) |
| 124 | +class DialogHarnessTest { |
| 125 | + @ViewChild(TemplateRef, {static: false}) dialogTmpl: TemplateRef<any>; |
| 126 | + |
| 127 | + constructor(readonly dialog: MatDialog) {} |
| 128 | + |
| 129 | + open(config?: MatDialogConfig) { |
| 130 | + return this.dialog.open(this.dialogTmpl, config); |
| 131 | + } |
| 132 | +} |
0 commit comments