From 4fc53d1087dc8b3979133fd3e05c4ff1af73240c Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Mon, 13 Nov 2023 19:58:12 +0100 Subject: [PATCH 1/2] fix: cleanup components without id --- .../src/lib/testing-library.ts | 3 +++ ...ssue-398-component-without-host-id.spec.ts | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 projects/testing-library/tests/issues/issue-398-component-without-host-id.spec.ts diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index 48af2d5..61df094 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -492,7 +492,10 @@ function cleanupAtFixture(fixture: ComponentFixture) { if (!fixture.nativeElement.getAttribute('ng-version') && fixture.nativeElement.parentNode === document.body) { document.body.removeChild(fixture.nativeElement); + } else if (!fixture.nativeElement.getAttribute('id') && document.body.children[0] === fixture.nativeElement) { + document.body.removeChild(fixture.nativeElement); } + mountedFixtures.delete(fixture); } diff --git a/projects/testing-library/tests/issues/issue-398-component-without-host-id.spec.ts b/projects/testing-library/tests/issues/issue-398-component-without-host-id.spec.ts new file mode 100644 index 0000000..4508d64 --- /dev/null +++ b/projects/testing-library/tests/issues/issue-398-component-without-host-id.spec.ts @@ -0,0 +1,24 @@ +import { Component } from '@angular/core'; +import { render, screen } from '../../src/public_api'; + +test('should create the app', async () => { + await render(FixtureComponent); + expect(screen.getByRole('heading')).toBeInTheDocument(); +}); + +test('should re-create the app', async () => { + await render(FixtureComponent); + expect(screen.getByRole('heading')).toBeInTheDocument(); +}); + +@Component({ + selector: 'atl-fixture', + standalone: true, + template: '

My title

', + // eslint-disable-next-line @angular-eslint/no-host-metadata-property + host: { + // eslint-disable-next-line @typescript-eslint/naming-convention + '[attr.id]': 'null', // this breaks the cleaning up of tests + }, +}) +class FixtureComponent {} From 93c38346edefaccc729cdf9e58e95093de0ad84f Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Mon, 13 Nov 2023 19:59:48 +0100 Subject: [PATCH 2/2] better safe than sorry --- projects/testing-library/src/lib/testing-library.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index 61df094..65932fc 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -492,7 +492,7 @@ function cleanupAtFixture(fixture: ComponentFixture) { if (!fixture.nativeElement.getAttribute('ng-version') && fixture.nativeElement.parentNode === document.body) { document.body.removeChild(fixture.nativeElement); - } else if (!fixture.nativeElement.getAttribute('id') && document.body.children[0] === fixture.nativeElement) { + } else if (!fixture.nativeElement.getAttribute('id') && document.body.children?.[0] === fixture.nativeElement) { document.body.removeChild(fixture.nativeElement); }