Skip to content

Commit 343d701

Browse files
authored
test(multiple): clean up manual ngOnDestroy calls (#23423)
We used to have to call `ngOnDestroy` manually on providers, because `TestBed` wouldn't do it for us. Now that we're using the `teardown` flag, we don't have to do it anymore.
1 parent e9091fe commit 343d701

39 files changed

+62
-436
lines changed

src/cdk-experimental/dialog/dialog.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ describe('Dialog', () => {
4242
let testViewContainerRef: ViewContainerRef;
4343
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
4444
let mockLocation: SpyLocation;
45-
let overlayContainer: OverlayContainer;
4645

4746
beforeEach(fakeAsync(() => {
4847
TestBed.configureTestingModule({
@@ -59,7 +58,6 @@ describe('Dialog', () => {
5958
(d: Dialog, l: Location, o: OverlayContainer) => {
6059
dialog = d;
6160
mockLocation = l as SpyLocation;
62-
overlayContainer = o;
6361
overlayContainerElement = o.getContainerElement();
6462
}));
6563

@@ -70,8 +68,6 @@ describe('Dialog', () => {
7068
testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer;
7169
});
7270

73-
afterEach(() => overlayContainer.ngOnDestroy());
74-
7571
it('should open a dialog with a component', () => {
7672
let dialogRef = dialog.openFromComponent(PizzaMsg, {
7773
viewContainerRef: testViewContainerRef

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import {CdkTableModule} from '@angular/cdk/table';
44
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
55
import {CommonModule} from '@angular/common';
66
import {Component, Directive, ElementRef, ViewChild} from '@angular/core';
7-
import {ComponentFixture, fakeAsync, flush, TestBed, tick, inject} from '@angular/core/testing';
7+
import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';
88
import {FormsModule, NgForm} from '@angular/forms';
99
import {BidiModule, Direction} from '@angular/cdk/bidi';
10-
import {OverlayContainer} from '@angular/cdk/overlay';
1110
import {BehaviorSubject} from 'rxjs';
1211

1312
import {
@@ -371,29 +370,18 @@ describe('CDK Popover Edit', () => {
371370
describe(label, () => {
372371
let component: BaseTestComponent;
373372
let fixture: ComponentFixture<BaseTestComponent>;
374-
let overlayContainer: OverlayContainer;
375373

376374
beforeEach(fakeAsync(() => {
377375
TestBed.configureTestingModule({
378376
imports: [CdkTableModule, CdkPopoverEditModule, CommonModule, FormsModule, BidiModule],
379377
declarations: [componentClass],
380378
}).compileComponents();
381-
inject([OverlayContainer], (oc: OverlayContainer) => {
382-
overlayContainer = oc;
383-
})();
384379
fixture = TestBed.createComponent<BaseTestComponent>(componentClass);
385380
component = fixture.componentInstance;
386381
fixture.detectChanges();
387382
tick(10);
388383
}));
389384

390-
afterEach(() => {
391-
// The overlay container's `ngOnDestroy` won't be called between test runs so we need
392-
// to call it ourselves, in order to avoid leaking containers between tests and potentially
393-
// throwing off `querySelector` calls.
394-
overlayContainer.ngOnDestroy();
395-
});
396-
397385
describe('row hover content', () => {
398386
it('makes the first and last rows focusable but invisible', fakeAsync(() => {
399387
const rows = component.getRows();

src/cdk/a11y/aria-describer/aria-describer.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ describe('AriaDescriber', () => {
2121
fixture.detectChanges();
2222
}
2323

24-
afterEach(() => {
25-
ariaDescriber.ngOnDestroy();
26-
});
27-
2824
it('should initialize without the message container', () => {
2925
createFixture();
3026
expect(getMessagesContainer()).toBeNull();

src/cdk/a11y/focus-monitor/focus-monitor.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,13 @@ describe('FocusMonitor', () => {
247247
tick();
248248

249249
expect(buttonElement.classList.length)
250-
251250
.withContext('button should have exactly 2 focus classes')
252251
.toBe(2);
253252

254253
focusMonitor.stopMonitoring(buttonElement);
255254
fixture.detectChanges();
256255

257256
expect(buttonElement.classList.length)
258-
259257
.withContext('button should not have any focus classes')
260258
.toBe(0);
261259
}));
@@ -266,7 +264,6 @@ describe('FocusMonitor', () => {
266264
tick();
267265

268266
expect(buttonElement.classList.length)
269-
270267
.withContext('button should have exactly 2 focus classes')
271268
.toBe(2);
272269

@@ -275,7 +272,6 @@ describe('FocusMonitor', () => {
275272
fixture.detectChanges();
276273

277274
expect(buttonElement.classList.length)
278-
279275
.withContext('button should not have any focus classes')
280276
.toBe(0);
281277
}));

src/cdk/a11y/input-modality/input-modality-detector.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ describe('InputModalityDetector', () => {
3232
detector = TestBed.inject(InputModalityDetector);
3333
}
3434

35-
afterEach(() => {
36-
detector?.ngOnDestroy();
37-
detector = undefined!;
38-
});
39-
4035
it('should do nothing on non-browser platforms', () => {
4136
setupTest(false);
4237
expect(detector.mostRecentModality).toBe(null);

src/cdk/a11y/live-announcer/live-announcer.spec.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ describe('LiveAnnouncer', () => {
2828
fixture = TestBed.createComponent(TestApp);
2929
})));
3030

31-
afterEach(() => {
32-
// In our tests we always remove the current live element, in
33-
// order to avoid having multiple announcer elements in the DOM.
34-
announcer.ngOnDestroy();
35-
});
36-
3731
it('should correctly update the announce text', fakeAsync(() => {
3832
let buttonElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
3933
buttonElement.click();
@@ -116,7 +110,6 @@ describe('LiveAnnouncer', () => {
116110
}));
117111

118112
it('should ensure that there is only one live element at a time', fakeAsync(() => {
119-
announcer.ngOnDestroy();
120113
fixture.destroy();
121114

122115
TestBed.resetTestingModule().configureTestingModule({
@@ -278,12 +271,6 @@ describe('CdkAriaLive', () => {
278271
flush();
279272
})));
280273

281-
afterEach(fakeAsync(() => {
282-
// In our tests we always remove the current live element, in
283-
// order to avoid having multiple announcer elements in the DOM.
284-
announcer.ngOnDestroy();
285-
}));
286-
287274
it('should default politeness to polite', fakeAsync(() => {
288275
fixture.componentInstance.content = 'New content';
289276
fixture.detectChanges();

src/cdk/drag-drop/drag-drop-registry.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ describe('DragDropRegistry', () => {
2828
})();
2929
}));
3030

31-
afterEach(() => {
32-
registry.ngOnDestroy();
33-
});
34-
3531
it('should be able to start dragging an item', () => {
3632
const item = new DragItem();
3733

src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {TestBed, inject} from '@angular/core/testing';
22
import {dispatchKeyboardEvent} from '../../testing/private';
33
import {ESCAPE} from '@angular/cdk/keycodes';
44
import {Component, NgModule} from '@angular/core';
5-
import {OverlayModule, OverlayContainer, Overlay} from '../index';
5+
import {OverlayModule, Overlay} from '../index';
66
import {OverlayKeyboardDispatcher} from './overlay-keyboard-dispatcher';
77
import {ComponentPortal} from '@angular/cdk/portal';
88

@@ -22,10 +22,6 @@ describe('OverlayKeyboardDispatcher', () => {
2222
})();
2323
});
2424

25-
afterEach(inject([OverlayContainer], (overlayContainer: OverlayContainer) => {
26-
overlayContainer.ngOnDestroy();
27-
}));
28-
2925
it('should track overlays in order as they are attached and detached', () => {
3026
const overlayOne = overlay.create();
3127
const overlayTwo = overlay.create();

src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {TestBed, inject, fakeAsync} from '@angular/core/testing';
22
import {Component, NgModule} from '@angular/core';
33
import {dispatchFakeEvent, dispatchMouseEvent} from '../../testing/private';
4-
import {OverlayModule, OverlayContainer, Overlay} from '../index';
4+
import {OverlayModule, Overlay} from '../index';
55
import {OverlayOutsideClickDispatcher} from './overlay-outside-click-dispatcher';
66
import {ComponentPortal} from '@angular/cdk/portal';
77

@@ -21,10 +21,6 @@ describe('OverlayOutsideClickDispatcher', () => {
2121
})();
2222
});
2323

24-
afterEach(inject([OverlayContainer], (overlayContainer: OverlayContainer) => {
25-
overlayContainer.ngOnDestroy();
26-
}));
27-
2824
it('should track overlays in order as they are attached and detached', () => {
2925
const overlayOne = overlay.create();
3026
const overlayTwo = overlay.create();

src/cdk/overlay/fullscreen-overlay-container.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {Overlay, OverlayContainer, OverlayModule, FullscreenOverlayContainer} fr
66

77
describe('FullscreenOverlayContainer', () => {
88
let overlay: Overlay;
9-
let overlayContainer: FullscreenOverlayContainer;
109
let fullscreenListeners: Set<Function>;
1110
let fakeDocument: any;
1211

@@ -58,13 +57,11 @@ describe('FullscreenOverlayContainer', () => {
5857
}).compileComponents();
5958
}));
6059

61-
beforeEach(inject([Overlay, OverlayContainer], (o: Overlay, oc: OverlayContainer) => {
60+
beforeEach(inject([Overlay], (o: Overlay) => {
6261
overlay = o;
63-
overlayContainer = oc as FullscreenOverlayContainer;
6462
}));
6563

6664
afterEach(() => {
67-
overlayContainer.ngOnDestroy();
6865
fakeDocument = null;
6966
});
7067

src/cdk/overlay/overlay-container.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ describe('OverlayContainer', () => {
1818
overlayContainer = oc;
1919
}));
2020

21-
afterEach(() => {
22-
overlayContainer.ngOnDestroy();
23-
});
24-
2521
it('should remove the overlay container element from the DOM on destruction', () => {
2622
const fixture = TestBed.createComponent(TestComponentWithTemplatePortals);
2723
fixture.detectChanges();

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {Subject} from 'rxjs';
3737

3838
describe('Overlay directives', () => {
3939
let overlay: Overlay;
40-
let overlayContainer: OverlayContainer;
4140
let overlayContainerElement: HTMLElement;
4241
let fixture: ComponentFixture<ConnectedOverlayDirectiveTest>;
4342
let dir: {value: string};
@@ -61,15 +60,10 @@ describe('Overlay directives', () => {
6160
});
6261

6362
beforeEach(inject([OverlayContainer, Overlay], (oc: OverlayContainer, o: Overlay) => {
64-
overlayContainer = oc;
6563
overlay = o;
6664
overlayContainerElement = oc.getContainerElement();
6765
}));
6866

69-
afterEach(() => {
70-
overlayContainer.ngOnDestroy();
71-
});
72-
7367
/** Returns the current open overlay pane element. */
7468
function getPaneElement() {
7569
return overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;

src/cdk/overlay/position/global-position-strategy.spec.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {NgModule, NgZone, Component} from '@angular/core';
2-
import {TestBed, inject} from '@angular/core/testing';
2+
import {TestBed} from '@angular/core/testing';
33
import {MockNgZone} from '../../testing/private';
44
import {PortalModule, ComponentPortal} from '@angular/cdk/portal';
5-
import {OverlayModule, Overlay, OverlayConfig, OverlayRef, OverlayContainer} from '../index';
5+
import {OverlayModule, Overlay, OverlayConfig, OverlayRef} from '../index';
66

77

88
describe('GlobalPositonStrategy', () => {
@@ -16,19 +16,15 @@ describe('GlobalPositonStrategy', () => {
1616
providers: [{provide: NgZone, useFactory: () => zone = new MockNgZone()}]
1717
});
1818

19-
inject([Overlay], (o: Overlay) => {
20-
overlay = o;
21-
})();
19+
overlay = TestBed.inject(Overlay);
2220
});
2321

24-
afterEach(inject([OverlayContainer], (overlayContainer: OverlayContainer) => {
22+
afterEach(() => {
2523
if (overlayRef) {
2624
overlayRef.dispose();
2725
overlayRef = null!;
2826
}
29-
30-
overlayContainer.ngOnDestroy();
31-
}));
27+
});
3228

3329
function attachOverlay(config: OverlayConfig): OverlayRef {
3430
const portal = new ComponentPortal(BlankPortal);

src/cdk/scrolling/viewport-ruler.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ describe('ViewportRuler', () => {
2828
scrollTo(0, 0);
2929
}));
3030

31-
afterEach(() => {
32-
viewportRuler.ngOnDestroy();
33-
});
34-
3531
it('should get the viewport size', () => {
3632
let size = viewportRuler.getViewportSize();
3733
expect(size.width).toBe(window.innerWidth);

src/cdk/text-field/autofill.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ describe('AutofillMonitor', () => {
4141
}
4242
}));
4343

44-
afterEach(() => {
45-
// Call destroy to make sure we clean up all listeners.
46-
autofillMonitor.ngOnDestroy();
47-
});
48-
4944
it('should add monitored class and listener upon monitoring', () => {
5045
const inputEl = testComponent.input1.nativeElement;
5146
expect(inputEl.addEventListener).not.toHaveBeenCalled();

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import {
55
ViewChild,
66
ChangeDetectionStrategy,
77
} from '@angular/core';
8-
import {ComponentFixture, TestBed, fakeAsync, flushMicrotasks, inject} from '@angular/core/testing';
8+
import {ComponentFixture, TestBed, fakeAsync, flushMicrotasks} from '@angular/core/testing';
99
import {BidiModule} from '@angular/cdk/bidi';
1010
import {DataSource} from '@angular/cdk/collections';
1111
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
1212
import {ESCAPE} from '@angular/cdk/keycodes';
13-
import {OverlayContainer} from '@angular/cdk/overlay';
1413
import {MatTableModule} from '@angular/material/table';
1514
import {BehaviorSubject} from 'rxjs';
1615

@@ -348,7 +347,6 @@ describe('Material Popover Edit', () => {
348347
describe(label, () => {
349348
let component: BaseTestComponent;
350349
let fixture: ComponentFixture<BaseTestComponent>;
351-
let overlayContainer: OverlayContainer;
352350

353351
beforeEach(fakeAsync(() => {
354352
jasmine.addMatchers(approximateMatcher);
@@ -357,22 +355,12 @@ describe('Material Popover Edit', () => {
357355
imports: [BidiModule, MatTableModule, resizeModule],
358356
declarations: [componentClass],
359357
}).compileComponents();
360-
inject([OverlayContainer], (oc: OverlayContainer) => {
361-
overlayContainer = oc;
362-
})();
363358
fixture = TestBed.createComponent(componentClass);
364359
component = fixture.componentInstance;
365360
fixture.detectChanges();
366361
flushMicrotasks();
367362
}));
368363

369-
afterEach(() => {
370-
// The overlay container's `ngOnDestroy` won't be called between test runs so we need
371-
// to call it ourselves, in order to avoid leaking containers between tests and potentially
372-
// throwing `querySelector` calls.
373-
overlayContainer.ngOnDestroy();
374-
});
375-
376364
it('shows resize handle overlays on header row hover and while a resize handle is in use',
377365
fakeAsync(() => {
378366
expect(component.getOverlayThumbElement(0)).toBeUndefined();

0 commit comments

Comments
 (0)