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

0 commit comments

Comments
 (0)