Skip to content

Commit 378258a

Browse files
committed
feat: add configureTestBed callback method
1 parent d36c2ea commit 378258a

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Type, DebugElement } from '@angular/core';
2-
import { ComponentFixture } from '@angular/core/testing';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { Routes } from '@angular/router';
44
import { BoundFunction, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';
55

@@ -349,6 +349,20 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
349349
* })
350350
*/
351351
removeAngularAttributes?: boolean;
352+
353+
/**
354+
* @description
355+
* Callback to configure the testbed before the compilation.
356+
*
357+
* @default
358+
* () => {}
359+
*
360+
* @example
361+
* const component = await render(AppComponent, {
362+
* configureTestBed: (testBed) => { }
363+
* })
364+
*/
365+
configureTestBed?: (testbed: TestBed) => void;
352366
}
353367

354368
export interface ComponentOverride<T> {

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

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export async function render<SutType, WrapperType = SutType>(
6565
removeAngularAttributes = false,
6666
defaultImports = [],
6767
initialRoute = '',
68+
configureTestBed = () => {
69+
/* noop*/
70+
},
6871
} = { ...globalConfig, ...renderOptions };
6972

7073
dtlConfigure({
@@ -94,6 +97,8 @@ export async function render<SutType, WrapperType = SutType>(
9497
overrideComponentImports(sut, componentImports);
9598
overrideChildComponentProviders(childComponentOverrides);
9699

100+
configureTestBed(TestBed);
101+
97102
await TestBed.compileComponents();
98103

99104
componentProviders
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Component } from '@angular/core';
2+
import { render, screen } from '../../src/public_api';
3+
4+
test('child', async () => {
5+
await render(FixtureComponent);
6+
expect(screen.getByText('Hello from child')).toBeInTheDocument();
7+
});
8+
9+
test('stub', async () => {
10+
await render(FixtureComponent, {
11+
componentImports: [StubComponent],
12+
});
13+
14+
expect(screen.getByText('Hello from stub')).toBeInTheDocument();
15+
});
16+
17+
test('configure', async () => {
18+
await render(FixtureComponent, {
19+
configureTestBed: (testBed) => {
20+
testBed.overrideComponent(FixtureComponent, {
21+
add: {
22+
imports: [StubComponent],
23+
},
24+
remove: {
25+
imports: [ChildComponent],
26+
},
27+
});
28+
},
29+
});
30+
31+
expect(screen.getByText('Hello from stub')).toBeInTheDocument();
32+
});
33+
34+
@Component({
35+
selector: 'atl-child',
36+
template: `Hello from child`,
37+
standalone: true,
38+
})
39+
class ChildComponent {}
40+
41+
@Component({
42+
selector: 'atl-child',
43+
template: `Hello from stub`,
44+
standalone: true,
45+
})
46+
class StubComponent {}
47+
48+
@Component({
49+
selector: 'atl-fixture',
50+
template: `<atl-child />`,
51+
standalone: true,
52+
imports: [ChildComponent],
53+
})
54+
class FixtureComponent {}

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

+11
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,14 @@ describe('initialRoute', () => {
366366
expect(screen.getByText('button')).toBeInTheDocument();
367367
});
368368
});
369+
370+
describe('configureTestBed', () => {
371+
it('invokes configureTestBed', async () => {
372+
const configureTestBedFn = jest.fn();
373+
await render(FixtureComponent, {
374+
configureTestBed: configureTestBedFn,
375+
});
376+
377+
expect(configureTestBedFn).toHaveBeenCalledTimes(1);
378+
});
379+
});

0 commit comments

Comments
 (0)