Skip to content

Commit a5ad0c8

Browse files
feat: add config for testing with zoneless Angular (#532)
1 parent 59f73ae commit a5ad0c8

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import { Config } from './models';
33
let config: Config = {
44
dom: {},
55
defaultImports: [],
6+
zoneless: false,
67
};
78

89
export function configure(newConfig: Partial<Config> | ((config: Partial<Config>) => Partial<Config>)) {
910
if (typeof newConfig === 'function') {
10-
// Pass the existing config out to the provided function
11-
// and accept a delta in return
1211
newConfig = newConfig(config);
1312
}
1413

15-
// Merge the incoming config delta
1614
config = {
1715
...config,
1816
...newConfig,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,11 @@ export interface Config extends Pick<RenderComponentOptions<any>, 'excludeCompon
497497
* Imports that are added to the imports
498498
*/
499499
defaultImports: any[];
500+
/**
501+
* Set to `true` to use zoneless change detection.
502+
* This automatically adds `provideZonelessChangeDetection` to the default imports.
503+
*
504+
* @default false
505+
*/
506+
zoneless?: boolean;
500507
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SimpleChanges,
1111
Type,
1212
isStandalone,
13+
provideZonelessChangeDetection,
1314
} from '@angular/core';
1415
import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed, tick } from '@angular/core/testing';
1516
import { NavigationExtras, Router } from '@angular/router';
@@ -78,6 +79,7 @@ export async function render<SutType, WrapperType = SutType>(
7879
initialRoute = '',
7980
deferBlockStates = undefined,
8081
deferBlockBehavior = undefined,
82+
zoneless = false,
8183
configureTestBed = () => {
8284
/* noop*/
8385
},
@@ -105,6 +107,7 @@ export async function render<SutType, WrapperType = SutType>(
105107
imports: addAutoImports(sut, {
106108
imports: imports.concat(defaultImports),
107109
routes,
110+
zoneless,
108111
}),
109112
providers: [...providers],
110113
schemas: [...schemas],
@@ -510,11 +513,16 @@ function addAutoDeclarations<SutType>(
510513

511514
function addAutoImports<SutType>(
512515
sut: Type<SutType> | string,
513-
{ imports = [], routes }: Pick<RenderComponentOptions<any>, 'imports' | 'routes'>,
516+
{
517+
imports = [],
518+
routes,
519+
zoneless,
520+
}: Pick<RenderComponentOptions<any>, 'imports' | 'routes'> & Pick<Config, 'zoneless'>,
514521
) {
515522
const routing = () => (routes ? [RouterTestingModule.withRoutes(routes)] : []);
516523
const components = () => (typeof sut !== 'string' && isStandalone(sut) ? [sut] : []);
517-
return [...imports, ...components(), ...routing()];
524+
const provideZoneless = () => (zoneless ? [provideZonelessChangeDetection()] : []);
525+
return [...imports, ...components(), ...routing(), ...provideZoneless()];
518526
}
519527

520528
async function renderDeferBlock<SutType>(

0 commit comments

Comments
 (0)