From 0af6018a9c28a9b1f06664852585b6449788ca02 Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Mon, 29 Mar 2021 14:24:23 +0200 Subject: [PATCH] feat: add global excludeComponentDeclaration (#194) --- projects/testing-library/src/lib/config.ts | 2 +- projects/testing-library/src/lib/models.ts | 11 +++++++++-- projects/testing-library/src/lib/testing-library.ts | 13 ++++++++----- projects/testing-library/tests/render.spec.ts | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/projects/testing-library/src/lib/config.ts b/projects/testing-library/src/lib/config.ts index 1ac96455..bd8ee9bb 100644 --- a/projects/testing-library/src/lib/config.ts +++ b/projects/testing-library/src/lib/config.ts @@ -1,8 +1,8 @@ import { Config } from './models'; let config: Config = { - defaultImports: [], dom: {}, + defaultImports: [], }; export function configure(newConfig: Partial | ((config: Partial) => Partial)) { diff --git a/projects/testing-library/src/lib/models.ts b/projects/testing-library/src/lib/models.ts index ded4d7c2..5d9b225b 100644 --- a/projects/testing-library/src/lib/models.ts +++ b/projects/testing-library/src/lib/models.ts @@ -281,7 +281,14 @@ export interface RenderDirectiveOptions; } -export interface Config { - defaultImports: any[]; +export interface Config extends Pick, 'excludeComponentDeclaration'> { + /** + * DOM Testing Library config + * @link https://testing-library.com/docs/dom-testing-library/api-configuration/ + */ dom: Partial; + /** + * Imports that are added to the imports + */ + defaultImports: any[]; } diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index 642b86c7..62c2c0c0 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -41,6 +41,7 @@ export async function render( sut: Type, renderOptions: RenderComponentOptions | RenderDirectiveOptions = {}, ): Promise> { + const { dom: domConfig, ...globalConfig } = getConfig(); const { detectChanges: detectChangesOnRender = true, declarations = [], @@ -55,9 +56,8 @@ export async function render( excludeComponentDeclaration = false, routes, removeAngularAttributes = false, - } = renderOptions as RenderDirectiveOptions; - - const config = getConfig(); + defaultImports = [], + } = { ...globalConfig, ...renderOptions }; dtlConfigure({ eventWrapper: (cb) => { @@ -65,12 +65,15 @@ export async function render( detectChangesForMountedFixtures(); return result; }, - ...config.dom, + ...domConfig, }); TestBed.configureTestingModule({ declarations: addAutoDeclarations(sut, { declarations, excludeComponentDeclaration, template, wrapper }), - imports: addAutoImports({ imports: imports.concat(config.defaultImports), routes }), + imports: addAutoImports({ + imports: imports.concat(defaultImports), + routes, + }), providers: [...providers], schemas: [...schemas], }); diff --git a/projects/testing-library/tests/render.spec.ts b/projects/testing-library/tests/render.spec.ts index 292b97ab..01fe9f0c 100644 --- a/projects/testing-library/tests/render.spec.ts +++ b/projects/testing-library/tests/render.spec.ts @@ -55,7 +55,7 @@ describe('animationModule', () => { }) class FixtureModule {} describe('excludeComponentDeclaration', () => { - test('will throw if component is declared in an import', async () => { + test('does not throw if component is declared in an imported module', async () => { await render(FixtureComponent, { imports: [FixtureModule], excludeComponentDeclaration: true,