Skip to content

Commit b43e9c0

Browse files
committed
revert cleanup changes
1 parent 333e852 commit b43e9c0

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

projects/testing-library/src/lib/models.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,20 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
451451
*/
452452
initialRoute?: string;
453453

454+
/**
455+
* @description
456+
* Removes the Angular attributes (ng-version, and root-id) from the fixture.
457+
*
458+
* @default
459+
* `false`
460+
*
461+
* @example
462+
* await render(AppComponent, {
463+
* removeAngularAttributes: true
464+
* })
465+
*/
466+
removeAngularAttributes?: boolean;
467+
454468
/**
455469
* @description
456470
* Callback to configure the testbed before the compilation.

projects/testing-library/src/lib/testing-library.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export async function render<SutType, WrapperType = SutType>(
7676
childComponentOverrides = [],
7777
componentImports,
7878
excludeComponentDeclaration = false,
79+
removeAngularAttributes = false,
7980
routes = [],
8081
defaultImports = [],
8182
initialRoute = '',
@@ -226,6 +227,14 @@ export async function render<SutType, WrapperType = SutType>(
226227
subscribedOutputs = subscribeToComponentOutputs(createdFixture, subscribeTo);
227228
}
228229

230+
if (removeAngularAttributes) {
231+
createdFixture.nativeElement.removeAttribute('ng-version');
232+
const idAttribute = createdFixture.nativeElement.getAttribute('id');
233+
if (idAttribute?.startsWith('root')) {
234+
createdFixture.nativeElement.removeAttribute('id');
235+
}
236+
}
237+
229238
mountedFixtures.add(createdFixture);
230239

231240
if (hasOnChangesHook(createdFixture.componentInstance) && Object.keys(properties).length > 0) {
@@ -619,7 +628,19 @@ async function waitForElementToBeRemovedWrapper<T>(
619628
}
620629

621630
function cleanup() {
622-
mountedFixtures.clear();
631+
mountedFixtures.forEach(cleanupAtFixture);
632+
}
633+
634+
function cleanupAtFixture(fixture: ComponentFixture<any>) {
635+
fixture.destroy();
636+
637+
if (!fixture.nativeElement.getAttribute('ng-version') && fixture.nativeElement.parentNode === document.body) {
638+
document.body.removeChild(fixture.nativeElement);
639+
} else if (!fixture.nativeElement.getAttribute('id') && document.body.children?.[0] === fixture.nativeElement) {
640+
document.body.removeChild(fixture.nativeElement);
641+
}
642+
643+
mountedFixtures.delete(fixture);
623644
}
624645

625646
// if we're running in a test runner that supports afterEach
@@ -670,6 +691,7 @@ function safeDetectChanges<T>(fixture: ComponentFixture<T>) {
670691
try {
671692
const appRef = fixture.componentRef.injector.get(ApplicationRef);
672693
if (!appRef.destroyed) {
694+
fixture.changeDetectorRef.markForCheck();
673695
fixture.detectChanges();
674696
}
675697
} catch (err: any) {

projects/testing-library/src/tests/wait-for-element-to-be-removed.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { Component, OnInit, signal } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { render, screen, waitForElementToBeRemoved } from '../public_api';
33
import { timer } from 'rxjs';
44
import { test, expect } from 'vitest';
55
import { NgIf } from '@angular/common';
66

77
@Component({
88
selector: 'atl-fixture',
9-
template: ` <div *ngIf="visible()" data-testid="im-here">👋</div> `,
9+
template: ` <div *ngIf="visible" data-testid="im-here">👋</div> `,
1010
imports: [NgIf],
1111
})
1212
class FixtureComponent implements OnInit {
13-
visible = signal(true);
13+
visible = true;
1414
ngOnInit() {
15-
timer(500).subscribe(() => this.visible.set(false));
15+
timer(500).subscribe(() => (this.visible = false));
1616
}
1717
}
1818

projects/testing-library/src/tests/wait-for.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, signal } from '@angular/core';
1+
import { Component } from '@angular/core';
22
import { timer } from 'rxjs';
33
import { test } from 'vitest';
44
import { render, screen, waitFor, fireEvent } from '../public_api';
@@ -7,14 +7,14 @@ import { render, screen, waitFor, fireEvent } from '../public_api';
77
selector: 'atl-fixture',
88
template: `
99
<button data-testid="button" (click)="load()">Load</button>
10-
<div>{{ result() }}</div>
10+
<div>{{ result }}</div>
1111
`,
1212
})
1313
class FixtureComponent {
14-
result = signal('');
14+
result = '';
1515

1616
load() {
17-
timer(500).subscribe(() => this.result.set('Success'));
17+
timer(500).subscribe(() => (this.result = 'Success'));
1818
}
1919
}
2020

0 commit comments

Comments
 (0)