Skip to content

Commit 5bb45d6

Browse files
committed
fixup! feat(material/dialog): Switch dialog implementation to use MDC
1 parent 82925e2 commit 5bb45d6

File tree

12 files changed

+205
-200
lines changed

12 files changed

+205
-200
lines changed

src/material/dialog/testing/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ ts_library(
1212
"//src/cdk/overlay",
1313
"//src/cdk/testing",
1414
"//src/material/dialog",
15-
"//src/material/legacy-dialog/testing",
1615
"@npm//@angular/core",
1716
"@npm//@angular/platform-browser",
17+
"@npm//rxjs",
1818
],
1919
)
2020

src/material/legacy-dialog/testing/dialog-harness-filters.ts renamed to src/material/dialog/testing/dialog-harness-filters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
import {BaseHarnessFilters} from '@angular/cdk/testing';
1010

1111
/** A set of criteria that can be used to filter a list of `MatDialogHarness` instances. */
12-
export interface LegacyDialogHarnessFilters extends BaseHarnessFilters {}
12+
export interface DialogHarnessFilters extends BaseHarnessFilters {}

src/material/dialog/testing/dialog-harness.ts

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarnessConstructor, HarnessPredicate} from '@angular/cdk/testing';
109
import {
11-
LegacyDialogHarnessFilters,
12-
_MatLegacyDialogHarnessBase,
13-
} from '@angular/material/legacy-dialog/testing';
10+
ComponentHarnessConstructor,
11+
ContentContainerComponentHarness,
12+
HarnessPredicate,
13+
TestKey,
14+
} from '@angular/cdk/testing';
15+
import {DialogHarnessFilters} from './dialog-harness-filters';
16+
import {DialogRole} from '@angular/material/dialog';
1417

