Skip to content

Commit 041068c

Browse files
feat: add configureTestBed callback method (#403)
1 parent 5cab519 commit 041068c

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

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

+32-18
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

@@ -74,7 +74,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
7474
* true
7575
*
7676
* @example
77-
* const component = await render(AppComponent, {
77+
* await render(AppComponent, {
7878
* autoDetectChanges: false
7979
* })
8080
*/
@@ -87,7 +87,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
8787
* true
8888
*
8989
* @example
90-
* const component = await render(AppComponent, {
90+
* await render(AppComponent, {
9191
* detectChangesOnRender: false
9292
* })
9393
*/
@@ -103,7 +103,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
103103
* []
104104
*
105105
* @example
106-
* const component = await render(AppComponent, {
106+
* await render(AppComponent, {
107107
* declarations: [ CustomerDetailComponent, ButtonComponent ]
108108
* })
109109
*/
@@ -118,7 +118,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
118118
* []
119119
*
120120
* @example
121-
* const component = await render(AppComponent, {
121+
* await render(AppComponent, {
122122
* providers: [
123123
* CustomersService,
124124
* {
@@ -140,7 +140,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
140140
* `[NoopAnimationsModule]`
141141
*
142142
* @example
143-
* const component = await render(AppComponent, {
143+
* await render(AppComponent, {
144144
* imports: [
145145
* AppSharedModule,
146146
* MaterialModule,
@@ -159,7 +159,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
159159
* []
160160
*
161161
* @example
162-
* const component = await render(AppComponent, {
162+
* await render(AppComponent, {
163163
* schemas: [
164164
* NO_ERRORS_SCHEMA,
165165
* ]
@@ -174,7 +174,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
174174
* {}
175175
*
176176
* @example
177-
* const component = await render(AppComponent, {
177+
* await render(AppComponent, {
178178
* componentProperties: {
179179
* counterValue: 10,
180180
* send: (value) => { ... }
@@ -190,7 +190,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
190190
* {}
191191
*
192192
* @example
193-
* const component = await render(AppComponent, {
193+
* await render(AppComponent, {
194194
* componentInputs: {
195195
* counterValue: 10
196196
* }
@@ -206,7 +206,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
206206
*
207207
* @example
208208
* const sendValue = (value) => { ... }
209-
* const component = await render(AppComponent, {
209+
* await render(AppComponent, {
210210
* componentOutputs: {
211211
* send: {
212212
* emit: sendValue
@@ -225,7 +225,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
225225
* []
226226
*
227227
* @example
228-
* const component = await render(AppComponent, {
228+
* await render(AppComponent, {
229229
* componentProviders: [
230230
* AppComponentService
231231
* ]
@@ -259,7 +259,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
259259
* undefined
260260
*
261261
* @example
262-
* const component = await render(AppComponent, {
262+
* await render(AppComponent, {
263263
* componentImports: [
264264
* MockChildComponent
265265
* ]
@@ -277,7 +277,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
277277
* import * as customQueries from 'custom-queries'
278278
* import { queries } from '@testing-library/angular'
279279
*
280-
* const component = await render(AppComponent, {
280+
* await render(AppComponent, {
281281
* queries: { ...queries, ...customQueries }
282282
* })
283283
*/
@@ -291,7 +291,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
291291
* false
292292
*
293293
* @example
294-
* const component = await render(AppComponent, {
294+
* await render(AppComponent, {
295295
* imports: [AppModule], // a module that includes AppComponent
296296
* excludeComponentDeclaration: true
297297
* })
@@ -304,7 +304,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
304304
* For more info see https://angular.io/api/router/Routes.
305305
*
306306
* @example
307-
* const component = await render(AppComponent, {
307+
* await render(AppComponent, {
308308
* declarations: [ChildComponent],
309309
* routes: [
310310
* {
@@ -326,7 +326,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
326326
* Specifies which route should be initially navigated to
327327
*
328328
* @example
329-
* const component = await render(AppComponent, {
329+
* await render(AppComponent, {
330330
* initialRoute: 'myroute',
331331
* routes: [
332332
* { path: '', component: HomeComponent },
@@ -344,11 +344,25 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
344344
* `false`
345345
*
346346
* @example
347-
* const component = await render(AppComponent, {
347+
* await render(AppComponent, {
348348
* removeAngularAttributes: true
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+
* await render(AppComponent, {
362+
* configureTestBed: (testBed) => { }
363+
* })
364+
*/
365+
configureTestBed?: (testbed: TestBed) => void;
352366
}
353367

354368
export interface ComponentOverride<T> {
@@ -368,7 +382,7 @@ export interface RenderTemplateOptions<WrapperType, Properties extends object =
368382
* `WrapperComponent`, an empty component that strips the `ng-version` attribute
369383
*
370384
* @example
371-
* const component = await render(`<div spoiler message='SPOILER'></div>`, {
385+
* await render(`<div spoiler message='SPOILER'></div>`, {
372386
* declarations: [SpoilerDirective]
373387
* wrapper: CustomWrapperComponent
374388
* })

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)