Skip to content

Commit 7122f18

Browse files
committed
refactor(multiple): convert tests and update formatting
Switches the dialog and snack bar tests to standalone and fixes some out of date formatting.
1 parent 3ea70e3 commit 7122f18

File tree

5 files changed

+98
-33
lines changed

5 files changed

+98
-33
lines changed

src/material/dialog/dialog.spec.ts

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
dispatchKeyboardEvent,
1111
dispatchMouseEvent,
1212
patchElementFocus,
13-
} from '../../cdk/testing/private';
13+
} from '@angular/cdk/testing/private';
1414
import {Location} from '@angular/common';
1515
import {SpyLocation} from '@angular/common/testing';
1616
import {
@@ -29,6 +29,7 @@ import {
2929
ViewChild,
3030
ViewContainerRef,
3131
ViewEncapsulation,
32+
forwardRef,
3233
} from '@angular/core';
3334
import {
3435
ComponentFixture,
@@ -49,6 +50,10 @@ import {
4950
MatDialogRef,
5051
MAT_DIALOG_DATA,
5152
MAT_DIALOG_DEFAULT_OPTIONS,
53+
MatDialogContent,
54+
MatDialogTitle,
55+
MatDialogActions,
56+
MatDialogClose,
5257
} from './index';
5358
import {CLOSE_ANIMATION_DURATION, OPEN_ANIMATION_DURATION} from './dialog-container';
5459

@@ -64,8 +69,9 @@ describe('MDC-based MatDialog', () => {
6469

6570
beforeEach(fakeAsync(() => {
6671
TestBed.configureTestingModule({
67-
imports: [MatDialogModule, NoopAnimationsModule],
68-
declarations: [
72+
imports: [
73+
MatDialogModule,
74+
NoopAnimationsModule,
6975
ComponentWithChildViewContainer,
7076
ComponentWithTemplateRef,
7177
PizzaMsg,
@@ -1839,8 +1845,7 @@ describe('MDC-based MatDialog with a parent MatDialog', () => {
18391845

18401846
beforeEach(fakeAsync(() => {
18411847
TestBed.configureTestingModule({
1842-
imports: [MatDialogModule, NoopAnimationsModule],
1843-
declarations: [ComponentThatProvidesMatDialog],
1848+
imports: [MatDialogModule, NoopAnimationsModule, ComponentThatProvidesMatDialog],
18441849
providers: [
18451850
{
18461851
provide: OverlayContainer,
@@ -1953,8 +1958,12 @@ describe('MDC-based MatDialog with default options', () => {
19531958
};
19541959

19551960
TestBed.configureTestingModule({
1956-
imports: [MatDialogModule, NoopAnimationsModule],
1957-
declarations: [ComponentWithChildViewContainer, DirectiveWithViewContainer],
1961+
imports: [
1962+
MatDialogModule,
1963+
NoopAnimationsModule,
1964+
ComponentWithChildViewContainer,
1965+
DirectiveWithViewContainer,
1966+
],
19581967
providers: [{provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: defaultConfig}],
19591968
});
19601969

@@ -2021,8 +2030,12 @@ describe('MDC-based MatDialog with animations enabled', () => {
20212030

20222031
beforeEach(fakeAsync(() => {
20232032
TestBed.configureTestingModule({
2024-
imports: [MatDialogModule, BrowserAnimationsModule],
2025-
declarations: [ComponentWithChildViewContainer, DirectiveWithViewContainer],
2033+
imports: [
2034+
MatDialogModule,
2035+
BrowserAnimationsModule,
2036+
ComponentWithChildViewContainer,
2037+
DirectiveWithViewContainer,
2038+
],
20262039
});
20272040

20282041
TestBed.compileComponents();
@@ -2081,8 +2094,7 @@ describe('MatDialog with explicit injector provided', () => {
20812094

20822095
beforeEach(fakeAsync(() => {
20832096
TestBed.configureTestingModule({
2084-
imports: [MatDialogModule, BrowserAnimationsModule],
2085-
declarations: [ModuleBoundDialogParentComponent],
2097+
imports: [MatDialogModule, BrowserAnimationsModule, ModuleBoundDialogParentComponent],
20862098
});
20872099

20882100
TestBed.compileComponents();
@@ -2106,7 +2118,10 @@ describe('MatDialog with explicit injector provided', () => {
21062118
});
21072119
});
21082120

2109-
@Directive({selector: 'dir-with-view-container'})
2121+
@Directive({
2122+
selector: 'dir-with-view-container',
2123+
standalone: true,
2124+
})
21102125
class DirectiveWithViewContainer {
21112126
constructor(public viewContainerRef: ViewContainerRef) {}
21122127
}
@@ -2122,6 +2137,8 @@ class ComponentWithOnPushViewContainer {
21222137
@Component({
21232138
selector: 'arbitrary-component',
21242139
template: `@if (showChildView) {<dir-with-view-container></dir-with-view-container>}`,
2140+
standalone: true,
2141+
imports: [DirectiveWithViewContainer],
21252142
})
21262143
class ComponentWithChildViewContainer {
21272144
showChildView = true;
@@ -2137,6 +2154,7 @@ class ComponentWithChildViewContainer {
21372154
selector: 'arbitrary-component-with-template-ref',
21382155
template: `<ng-template let-data let-dialogRef="dialogRef">
21392156
Cheese {{localValue}} {{data?.value}}{{setDialogRef(dialogRef)}}</ng-template>`,
2157+
standalone: true,
21402158
})
21412159
class ComponentWithTemplateRef {
21422160
localValue: string;
@@ -2151,7 +2169,10 @@ class ComponentWithTemplateRef {
21512169
}
21522170

21532171
/** Simple component for testing ComponentPortal. */
2154-
@Component({template: '<p>Pizza</p> <input> <button>Close</button>'})
2172+
@Component({
2173+
template: '<p>Pizza</p> <input> <button>Close</button>',
2174+
standalone: true,
2175+
})
21552176
class PizzaMsg {
21562177
constructor(
21572178
public dialogRef: MatDialogRef<PizzaMsg>,
@@ -2183,6 +2204,8 @@ class PizzaMsg {
21832204
<button class="with-submit" type="submit" mat-dialog-close>Should have submit</button>
21842205
</mat-dialog-actions>
21852206
`,
2207+
standalone: true,
2208+
imports: [MatDialogTitle, MatDialogContent, MatDialogActions, MatDialogClose],
21862209
})
21872210
class ContentElementDialog {
21882211
shownTitle: 'first' | 'second' | 'third' | 'all' = 'first';
@@ -2217,6 +2240,8 @@ class ContentElementDialog {
22172240
</mat-dialog-actions>
22182241
</ng-template>
22192242
`,
2243+
standalone: true,
2244+
imports: [MatDialogTitle, MatDialogContent, MatDialogActions, MatDialogClose],
22202245
})
22212246
class ComponentWithContentElementTemplateRef {
22222247
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
@@ -2228,18 +2253,28 @@ class ComponentWithContentElementTemplateRef {
22282253
}
22292254
}
22302255

2231-
@Component({template: '', providers: [MatDialog]})
2256+
@Component({
2257+
template: '',
2258+
providers: [MatDialog],
2259+
standalone: true,
2260+
})
22322261
class ComponentThatProvidesMatDialog {
22332262
constructor(public dialog: MatDialog) {}
22342263
}
22352264

22362265
/** Simple component for testing ComponentPortal. */
2237-
@Component({template: ''})
2266+
@Component({
2267+
template: '',
2268+
standalone: true,
2269+
})
22382270
class DialogWithInjectedData {
22392271
constructor(@Inject(MAT_DIALOG_DATA) public data: any) {}
22402272
}
22412273

2242-
@Component({template: '<p>Pasta</p>'})
2274+
@Component({
2275+
template: '<p>Pasta</p>',
2276+
standalone: true,
2277+
})
22432278
class DialogWithoutFocusableElements {}
22442279

22452280
@Component({
@@ -2248,7 +2283,10 @@ class DialogWithoutFocusableElements {}
22482283
})
22492284
class ShadowDomComponent {}
22502285

2251-
@Component({template: ''})
2286+
@Component({
2287+
template: '',
2288+
standalone: true,
2289+
})
22522290
class ModuleBoundDialogParentComponent {
22532291
constructor(
22542292
private _injector: Injector,
@@ -2272,16 +2310,22 @@ class ModuleBoundDialogService {
22722310

22732311
@Component({
22742312
template: '<module-bound-dialog-child-component></module-bound-dialog-child-component>',
2313+
standalone: true,
2314+
imports: [forwardRef(() => ModuleBoundDialogChildComponent)],
22752315
})
22762316
class ModuleBoundDialogComponent {}
22772317

2278-
@Component({selector: 'module-bound-dialog-child-component', template: '<p>{{service.name}}</p>'})
2318+
@Component({
2319+
selector: 'module-bound-dialog-child-component',
2320+
template: '<p>{{service.name}}</p>',
2321+
standalone: true,
2322+
})
22792323
class ModuleBoundDialogChildComponent {
22802324
constructor(public service: ModuleBoundDialogService) {}
22812325
}
22822326

22832327
@NgModule({
2284-
declarations: [ModuleBoundDialogComponent, ModuleBoundDialogChildComponent],
2328+
imports: [ModuleBoundDialogComponent, ModuleBoundDialogChildComponent],
22852329
providers: [ModuleBoundDialogService],
22862330
})
22872331
class ModuleBoundDialogModule {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
77
describe('MDC-based MatTestDialogOpener', () => {
88
beforeEach(fakeAsync(() => {
99
TestBed.configureTestingModule({
10-
imports: [MatTestDialogOpenerModule, NoopAnimationsModule],
11-
declarations: [ExampleComponent],
10+
imports: [MatTestDialogOpenerModule, NoopAnimationsModule, ExampleComponent],
1211
});
1312

1413
TestBed.compileComponents();
@@ -61,6 +60,7 @@ interface ExampleDialogResult {
6160
Data: {{data}}
6261
<button id="close-btn" (click)="close()">Close</button>
6362
`,
63+
standalone: true,
6464
})
6565
class ExampleComponent {
6666
constructor(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {Subscription} from 'rxjs';
2424
template: '',
2525
changeDetection: ChangeDetectionStrategy.OnPush,
2626
encapsulation: ViewEncapsulation.None,
27+
standalone: true,
2728
})
2829
export class MatTestDialogOpener<T = unknown, R = unknown> implements OnDestroy {
2930
/** Component that should be opened with the MatDialog `open` method. */
@@ -72,7 +73,6 @@ export class MatTestDialogOpener<T = unknown, R = unknown> implements OnDestroy
7273
}
7374

7475
@NgModule({
75-
declarations: [MatTestDialogOpener],
76-
imports: [MatDialogModule, NoopAnimationsModule],
76+
imports: [MatDialogModule, NoopAnimationsModule, MatTestDialogOpener],
7777
})
7878
export class MatTestDialogOpenerModule {}

src/material/snack-bar/snack-bar-ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export class MatSnackBarRef<T> {
5050
/** Whether the snack bar was dismissed using the action button. */
5151
private _dismissedByAction = false;
5252

53-
constructor(containerInstance: MatSnackBarContainer, private _overlayRef: OverlayRef) {
53+
constructor(
54+
containerInstance: MatSnackBarContainer,
55+
private _overlayRef: OverlayRef,
56+
) {
5457
this.containerInstance = containerInstance;
5558
containerInstance._onExit.subscribe(() => this._finishDismiss());
5659
}

src/material/snack-bar/snack-bar.spec.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {LiveAnnouncer} from '@angular/cdk/a11y';
22
import {OverlayContainer} from '@angular/cdk/overlay';
3-
import {CommonModule} from '@angular/common';
43
import {
54
Component,
65
Directive,
@@ -39,8 +38,9 @@ describe('MatSnackBar', () => {
3938

4039
beforeEach(fakeAsync(() => {
4140
TestBed.configureTestingModule({
42-
imports: [MatSnackBarModule, CommonModule, NoopAnimationsModule],
43-
declarations: [
41+
imports: [
42+
MatSnackBarModule,
43+
NoopAnimationsModule,
4444
ComponentWithChildViewContainer,
4545
BurritosNotification,
4646
DirectiveWithViewContainer,
@@ -765,8 +765,12 @@ describe('MatSnackBar with parent MatSnackBar', () => {
765765

766766
beforeEach(fakeAsync(() => {
767767
TestBed.configureTestingModule({
768-
imports: [MatSnackBarModule, CommonModule, NoopAnimationsModule],
769-
declarations: [ComponentThatProvidesMatSnackBar, DirectiveWithViewContainer],
768+
imports: [
769+
MatSnackBarModule,
770+
NoopAnimationsModule,
771+
ComponentThatProvidesMatSnackBar,
772+
DirectiveWithViewContainer,
773+
],
770774
}).compileComponents();
771775
}));
772776

@@ -839,8 +843,12 @@ describe('MatSnackBar Positioning', () => {
839843

840844
beforeEach(fakeAsync(() => {
841845
TestBed.configureTestingModule({
842-
imports: [MatSnackBarModule, CommonModule, NoopAnimationsModule],
843-
declarations: [ComponentWithChildViewContainer, DirectiveWithViewContainer],
846+
imports: [
847+
MatSnackBarModule,
848+
NoopAnimationsModule,
849+
ComponentWithChildViewContainer,
850+
DirectiveWithViewContainer,
851+
],
844852
}).compileComponents();
845853
}));
846854

@@ -1084,14 +1092,19 @@ describe('MatSnackBar Positioning', () => {
10841092
}));
10851093
});
10861094

1087-
@Directive({selector: 'dir-with-view-container'})
1095+
@Directive({
1096+
selector: 'dir-with-view-container',
1097+
standalone: true,
1098+
})
10881099
class DirectiveWithViewContainer {
10891100
constructor(public viewContainerRef: ViewContainerRef) {}
10901101
}
10911102

10921103
@Component({
10931104
selector: 'arbitrary-component',
10941105
template: `@if (childComponentExists) {<dir-with-view-container></dir-with-view-container>}`,
1106+
standalone: true,
1107+
imports: [DirectiveWithViewContainer],
10951108
})
10961109
class ComponentWithChildViewContainer {
10971110
@ViewChild(DirectiveWithViewContainer) childWithViewContainer: DirectiveWithViewContainer;
@@ -1110,14 +1123,18 @@ class ComponentWithChildViewContainer {
11101123
Fries {{localValue}} {{data?.value}}
11111124
</ng-template>
11121125
`,
1126+
standalone: true,
11131127
})
11141128
class ComponentWithTemplateRef {
11151129
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
11161130
localValue: string;
11171131
}
11181132

11191133
/** Simple component for testing ComponentPortal. */
1120-
@Component({template: '<p>Burritos are on the way.</p>'})
1134+
@Component({
1135+
template: '<p>Burritos are on the way.</p>',
1136+
standalone: true,
1137+
})
11211138
class BurritosNotification {
11221139
constructor(
11231140
public snackBarRef: MatSnackBarRef<BurritosNotification>,
@@ -1128,6 +1145,7 @@ class BurritosNotification {
11281145
@Component({
11291146
template: '',
11301147
providers: [MatSnackBar],
1148+
standalone: true,
11311149
})
11321150
class ComponentThatProvidesMatSnackBar {
11331151
constructor(public snackBar: MatSnackBar) {}

0 commit comments

Comments
 (0)