Skip to content

Commit 3e8bf9e

Browse files
crisbetojelbourn
authored andcommitted
test: clean up dom insertions in tests (#17730)
Fixes a few DOM insertion cases that I noticed while looking through some tests. E.g. in all the chips tests we were appending the element to the DOM ourselves and then removing it, even though Angular already inserted it in the DOM for us. Also fixes a few cases where we were inserting elements in tests, but not cleaning them up.
1 parent d2ea0b8 commit 3e8bf9e

File tree

8 files changed

+15
-38
lines changed

8 files changed

+15
-38
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ describe('AriaDescriber', () => {
185185

186186
// Use `querySelectorAll` with an attribute since `getElementById` will stop at the first match.
187187
expect(document.querySelectorAll(`[id='${MESSAGES_CONTAINER_ID}']`).length).toBe(1);
188+
189+
if (extraContainer.parentNode) {
190+
extraContainer.parentNode.removeChild(extraContainer);
191+
}
188192
});
189193

190194
it('should not describe messages that match up with the aria-label of the element', () => {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ describe('LiveAnnouncer', () => {
138138

139139
expect(document.body.querySelectorAll('.cdk-live-announcer-element').length)
140140
.toBe(1, 'Expected only one live announcer element in the DOM.');
141+
142+
if (extraElement.parentNode) {
143+
extraElement.parentNode.removeChild(extraElement);
144+
}
141145
}));
142146

143147
it('should clear any previous timers when a new one is started', fakeAsync(() => {

src/cdk/clipboard/clipboard.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ describe('Clipboard', () => {
2828
});
2929

3030
afterEach(() => {
31-
focusedInput.remove();
31+
if (focusedInput.parentNode) {
32+
focusedInput.parentNode.removeChild(focusedInput);
33+
}
3234
});
3335

3436
describe('#beginCopy', () => {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ describe('OverlayContainer', () => {
5959
document.body.appendChild(extraContainer);
6060

6161
overlayContainer.getContainerElement();
62-
6362
expect(document.querySelectorAll('.cdk-overlay-container').length).toBe(1);
63+
64+
if (extraContainer.parentNode) {
65+
extraContainer.parentNode.removeChild(extraContainer);
66+
}
6467
});
6568
});
6669

src/material-experimental/mdc-chips/chip-option.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ describe('MDC-based Option Chips', () => {
5252
chipNativeElement = chipDebugElement.nativeElement;
5353
chipInstance = chipDebugElement.injector.get<MatChipOption>(MatChipOption);
5454
testComponent = fixture.debugElement.componentInstance;
55-
56-
document.body.appendChild(chipNativeElement);
57-
});
58-
59-
afterEach(() => {
60-
document.body.removeChild(chipNativeElement);
6155
});
6256

6357
describe('basic behaviors', () => {

src/material-experimental/mdc-chips/chip-row.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ describe('MDC-based Row Chips', () => {
5050
chipNativeElement = chipDebugElement.nativeElement;
5151
chipInstance = chipDebugElement.injector.get<MatChipRow>(MatChipRow);
5252
testComponent = fixture.debugElement.componentInstance;
53-
54-
document.body.appendChild(chipNativeElement);
55-
});
56-
57-
afterEach(() => {
58-
document.body.removeChild(chipNativeElement);
5953
});
6054

6155
describe('basic behaviors', () => {

src/material-experimental/mdc-chips/chip.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ describe('MDC-based MatChip', () => {
4343
chipDebugElement = fixture.debugElement.query(By.directive(MatChip))!;
4444
chipNativeElement = chipDebugElement.nativeElement;
4545
chipInstance = chipDebugElement.injector.get<MatChip>(MatChip);
46-
47-
document.body.appendChild(chipNativeElement);
48-
});
49-
50-
afterEach(() => {
51-
document.body.removeChild(chipNativeElement);
5246
});
5347

5448
it('adds the `mat-mdc-basic-chip` class', () => {
@@ -67,12 +61,6 @@ describe('MDC-based MatChip', () => {
6761
chipNativeElement = chipDebugElement.nativeElement;
6862
chipInstance = chipDebugElement.injector.get<MatChip>(MatChip);
6963
testComponent = fixture.debugElement.componentInstance;
70-
71-
document.body.appendChild(chipNativeElement);
72-
});
73-
74-
afterEach(() => {
75-
document.body.removeChild(chipNativeElement);
7664
});
7765

7866
it('adds the `mat-chip` class', () => {

src/material/chips/chip.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ describe('MatChip', () => {
4444
chipDebugElement = fixture.debugElement.query(By.directive(MatChip))!;
4545
chipNativeElement = chipDebugElement.nativeElement;
4646
chipInstance = chipDebugElement.injector.get<MatChip>(MatChip);
47-
48-
document.body.appendChild(chipNativeElement);
49-
});
50-
51-
afterEach(() => {
52-
document.body.removeChild(chipNativeElement);
5347
});
5448

5549
it('adds the `mat-basic-chip` class', () => {
@@ -69,12 +63,6 @@ describe('MatChip', () => {
6963
chipNativeElement = chipDebugElement.nativeElement;
7064
chipInstance = chipDebugElement.injector.get<MatChip>(MatChip);
7165
testComponent = fixture.debugElement.componentInstance;
72-
73-
document.body.appendChild(chipNativeElement);
74-
});
75-
76-
afterEach(() => {
77-
document.body.removeChild(chipNativeElement);
7866
});
7967

8068
describe('basic behaviors', () => {

0 commit comments

Comments
 (0)