Skip to content

Commit 3d92f91

Browse files
authored
docs(material/dialog): add harness example for dialog (#21474)
1 parent ddc2e23 commit 3d92f91

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

src/components-examples/material/dialog/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/dialog",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
1517
"//src/material/dialog",
18+
"//src/material/dialog/testing",
1619
"//src/material/input",
1720
"//src/material/menu",
1821
"@npm//@angular/forms",
22+
"@npm//@angular/platform-browser",
23+
"@npm//@angular/platform-browser-dynamic",
24+
"@npm//@types/jasmine",
1925
],
2026
)
2127

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ng-template>
2+
Hello from the dialog!
3+
</ng-template>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Component, TemplateRef, ViewChild} from '@angular/core';
2+
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
3+
4+
/**
5+
* @title Testing with MatDialogHarness
6+
*/
7+
@Component({
8+
selector: 'dialog-harness-example',
9+
templateUrl: 'dialog-harness-example.html',
10+
})
11+
export class DialogHarnessExample {
12+
@ViewChild(TemplateRef) dialogTemplate: TemplateRef<any>;
13+
14+
constructor(readonly dialog: MatDialog) {}
15+
16+
open(config?: MatDialogConfig) {
17+
return this.dialog.open(this.dialogTemplate, config);
18+
}
19+
}

src/components-examples/material/dialog/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
DialogFromMenuExample,
2323
DialogFromMenuExampleDialog
2424
} from './dialog-from-menu/dialog-from-menu-example';
25+
import {DialogHarnessExample} from './dialog-harness/dialog-harness-example';
2526

2627
export {
2728
DialogContentExample,
@@ -32,6 +33,7 @@ export {
3233
DialogElementsExampleDialog,
3334
DialogFromMenuExample,
3435
DialogFromMenuExampleDialog,
36+
DialogHarnessExample,
3537
DialogOverviewExample,
3638
DialogOverviewExampleDialog,
3739
};
@@ -45,6 +47,7 @@ const EXAMPLES = [
4547
DialogElementsExampleDialog,
4648
DialogFromMenuExample,
4749
DialogFromMenuExampleDialog,
50+
DialogHarnessExample,
4851
DialogOverviewExample,
4952
DialogOverviewExampleDialog,
5053
];

0 commit comments

Comments
 (0)