Skip to content

Commit 31077c2

Browse files
authored
test(multiple): switch CDK tests to standalone (#31113)
Switches the remaining CDK tests that weren't standalone to standalone. For the `popover-edit` tests specifically I ended up removing the templates that were created using string interpolation, because it made them hard to read and it can be a problem if we start running the tests in AoT.
1 parent 7228488 commit 31077c2

File tree

9 files changed

+784
-905
lines changed

9 files changed

+784
-905
lines changed

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 626 additions & 562 deletions
Large diffs are not rendered by default.

src/cdk/dialog/dialog.spec.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,6 @@ describe('Dialog', () => {
4646

4747
beforeEach(fakeAsync(() => {
4848
TestBed.configureTestingModule({
49-
imports: [
50-
DialogModule,
51-
ComponentWithChildViewContainer,
52-
ComponentWithTemplateRef,
53-
PizzaMsg,
54-
ContentElementDialog,
55-
DialogWithInjectedData,
56-
DialogWithoutFocusableElements,
57-
DirectiveWithViewContainer,
58-
TemplateInjectorParentComponent,
59-
TemplateInjectorInnerDirective,
60-
],
6149
providers: [
6250
{provide: Location, useClass: SpyLocation},
6351
{provide: TEMPLATE_INJECTOR_TEST_TOKEN, useValue: 'hello from test module'},
@@ -1416,7 +1404,6 @@ class DirectiveWithViewContainer {
14161404
@Component({
14171405
changeDetection: ChangeDetectionStrategy.OnPush,
14181406
template: 'hello',
1419-
standalone: false,
14201407
})
14211408
class ComponentWithOnPushViewContainer {
14221409
viewContainerRef = inject(ViewContainerRef);
@@ -1509,7 +1496,6 @@ class DialogWithoutFocusableElements {}
15091496
@Component({
15101497
template: `<button>I'm a button</button>`,
15111498
encapsulation: ViewEncapsulation.ShadowDom,
1512-
standalone: false,
15131499
})
15141500
class ShadowDomComponent {}
15151501

src/cdk/listbox/listbox.spec.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ import {TestBed, fakeAsync, tick} from '@angular/core/testing';
44
import {FormControl, ReactiveFormsModule} from '@angular/forms';
55
import {By} from '@angular/platform-browser';
66
import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '../testing/private';
7-
import {CdkListbox, CdkListboxModule, CdkOption, ListboxValueChangeEvent} from './index';
7+
import {CdkListbox, CdkOption, ListboxValueChangeEvent} from './index';
88

9-
function setupComponent<T, O = string>(component: Type<T>, imports: any[] = []) {
10-
TestBed.configureTestingModule({
11-
imports: [CdkListboxModule, ...imports],
12-
declarations: [component],
13-
});
9+
function setupComponent<T, O = string>(component: Type<T>) {
1410
const fixture = TestBed.createComponent(component);
1511
fixture.detectChanges();
1612

@@ -861,29 +857,23 @@ describe('CdkOption and CdkListbox', () => {
861857

862858
describe('with FormControl', () => {
863859
it('should reflect disabled state of the FormControl', () => {
864-
const {testComponent, fixture, listbox} = setupComponent(ListboxWithFormControl, [
865-
ReactiveFormsModule,
866-
]);
860+
const {testComponent, fixture, listbox} = setupComponent(ListboxWithFormControl);
867861
testComponent.formControl.disable();
868862
fixture.detectChanges();
869863

870864
expect(listbox.disabled).toBeTrue();
871865
});
872866

873867
it('should update when FormControl value changes', () => {
874-
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl, [
875-
ReactiveFormsModule,
876-
]);
868+
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl);
877869
testComponent.formControl.setValue(['banana']);
878870
fixture.detectChanges();
879871

880872
expect(options[2].isSelected()).toBeTrue();
881873
});
882874

883875
it('should update FormControl when selection changes', () => {
884-
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl, [
885-
ReactiveFormsModule,
886-
]);
876+
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl);
887877
const spy = jasmine.createSpy();
888878
const subscription = testComponent.formControl.valueChanges.subscribe(spy);
889879
fixture.detectChanges();
@@ -898,9 +888,7 @@ describe('CdkOption and CdkListbox', () => {
898888
});
899889

900890
it('should update multi-select listbox when FormControl value changes', () => {
901-
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl, [
902-
ReactiveFormsModule,
903-
]);
891+
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl);
904892
testComponent.isMultiselectable = true;
905893
fixture.changeDetectorRef.markForCheck();
906894
fixture.detectChanges();
@@ -912,9 +900,7 @@ describe('CdkOption and CdkListbox', () => {
912900
});
913901

914902
it('should update FormControl when multi-selection listbox changes', () => {
915-
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl, [
916-
ReactiveFormsModule,
917-
]);
903+
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl);
918904
testComponent.isMultiselectable = true;
919905
fixture.changeDetectorRef.markForCheck();
920906
fixture.detectChanges();
@@ -935,9 +921,7 @@ describe('CdkOption and CdkListbox', () => {
935921
});
936922

937923
it('should throw when multiple values selected in single-select listbox', () => {
938-
const {testComponent, fixture} = setupComponent(ListboxWithFormControl, [
939-
ReactiveFormsModule,
940-
]);
924+
const {testComponent, fixture} = setupComponent(ListboxWithFormControl);
941925

942926
expect(() => {
943927
testComponent.formControl.setValue(['orange', 'banana']);
@@ -946,9 +930,7 @@ describe('CdkOption and CdkListbox', () => {
946930
});
947931

948932
it('should throw when an invalid value is selected', () => {
949-
const {testComponent, fixture} = setupComponent(ListboxWithFormControl, [
950-
ReactiveFormsModule,
951-
]);
933+
const {testComponent, fixture} = setupComponent(ListboxWithFormControl);
952934
testComponent.isMultiselectable = true;
953935
fixture.changeDetectorRef.markForCheck();
954936
fixture.detectChanges();
@@ -961,13 +943,13 @@ describe('CdkOption and CdkListbox', () => {
961943

962944
it('should not throw on init with a preselected form control and a dynamic set of options', () => {
963945
expect(() => {
964-
setupComponent(ListboxWithPreselectedFormControl, [ReactiveFormsModule]);
946+
setupComponent(ListboxWithPreselectedFormControl);
965947
}).not.toThrow();
966948
});
967949

968950
it('should throw on init if the preselected value is invalid', () => {
969951
expect(() => {
970-
setupComponent(ListboxWithInvalidPreselectedFormControl, [ReactiveFormsModule]);
952+
setupComponent(ListboxWithInvalidPreselectedFormControl);
971953
}).toThrowError('Listbox has selected values that do not match any of its options.');
972954
});
973955
});
@@ -1000,7 +982,7 @@ describe('CdkOption and CdkListbox', () => {
1000982
<div cdkOption="peach">Peach</div>
1001983
</div>
1002984
`,
1003-
standalone: false,
985+
imports: [CdkListbox, CdkOption],
1004986
})
1005987
class ListboxWithOptions {
1006988
changedOption: CdkOption | null;
@@ -1026,7 +1008,7 @@ class ListboxWithOptions {
10261008

10271009
@Component({
10281010
template: `<div cdkListbox></div>`,
1029-
standalone: false,
1011+
imports: [CdkListbox],
10301012
})
10311013
class ListboxWithNoOptions {}
10321014

@@ -1042,7 +1024,7 @@ class ListboxWithNoOptions {}
10421024
<div cdkOption="peach">Peach</div>
10431025
</div>
10441026
`,
1045-
standalone: false,
1027+
imports: [CdkListbox, CdkOption, ReactiveFormsModule],
10461028
})
10471029
class ListboxWithFormControl {
10481030
formControl = new FormControl();
@@ -1058,7 +1040,7 @@ class ListboxWithFormControl {
10581040
}
10591041
</div>
10601042
`,
1061-
standalone: false,
1043+
imports: [CdkListbox, CdkOption, ReactiveFormsModule],
10621044
})
10631045
class ListboxWithPreselectedFormControl {
10641046
options = ['a', 'b', 'c'];
@@ -1073,7 +1055,7 @@ class ListboxWithPreselectedFormControl {
10731055
}
10741056
</div>
10751057
`,
1076-
standalone: false,
1058+
imports: [CdkListbox, CdkOption, ReactiveFormsModule],
10771059
})
10781060
class ListboxWithInvalidPreselectedFormControl {
10791061
options = ['a', 'b', 'c'];
@@ -1089,7 +1071,7 @@ class ListboxWithInvalidPreselectedFormControl {
10891071
<li cdkOption="peach" cdkOptionTypeaheadLabel="peach">🍑</li>
10901072
</ul>
10911073
`,
1092-
standalone: false,
1074+
imports: [CdkListbox, CdkOption],
10931075
})
10941076
class ListboxWithCustomTypeahead {}
10951077

@@ -1103,7 +1085,7 @@ class ListboxWithCustomTypeahead {}
11031085
<div cdkOption="peach">Peach</div>
11041086
</div>
11051087
`,
1106-
standalone: false,
1088+
imports: [CdkListbox, CdkOption],
11071089
})
11081090
class ListboxWithBoundValue {
11091091
value = ['banana'];
@@ -1120,7 +1102,7 @@ class ListboxWithBoundValue {
11201102
<div cdkOption="peach">Peach</div>
11211103
</div>
11221104
`,
1123-
standalone: false,
1105+
imports: [CdkListbox, CdkOption],
11241106
})
11251107
class ListboxWithMultipleBoundValues {
11261108
value = ['apple', 'banana'];
@@ -1134,7 +1116,7 @@ class ListboxWithMultipleBoundValues {
11341116
}
11351117
</div>
11361118
`,
1137-
standalone: false,
1119+
imports: [CdkListbox, CdkOption],
11381120
})
11391121
class ListboxWithObjectValues {
11401122
fruits = [{name: 'Apple'}, {name: 'Orange'}, {name: 'Banana'}, {name: 'Peach'}];

src/cdk/menu/context-menu-trigger.spec.ts

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {Component, ViewChild, ElementRef, Type, ViewChildren, QueryList} from '@angular/core';
2-
import {CdkMenuModule} from './menu-module';
3-
import {TestBed, waitForAsync, ComponentFixture} from '@angular/core/testing';
1+
import {Component, ViewChild, ElementRef, ViewChildren, QueryList} from '@angular/core';
2+
import {TestBed, ComponentFixture} from '@angular/core/testing';
43
import {CdkMenu} from './menu';
54
import {CdkContextMenuTrigger} from './context-menu-trigger';
65
import {dispatchKeyboardEvent, dispatchMouseEvent} from '../testing/private';
@@ -14,13 +13,6 @@ describe('CdkContextMenuTrigger', () => {
1413
describe('with simple context menu trigger', () => {
1514
let fixture: ComponentFixture<SimpleContextMenu>;
1615

17-
beforeEach(waitForAsync(() => {
18-
TestBed.configureTestingModule({
19-
imports: [CdkMenuModule],
20-
declarations: [SimpleContextMenu],
21-
});
22-
}));
23-
2416
beforeEach(() => {
2517
fixture = TestBed.createComponent(SimpleContextMenu);
2618
fixture.detectChanges();
@@ -164,13 +156,6 @@ describe('CdkContextMenuTrigger', () => {
164156
describe('nested context menu triggers', () => {
165157
let fixture: ComponentFixture<NestedContextMenu>;
166158

167-
beforeEach(waitForAsync(() => {
168-
TestBed.configureTestingModule({
169-
imports: [CdkMenuModule],
170-
declarations: [NestedContextMenu],
171-
});
172-
}));
173-
174159
beforeEach(() => {
175160
fixture = TestBed.createComponent(NestedContextMenu);
176161
fixture.detectChanges();
@@ -279,13 +264,6 @@ describe('CdkContextMenuTrigger', () => {
279264
let fixture: ComponentFixture<ContextMenuWithSubmenu>;
280265
let instance: ContextMenuWithSubmenu;
281266

282-
beforeEach(waitForAsync(() => {
283-
TestBed.configureTestingModule({
284-
imports: [CdkMenuModule],
285-
declarations: [ContextMenuWithSubmenu],
286-
});
287-
}));
288-
289267
beforeEach(() => {
290268
fixture = TestBed.createComponent(ContextMenuWithSubmenu);
291269
fixture.detectChanges();
@@ -310,13 +288,6 @@ describe('CdkContextMenuTrigger', () => {
310288
let nativeMenuBar: HTMLElement;
311289
let nativeMenuBarTrigger: HTMLElement;
312290

313-
beforeEach(waitForAsync(() => {
314-
TestBed.configureTestingModule({
315-
imports: [CdkMenuModule],
316-
declarations: [ContextMenuWithMenuBarAndInlineMenu],
317-
});
318-
}));
319-
320291
beforeEach(() => {
321292
fixture = TestBed.createComponent(ContextMenuWithMenuBarAndInlineMenu);
322293
fixture.detectChanges();
@@ -410,23 +381,10 @@ describe('CdkContextMenuTrigger', () => {
410381
});
411382

412383
describe('with shared triggered menu', () => {
413-
/**
414-
* Return a function which builds the given component and renders it.
415-
* @param componentClass the component to create
416-
*/
417-
function createComponent<T>(componentClass: Type<T>) {
418-
TestBed.configureTestingModule({
419-
imports: [CdkMenuModule],
420-
declarations: [componentClass],
421-
});
422-
423-
const fixture = TestBed.createComponent(componentClass);
384+
it('should allow a context menu and menubar trigger share a menu', () => {
385+
const fixture = TestBed.createComponent(MenuBarAndContextTriggerShareMenu);
424386
fixture.detectChanges();
425-
return fixture;
426-
}
427387

428-
it('should allow a context menu and menubar trigger share a menu', () => {
429-
const fixture = createComponent(MenuBarAndContextTriggerShareMenu);
430388
expect(fixture.componentInstance.menus.length).toBe(0);
431389
fixture.componentInstance.menuBarTrigger.toggle();
432390
fixture.detectChanges();
@@ -441,11 +399,6 @@ describe('CdkContextMenuTrigger', () => {
441399
});
442400

443401
it('should be able to pass data to the menu via the template context', () => {
444-
TestBed.configureTestingModule({
445-
imports: [CdkMenuModule],
446-
declarations: [ContextTriggerWithData],
447-
});
448-
449402
const fixture = TestBed.createComponent(ContextTriggerWithData);
450403
fixture.componentInstance.menuData = {message: 'Hello!'};
451404
fixture.detectChanges();
@@ -467,7 +420,7 @@ describe('CdkContextMenuTrigger', () => {
467420
</div>
468421
</ng-template>
469422
`,
470-
standalone: false,
423+
imports: [CdkContextMenuTrigger, CdkMenu, CdkMenuItem],
471424
})
472425
class SimpleContextMenu {
473426
@ViewChild(CdkContextMenuTrigger) trigger: CdkContextMenuTrigger;
@@ -496,7 +449,7 @@ class SimpleContextMenu {
496449
<div #copy_menu cdkMenu></div>
497450
</ng-template>
498451
`,
499-
standalone: false,
452+
imports: [CdkContextMenuTrigger, CdkMenu],
500453
})
501454
class NestedContextMenu {
502455
@ViewChild('cut_trigger', {read: ElementRef}) cutContext: ElementRef<HTMLElement>;
@@ -522,7 +475,7 @@ class NestedContextMenu {
522475
<div #copy_menu cdkMenu></div>
523476
</ng-template>
524477
`,
525-
standalone: false,
478+
imports: [CdkContextMenuTrigger, CdkMenuTrigger, CdkMenu, CdkMenuItem],
526479
})
527480
class ContextMenuWithSubmenu {
528481
@ViewChild(CdkContextMenuTrigger, {read: ElementRef}) context: ElementRef<HTMLElement>;
@@ -553,7 +506,7 @@ class ContextMenuWithSubmenu {
553506
<button #inline_menu_button cdkMenuItem></button>
554507
</div>
555508
`,
556-
standalone: false,
509+
imports: [CdkContextMenuTrigger, CdkMenuTrigger, CdkMenu, CdkMenuItem, CdkMenuBar],
557510
})
558511
class ContextMenuWithMenuBarAndInlineMenu {
559512
@ViewChild(CdkMenuBar, {read: ElementRef}) nativeMenuBar: ElementRef;
@@ -582,7 +535,7 @@ class ContextMenuWithMenuBarAndInlineMenu {
582535
</div>
583536
</ng-template>
584537
`,
585-
standalone: false,
538+
imports: [CdkMenuBar, CdkContextMenuTrigger, CdkMenu, CdkMenuItem, CdkMenuTrigger],
586539
})
587540
class MenuBarAndContextTriggerShareMenu {
588541
@ViewChild(CdkMenuTrigger) menuBarTrigger: CdkMenuTrigger;
@@ -598,7 +551,7 @@ class MenuBarAndContextTriggerShareMenu {
598551
<div cdkMenu class="test-menu">{{message}}</div>
599552
</ng-template>
600553
`,
601-
standalone: false,
554+
imports: [CdkContextMenuTrigger, CdkMenu],
602555
})
603556
class ContextTriggerWithData {
604557
@ViewChild(CdkContextMenuTrigger, {read: ElementRef}) triggerElement: ElementRef<HTMLElement>;

0 commit comments

Comments
 (0)