Skip to content

test(multiple): switch CDK tests to standalone #31113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,188 changes: 626 additions & 562 deletions src/cdk-experimental/popover-edit/popover-edit.spec.ts

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions src/cdk/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ describe('Dialog', () => {

beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [
DialogModule,
ComponentWithChildViewContainer,
ComponentWithTemplateRef,
PizzaMsg,
ContentElementDialog,
DialogWithInjectedData,
DialogWithoutFocusableElements,
DirectiveWithViewContainer,
TemplateInjectorParentComponent,
TemplateInjectorInnerDirective,
],
providers: [
{provide: Location, useClass: SpyLocation},
{provide: TEMPLATE_INJECTOR_TEST_TOKEN, useValue: 'hello from test module'},
Expand Down Expand Up @@ -1416,7 +1404,6 @@ class DirectiveWithViewContainer {
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
template: 'hello',
standalone: false,
})
class ComponentWithOnPushViewContainer {
viewContainerRef = inject(ViewContainerRef);
Expand Down Expand Up @@ -1509,7 +1496,6 @@ class DialogWithoutFocusableElements {}
@Component({
template: `<button>I'm a button</button>`,
encapsulation: ViewEncapsulation.ShadowDom,
standalone: false,
})
class ShadowDomComponent {}

Expand Down
58 changes: 20 additions & 38 deletions src/cdk/listbox/listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import {TestBed, fakeAsync, tick} from '@angular/core/testing';
import {FormControl, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '../testing/private';
import {CdkListbox, CdkListboxModule, CdkOption, ListboxValueChangeEvent} from './index';
import {CdkListbox, CdkOption, ListboxValueChangeEvent} from './index';

function setupComponent<T, O = string>(component: Type<T>, imports: any[] = []) {
TestBed.configureTestingModule({
imports: [CdkListboxModule, ...imports],
declarations: [component],
});
function setupComponent<T, O = string>(component: Type<T>) {
const fixture = TestBed.createComponent(component);
fixture.detectChanges();

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

describe('with FormControl', () => {
it('should reflect disabled state of the FormControl', () => {
const {testComponent, fixture, listbox} = setupComponent(ListboxWithFormControl, [
ReactiveFormsModule,
]);
const {testComponent, fixture, listbox} = setupComponent(ListboxWithFormControl);
testComponent.formControl.disable();
fixture.detectChanges();

expect(listbox.disabled).toBeTrue();
});

it('should update when FormControl value changes', () => {
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl, [
ReactiveFormsModule,
]);
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl);
testComponent.formControl.setValue(['banana']);
fixture.detectChanges();

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

it('should update FormControl when selection changes', () => {
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl, [
ReactiveFormsModule,
]);
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl);
const spy = jasmine.createSpy();
const subscription = testComponent.formControl.valueChanges.subscribe(spy);
fixture.detectChanges();
Expand All @@ -898,9 +888,7 @@ describe('CdkOption and CdkListbox', () => {
});

it('should update multi-select listbox when FormControl value changes', () => {
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl, [
ReactiveFormsModule,
]);
const {testComponent, fixture, options} = setupComponent(ListboxWithFormControl);
testComponent.isMultiselectable = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
Expand All @@ -912,9 +900,7 @@ describe('CdkOption and CdkListbox', () => {
});

it('should update FormControl when multi-selection listbox changes', () => {
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl, [
ReactiveFormsModule,
]);
const {testComponent, fixture, optionEls} = setupComponent(ListboxWithFormControl);
testComponent.isMultiselectable = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
Expand All @@ -935,9 +921,7 @@ describe('CdkOption and CdkListbox', () => {
});

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

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

it('should throw when an invalid value is selected', () => {
const {testComponent, fixture} = setupComponent(ListboxWithFormControl, [
ReactiveFormsModule,
]);
const {testComponent, fixture} = setupComponent(ListboxWithFormControl);
testComponent.isMultiselectable = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
Expand All @@ -961,13 +943,13 @@ describe('CdkOption and CdkListbox', () => {

it('should not throw on init with a preselected form control and a dynamic set of options', () => {
expect(() => {
setupComponent(ListboxWithPreselectedFormControl, [ReactiveFormsModule]);
setupComponent(ListboxWithPreselectedFormControl);
}).not.toThrow();
});

it('should throw on init if the preselected value is invalid', () => {
expect(() => {
setupComponent(ListboxWithInvalidPreselectedFormControl, [ReactiveFormsModule]);
setupComponent(ListboxWithInvalidPreselectedFormControl);
}).toThrowError('Listbox has selected values that do not match any of its options.');
});
});
Expand Down Expand Up @@ -1000,7 +982,7 @@ describe('CdkOption and CdkListbox', () => {
<div cdkOption="peach">Peach</div>
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption],
})
class ListboxWithOptions {
changedOption: CdkOption | null;
Expand All @@ -1026,7 +1008,7 @@ class ListboxWithOptions {

@Component({
template: `<div cdkListbox></div>`,
standalone: false,
imports: [CdkListbox],
})
class ListboxWithNoOptions {}

Expand All @@ -1042,7 +1024,7 @@ class ListboxWithNoOptions {}
<div cdkOption="peach">Peach</div>
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption, ReactiveFormsModule],
})
class ListboxWithFormControl {
formControl = new FormControl();
Expand All @@ -1058,7 +1040,7 @@ class ListboxWithFormControl {
}
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption, ReactiveFormsModule],
})
class ListboxWithPreselectedFormControl {
options = ['a', 'b', 'c'];
Expand All @@ -1073,7 +1055,7 @@ class ListboxWithPreselectedFormControl {
}
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption, ReactiveFormsModule],
})
class ListboxWithInvalidPreselectedFormControl {
options = ['a', 'b', 'c'];
Expand All @@ -1089,7 +1071,7 @@ class ListboxWithInvalidPreselectedFormControl {
<li cdkOption="peach" cdkOptionTypeaheadLabel="peach">🍑</li>
</ul>
`,
standalone: false,
imports: [CdkListbox, CdkOption],
})
class ListboxWithCustomTypeahead {}

Expand All @@ -1103,7 +1085,7 @@ class ListboxWithCustomTypeahead {}
<div cdkOption="peach">Peach</div>
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption],
})
class ListboxWithBoundValue {
value = ['banana'];
Expand All @@ -1120,7 +1102,7 @@ class ListboxWithBoundValue {
<div cdkOption="peach">Peach</div>
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption],
})
class ListboxWithMultipleBoundValues {
value = ['apple', 'banana'];
Expand All @@ -1134,7 +1116,7 @@ class ListboxWithMultipleBoundValues {
}
</div>
`,
standalone: false,
imports: [CdkListbox, CdkOption],
})
class ListboxWithObjectValues {
fruits = [{name: 'Apple'}, {name: 'Orange'}, {name: 'Banana'}, {name: 'Peach'}];
Expand Down
67 changes: 10 additions & 57 deletions src/cdk/menu/context-menu-trigger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Component, ViewChild, ElementRef, Type, ViewChildren, QueryList} from '@angular/core';
import {CdkMenuModule} from './menu-module';
import {TestBed, waitForAsync, ComponentFixture} from '@angular/core/testing';
import {Component, ViewChild, ElementRef, ViewChildren, QueryList} from '@angular/core';
import {TestBed, ComponentFixture} from '@angular/core/testing';
import {CdkMenu} from './menu';
import {CdkContextMenuTrigger} from './context-menu-trigger';
import {dispatchKeyboardEvent, dispatchMouseEvent} from '../testing/private';
Expand All @@ -14,13 +13,6 @@ describe('CdkContextMenuTrigger', () => {
describe('with simple context menu trigger', () => {
let fixture: ComponentFixture<SimpleContextMenu>;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [SimpleContextMenu],
});
}));

beforeEach(() => {
fixture = TestBed.createComponent(SimpleContextMenu);
fixture.detectChanges();
Expand Down Expand Up @@ -164,13 +156,6 @@ describe('CdkContextMenuTrigger', () => {
describe('nested context menu triggers', () => {
let fixture: ComponentFixture<NestedContextMenu>;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [NestedContextMenu],
});
}));

beforeEach(() => {
fixture = TestBed.createComponent(NestedContextMenu);
fixture.detectChanges();
Expand Down Expand Up @@ -279,13 +264,6 @@ describe('CdkContextMenuTrigger', () => {
let fixture: ComponentFixture<ContextMenuWithSubmenu>;
let instance: ContextMenuWithSubmenu;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [ContextMenuWithSubmenu],
});
}));

beforeEach(() => {
fixture = TestBed.createComponent(ContextMenuWithSubmenu);
fixture.detectChanges();
Expand All @@ -310,13 +288,6 @@ describe('CdkContextMenuTrigger', () => {
let nativeMenuBar: HTMLElement;
let nativeMenuBarTrigger: HTMLElement;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [ContextMenuWithMenuBarAndInlineMenu],
});
}));

beforeEach(() => {
fixture = TestBed.createComponent(ContextMenuWithMenuBarAndInlineMenu);
fixture.detectChanges();
Expand Down Expand Up @@ -410,23 +381,10 @@ describe('CdkContextMenuTrigger', () => {
});

describe('with shared triggered menu', () => {
/**
* Return a function which builds the given component and renders it.
* @param componentClass the component to create
*/
function createComponent<T>(componentClass: Type<T>) {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [componentClass],
});

const fixture = TestBed.createComponent(componentClass);
it('should allow a context menu and menubar trigger share a menu', () => {
const fixture = TestBed.createComponent(MenuBarAndContextTriggerShareMenu);
fixture.detectChanges();
return fixture;
}

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

it('should be able to pass data to the menu via the template context', () => {
TestBed.configureTestingModule({
imports: [CdkMenuModule],
declarations: [ContextTriggerWithData],
});

const fixture = TestBed.createComponent(ContextTriggerWithData);
fixture.componentInstance.menuData = {message: 'Hello!'};
fixture.detectChanges();
Expand All @@ -467,7 +420,7 @@ describe('CdkContextMenuTrigger', () => {
</div>
</ng-template>
`,
standalone: false,
imports: [CdkContextMenuTrigger, CdkMenu, CdkMenuItem],
})
class SimpleContextMenu {
@ViewChild(CdkContextMenuTrigger) trigger: CdkContextMenuTrigger;
Expand Down Expand Up @@ -496,7 +449,7 @@ class SimpleContextMenu {
<div #copy_menu cdkMenu></div>
</ng-template>
`,
standalone: false,
imports: [CdkContextMenuTrigger, CdkMenu],
})
class NestedContextMenu {
@ViewChild('cut_trigger', {read: ElementRef}) cutContext: ElementRef<HTMLElement>;
Expand All @@ -522,7 +475,7 @@ class NestedContextMenu {
<div #copy_menu cdkMenu></div>
</ng-template>
`,
standalone: false,
imports: [CdkContextMenuTrigger, CdkMenuTrigger, CdkMenu, CdkMenuItem],
})
class ContextMenuWithSubmenu {
@ViewChild(CdkContextMenuTrigger, {read: ElementRef}) context: ElementRef<HTMLElement>;
Expand Down Expand Up @@ -553,7 +506,7 @@ class ContextMenuWithSubmenu {
<button #inline_menu_button cdkMenuItem></button>
</div>
`,
standalone: false,
imports: [CdkContextMenuTrigger, CdkMenuTrigger, CdkMenu, CdkMenuItem, CdkMenuBar],
})
class ContextMenuWithMenuBarAndInlineMenu {
@ViewChild(CdkMenuBar, {read: ElementRef}) nativeMenuBar: ElementRef;
Expand Down Expand Up @@ -582,7 +535,7 @@ class ContextMenuWithMenuBarAndInlineMenu {
</div>
</ng-template>
`,
standalone: false,
imports: [CdkMenuBar, CdkContextMenuTrigger, CdkMenu, CdkMenuItem, CdkMenuTrigger],
})
class MenuBarAndContextTriggerShareMenu {
@ViewChild(CdkMenuTrigger) menuBarTrigger: CdkMenuTrigger;
Expand All @@ -598,7 +551,7 @@ class MenuBarAndContextTriggerShareMenu {
<div cdkMenu class="test-menu">{{message}}</div>
</ng-template>
`,
standalone: false,
imports: [CdkContextMenuTrigger, CdkMenu],
})
class ContextTriggerWithData {
@ViewChild(CdkContextMenuTrigger, {read: ElementRef}) triggerElement: ElementRef<HTMLElement>;
Expand Down
Loading
Loading