Skip to content

Commit 7ce3772

Browse files
committed
feat: remove angular attributes added to the container
BREAKING CHANGE: Angular adds the attributes ng-version and id to the container, these will be removed This behavior can be disabled with the removeAngularAttributes property ` s await render(HelloComponent, { removeAngularAttributes: false, }); `
1 parent 3ac06b4 commit 7ce3772

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
263263
* })
264264
*/
265265
routes?: Routes;
266+
267+
/**
268+
* @description
269+
* Removes the Angular attributes (ng-version, and id) from the fixture.
270+
*
271+
* @default
272+
* `false`
273+
*
274+
* @example
275+
* const component = await render(AppComponent, {
276+
* removeAngularAttributes: true
277+
* })
278+
*/
279+
removeAngularAttributes?: boolean;
266280
}
267281

268282
export interface RenderDirectiveOptions<DirectiveType, WrapperType, Q extends Queries = typeof queries>

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ import { RenderComponentOptions, RenderDirectiveOptions, RenderResult } from './
1818
import { createSelectOptions, createType } from './user-events';
1919

2020
@Component({ selector: 'wrapper-component', template: '' })
21-
class WrapperComponent implements OnInit {
22-
constructor(private elementRef: ElementRef) {}
23-
24-
ngOnInit() {
25-
this.elementRef.nativeElement.removeAttribute('ng-version');
26-
}
27-
}
21+
class WrapperComponent {}
2822

2923
export async function render<ComponentType>(
3024
component: Type<ComponentType>,
@@ -52,6 +46,7 @@ export async function render<SutType, WrapperType = SutType>(
5246
componentProviders = [],
5347
excludeComponentDeclaration = false,
5448
routes,
49+
removeAngularAttributes = true,
5550
} = renderOptions as RenderDirectiveOptions<SutType, WrapperType>;
5651

5752
TestBed.configureTestingModule({
@@ -73,6 +68,14 @@ export async function render<SutType, WrapperType = SutType>(
7368
const fixture = createComponentFixture(sut, { template, wrapper });
7469
setComponentProperties(fixture, { componentProperties });
7570

71+
if (removeAngularAttributes) {
72+
fixture.nativeElement.removeAttribute('ng-version');
73+
const idAttribute = fixture.nativeElement.getAttribute('id');
74+
if (idAttribute && idAttribute.startsWith('root')) {
75+
fixture.nativeElement.removeAttribute('id');
76+
}
77+
}
78+
7679
await TestBed.compileComponents();
7780

7881
let isAlive = true;

projects/testing-library/tests/directive.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,33 @@ test('overrides output properties', async () => {
7777
component.click(component.getByText('on'));
7878
expect(clicked).toHaveBeenCalledWith('off');
7979
});
80+
81+
test('should remove angular attributes', async () => {
82+
await render(OnOffDirective, {
83+
template: '<div onOff (clicked)="clicked($event)"></div>',
84+
});
85+
86+
expect(document.querySelector('[ng-version]')).toBeNull();
87+
expect(document.querySelector('[id]')).toBeNull();
88+
});
89+
90+
describe('removeAngularAttributes', () => {
91+
test('should remove angular attributes', async () => {
92+
await render(OnOffDirective, {
93+
template: '<div onOff (clicked)="clicked($event)"></div>',
94+
});
95+
96+
expect(document.querySelector('[ng-version]')).toBeNull();
97+
expect(document.querySelector('[id]')).toBeNull();
98+
});
99+
100+
test('can be disabled', async () => {
101+
await render(OnOffDirective, {
102+
template: '<div onOff (clicked)="clicked($event)"></div>',
103+
removeAngularAttributes: false,
104+
});
105+
106+
expect(document.querySelector('[ng-version]')).not.toBeNull();
107+
expect(document.querySelector('[id]')).not.toBeNull();
108+
});
109+
});

projects/testing-library/tests/render.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ test('creates queries and events', async () => {
2020
component.click(component.getByText('button'));
2121
});
2222

23+
describe('removeAngularAttributes', () => {
24+
test('should remove angular attribute', async () => {
25+
await render(FixtureComponent);
26+
27+
expect(document.querySelector('[ng-version]')).toBeNull();
28+
expect(document.querySelector('[id]')).toBeNull();
29+
});
30+
31+
test('can be disabled', async () => {
32+
await render(FixtureComponent, {
33+
removeAngularAttributes: false,
34+
});
35+
36+
expect(document.querySelector('[ng-version]')).not.toBeNull();
37+
expect(document.querySelector('[id]')).not.toBeNull();
38+
});
39+
});
40+
2341
@NgModule({
2442
declarations: [FixtureComponent],
2543
})

0 commit comments

Comments
 (0)