Skip to content

Commit c5f9a60

Browse files
authored
test(material/menu): remove NoopAnimationsModule usage (#31800)
Reworks a menu test so that it doesn't depend on the `NoopAnimationsModule` anymore.
1 parent ca1cd86 commit c5f9a60

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/material/menu/menu.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
} from '@angular/core';
3131
import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';
3232
import {By} from '@angular/platform-browser';
33-
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
3433
import {Subject} from 'rxjs';
3534
import {
3635
createKeyboardEvent,
@@ -256,10 +255,6 @@ describe('MatMenu', () => {
256255
}));
257256

258257
it('should move focus to another item if the active item is destroyed', fakeAsync(() => {
259-
// TODO(crisbeto): figure out why NoopAnimationsModule is necessary
260-
// here and our token isn't enough. Likely indicates an issue.
261-
TestBed.resetTestingModule().configureTestingModule({imports: [NoopAnimationsModule]});
262-
overlayContainerElement = TestBed.inject(OverlayContainer).getContainerElement();
263258
const fixture = TestBed.createComponent(MenuWithRepeatedItems);
264259
fixture.detectChanges();
265260
const triggerEl = fixture.componentInstance.triggerEl.nativeElement;
@@ -268,17 +263,19 @@ describe('MatMenu', () => {
268263
fixture.detectChanges();
269264
tick(500);
270265

271-
const items = overlayContainerElement.querySelectorAll(
266+
const items = overlayContainerElement.querySelectorAll<HTMLElement>(
272267
'.mat-mdc-menu-panel .mat-mdc-menu-item',
273268
);
274269

275-
expect(document.activeElement).toBe(items[0]);
270+
// Stub out the `_hasFocus` method since it's hard to move focus reliably within the test.
271+
spyOn(fixture.componentInstance.itemInstances.first, '_hasFocus').and.callFake(() => true);
272+
const spy = spyOn(items[1], 'focus');
276273

277274
fixture.componentInstance.items.shift();
278275
fixture.detectChanges();
279276
tick(500);
280277

281-
expect(document.activeElement).toBe(items[1]);
278+
expect(spy).toHaveBeenCalled();
282279
}));
283280

284281
it('should be able to set a custom class on the backdrop', fakeAsync(() => {
@@ -3027,5 +3024,6 @@ class MenuWithRepeatedItems {
30273024
@ViewChild(MatMenuTrigger, {static: false}) trigger: MatMenuTrigger;
30283025
@ViewChild('triggerEl', {static: false}) triggerEl: ElementRef<HTMLElement>;
30293026
@ViewChild(MatMenu, {static: false}) menu: MatMenu;
3027+
@ViewChildren(MatMenuItem) itemInstances: QueryList<MatMenuItem>;
30303028
items = ['One', 'Two', 'Three'];
30313029
}

0 commit comments

Comments
 (0)