1518
/** Selectors for different sections of the mat-dialog that can contain user content. */
1619
export const enum MatDialogSection {
@@ -19,8 +22,75 @@ export const enum MatDialogSection {
1922
ACTIONS = '.mat-mdc-dialog-actions',
2023
}
2124

25+
/** Base class for the `MatDialogHarness` implementation. */
26+
export class _MatDialogHarnessBase
27+
// @breaking-change 14.0.0 change generic type to MatDialogSection.
28+
extends ContentContainerComponentHarness<MatDialogSection | string>
29+
{
30+
protected _title = this.locatorForOptional(MatDialogSection.TITLE);
31+
protected _content = this.locatorForOptional(MatDialogSection.CONTENT);
32+
protected _actions = this.locatorForOptional(MatDialogSection.ACTIONS);
33+
34+
/** Gets the id of the dialog. */
35+
async getId(): Promise<string | null> {
36+
const id = await (await this.host()).getAttribute('id');
37+
// In case no id has been specified, the "id" property always returns
38+
// an empty string. To make this method more explicit, we return null.
39+
return id !== '' ? id : null;
40+
}
41+
42+
/** Gets the role of the dialog. */
43+
async getRole(): Promise<DialogRole | null> {
44+
return (await this.host()).getAttribute('role') as Promise<DialogRole | null>;
45+
}
46+
47+
/** Gets the value of the dialog's "aria-label" attribute. */
48+
async getAriaLabel(): Promise<string | null> {
49+
return (await this.host()).getAttribute('aria-label');
50+
}
51+
52+
/** Gets the value of the dialog's "aria-labelledby" attribute. */
53+
async getAriaLabelledby(): Promise<string | null> {
54+
return (await this.host()).getAttribute('aria-labelledby');
55+
}
56+
57+
/** Gets the value of the dialog's "aria-describedby" attribute. */
58+
async getAriaDescribedby(): Promise<string | null> {
59+
return (await this.host()).getAttribute('aria-describedby');
60+
}
61+
62+
/**
63+
* Closes the dialog by pressing escape.
64+
*
65+
* Note: this method does nothing if `disableClose` has been set to `true` for the dialog.
66+
*/
67+
async close(): Promise<void> {
68+
await (await this.host()).sendKeys(TestKey.ESCAPE);
69+
}
70+
71+
/** Gets te dialog's text. */
72+
async getText() {
73+
return (await this.host()).text();
74+
}
75+
76+
/** Gets the dialog's title text. This only works if the dialog is using mat-dialog-title. */
77+
async getTitleText() {
78+
return (await this._title())?.text() ?? '';
79+
}
80+
81+
/** Gets the dialog's content text. This only works if the dialog is using mat-dialog-content. */
82+
async getContentText() {
83+
return (await this._content())?.text() ?? '';
84+
}
85+
86+
/** Gets the dialog's actions text. This only works if the dialog is using mat-dialog-actions. */
87+
async getActionsText() {
88+
return (await this._actions())?.text() ?? '';
89+
}
90+
}
91+
2292
/** Harness for interacting with a standard `MatDialog` in tests. */
23-
export class MatDialogHarness extends _MatLegacyDialogHarnessBase {
93+
export class MatDialogHarness extends _MatDialogHarnessBase {
2494
/** The selector for the host element of a `MatDialog` instance. */
2595
static hostSelector = '.mat-mdc-dialog-container';
2696

@@ -31,12 +101,8 @@ export class MatDialogHarness extends _MatLegacyDialogHarnessBase {
31101
*/
32102
static with<T extends MatDialogHarness>(
33103
this: ComponentHarnessConstructor<T>,
34-
options: LegacyDialogHarnessFilters = {},
104+
options: DialogHarnessFilters = {},
35105
): HarnessPredicate<T> {
36106
return new HarnessPredicate(this, options);
37107
}
38-
39-
protected override _title = this.locatorForOptional(MatDialogSection.TITLE);
40-
protected override _content = this.locatorForOptional(MatDialogSection.CONTENT);
41-
protected override _actions = this.locatorForOptional(MatDialogSection.ACTIONS);
42108
}

src/material/dialog/testing/dialog-opener.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,65 @@
77
*/
88

99
import {ComponentType} from '@angular/cdk/overlay';
10-
import {ChangeDetectionStrategy, Component, NgModule, ViewEncapsulation} from '@angular/core';
11-
import {_MatTestLegacyDialogOpenerBase} from '@angular/material/legacy-dialog/testing';
10+
import {
11+
ChangeDetectionStrategy,
12+
Component,
13+
Directive,
14+
NgModule,
15+
OnDestroy,
16+
ViewEncapsulation,
17+
} from '@angular/core';
1218
import {
1319
MatDialog,
20+
MatDialogConfig,
1421
MatDialogContainer,
1522
MatDialogModule,
16-
MatDialogConfig,
23+
_MatDialogBase,
24+
_MatDialogContainerBase,
25+
MatDialogRef,
1726
} from '@angular/material/dialog';
1827
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
28+
import {Subscription} from 'rxjs';
29+
30+
/** Base class for a component that immediately opens a dialog when created. */
31+
@Directive()
32+
export class _MatTestDialogOpenerBase<C extends _MatDialogContainerBase, T, R>
33+
implements OnDestroy
34+
{
35+
/** Component that should be opened with the MatDialog `open` method. */
36+
protected static component: ComponentType<unknown> | undefined;
37+
38+
/** Config that should be provided to the MatDialog `open` method. */
39+
protected static config: MatDialogConfig | undefined;
40+
41+
/** MatDialogRef returned from the MatDialog `open` method. */
42+
dialogRef: MatDialogRef<T, R>;
43+
44+
/** Data passed to the `MatDialog` close method. */
45+
closedResult: R | undefined;
46+
47+
private readonly _afterClosedSubscription: Subscription;
48+
49+
constructor(public dialog: _MatDialogBase<C>) {
50+
if (!_MatTestDialogOpenerBase.component) {
51+
throw new Error(`MatTestDialogOpener does not have a component provided.`);
52+
}
53+
54+
this.dialogRef = this.dialog.open<T, R>(
55+
_MatTestDialogOpenerBase.component as ComponentType<T>,
56+
_MatTestDialogOpenerBase.config || {},
57+
);
58+
this._afterClosedSubscription = this.dialogRef.afterClosed().subscribe(result => {
59+
this.closedResult = result;
60+
});
61+
}
62+
63+
ngOnDestroy() {
64+
this._afterClosedSubscription.unsubscribe();
65+
_MatTestDialogOpenerBase.component = undefined;
66+
_MatTestDialogOpenerBase.config = undefined;
67+
}
68+
}
1969

2070
/** Test component that immediately opens a dialog when bootstrapped. */
2171
@Component({
@@ -24,7 +74,7 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
2474
changeDetection: ChangeDetectionStrategy.OnPush,
2575
encapsulation: ViewEncapsulation.None,
2676
})
27-
export class MatTestDialogOpener<T = unknown, R = unknown> extends _MatTestLegacyDialogOpenerBase<
77+
export class MatTestDialogOpener<T = unknown, R = unknown> extends _MatTestDialogOpenerBase<
2878
MatDialogContainer,
2979
T,
3080
R
@@ -38,8 +88,8 @@ export class MatTestDialogOpener<T = unknown, R = unknown> extends _MatTestLegac
3888
component: ComponentType<T>,
3989
config?: MatDialogConfig,
4090
) {
41-
_MatTestLegacyDialogOpenerBase.component = component;
42-
_MatTestLegacyDialogOpenerBase.config = config;
91+
_MatTestDialogOpenerBase.component = component;
92+
_MatTestDialogOpenerBase.config = config;
4393
return MatTestDialogOpener as ComponentType<MatTestDialogOpener<T, R>>;
4494
}
4595
}

src/material/dialog/testing/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
*/
88

99
export * from './public-api';
10+
export {_MatDialogHarnessBase} from './dialog-harness';

src/material/dialog/testing/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export {LegacyDialogHarnessFilters} from '@angular/material/legacy-dialog/testing';
9+
export {DialogHarnessFilters} from './dialog-harness-filters';
1010
export {MatDialogHarness, MatDialogSection} from './dialog-harness';
1111
export * from './dialog-opener';

src/material/legacy-dialog/testing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ts_library(
1212
"//src/cdk/coercion",
1313
"//src/cdk/overlay",
1414
"//src/cdk/testing",
15+
"//src/material/dialog/testing",
1516
"//src/material/legacy-dialog",
1617
"@npm//@angular/core",
1718
"@npm//@angular/platform-browser",

src/material/legacy-dialog/testing/dialog-harness.ts

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ContentContainerComponentHarness, HarnessPredicate, TestKey} from '@angular/cdk/testing';
10-
import {LegacyDialogRole} from '@angular/material/legacy-dialog';
11-
import {LegacyDialogHarnessFilters} from './dialog-harness-filters';
9+
import {HarnessPredicate} from '@angular/cdk/testing';
10+
import {_MatDialogHarnessBase, DialogHarnessFilters} from '@angular/material/dialog/testing';
1211

1312
/** Selectors for different sections of the mat-dialog that can contain user content. */
1413
export const enum MatLegacyDialogSection {
@@ -17,75 +16,8 @@ export const enum MatLegacyDialogSection {
1716
ACTIONS = '.mat-dialog-actions',
1817
}
1918

20-
/** Base class for the `MatDialogHarness` implementation. */
21-
export class _MatLegacyDialogHarnessBase
22-
// @breaking-change 14.0.0 change generic type to MatDialogSection.
23-
extends ContentContainerComponentHarness<MatLegacyDialogSection | string>
24-
{
25-
protected _title = this.locatorForOptional(MatLegacyDialogSection.TITLE);
26-
protected _content = this.locatorForOptional(MatLegacyDialogSection.CONTENT);
27-
protected _actions = this.locatorForOptional(MatLegacyDialogSection.ACTIONS);
28-
29-
/** Gets the id of the dialog. */
30-
async getId(): Promise<string | null> {
31-
const id = await (await this.host()).getAttribute('id');
32-
// In case no id has been specified, the "id" property always returns
33-
// an empty string. To make this method more explicit, we return null.
34-
return id !== '' ? id : null;
35-
}
36-
37-
/** Gets the role of the dialog. */
38-
async getRole(): Promise<LegacyDialogRole | null> {
39-
return (await this.host()).getAttribute('role') as Promise<LegacyDialogRole | null>;
40-
}
41-
42-
/** Gets the value of the dialog's "aria-label" attribute. */
43-
async getAriaLabel(): Promise<string | null> {
44-
return (await this.host()).getAttribute('aria-label');
45-
}
46-
47-
/** Gets the value of the dialog's "aria-labelledby" attribute. */
48-
async getAriaLabelledby(): Promise<string | null> {
49-
return (await this.host()).getAttribute('aria-labelledby');
50-
}
51-
52-
/** Gets the value of the dialog's "aria-describedby" attribute. */
53-
async getAriaDescribedby(): Promise<string | null> {
54-
return (await this.host()).getAttribute('aria-describedby');
55-
}
56-
57-
/**
58-
* Closes the dialog by pressing escape.
59-
*
60-
* Note: this method does nothing if `disableClose` has been set to `true` for the dialog.
61-
*/
62-
async close(): Promise<void> {
63-
await (await this.host()).sendKeys(TestKey.ESCAPE);
64-
}
65-
66-
/** Gets te dialog's text. */
67-
async getText() {
68-
return (await this.host()).text();
69-
}
70-
71-
/** Gets the dialog's title text. This only works if the dialog is using mat-dialog-title. */
72-
async getTitleText() {
73-
return (await this._title())?.text() ?? '';
74-
}
75-
76-
/** Gets the dialog's content text. This only works if the dialog is using mat-dialog-content. */
77-
async getContentText() {
78-
return (await this._content())?.text() ?? '';
79-
}
80-
81-
/** Gets the dialog's actions text. This only works if the dialog is using mat-dialog-actions. */
82-
async getActionsText() {
83-
return (await this._actions())?.text() ?? '';
84-
}
85-
}
86-
8719
/** Harness for interacting with a standard `MatDialog` in tests. */
88-
export class MatLegacyDialogHarness extends _MatLegacyDialogHarnessBase {
20+
export class MatLegacyDialogHarness extends _MatDialogHarnessBase {
8921
// Developers can provide a custom component or template for the
9022
// dialog. The canonical dialog parent is the "MatDialogContainer".
9123
/** The selector for the host element of a `MatDialog` instance. */
@@ -97,7 +29,11 @@ export class MatLegacyDialogHarness extends _MatLegacyDialogHarnessBase {
9729
* @param options Options for filtering which dialog instances are considered a match.
9830
* @return a `HarnessPredicate` configured with the given options.
9931
*/
100-
static with(options: LegacyDialogHarnessFilters = {}): HarnessPredicate<MatLegacyDialogHarness> {
32+
static with(options: DialogHarnessFilters = {}): HarnessPredicate<MatLegacyDialogHarness> {
10133
return new HarnessPredicate(MatLegacyDialogHarness, options);
10234
}
35+
36+
protected override _title = this.locatorForOptional(MatLegacyDialogSection.TITLE);
37+
protected override _content = this.locatorForOptional(MatLegacyDialogSection.CONTENT);
38+
protected override _actions = this.locatorForOptional(MatLegacyDialogSection.ACTIONS);
10339
}

0 commit comments

Comments
 (0)