diff --git a/projects/testing-library/src/lib/models.ts b/projects/testing-library/src/lib/models.ts index eb38d972..19190ab1 100644 --- a/projects/testing-library/src/lib/models.ts +++ b/projects/testing-library/src/lib/models.ts @@ -20,4 +20,9 @@ export interface RenderOptions { componentProviders?: any[]; queries?: Q; wrapper?: Type; + /** + * Exclude the component to be automatically be added as a declaration + * This is needed when the component is declared in an imported module + */ + excludeComponentDeclaration?: boolean; } diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index a3075e10..e08f76eb 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -29,10 +29,16 @@ export async function render( wrapper = WrapperComponent, componentProperties = {}, componentProviders = [], + excludeComponentDeclaration = false, } = renderOptions; const isTemplate = typeof templateOrComponent === 'string'; - const componentDeclarations = isTemplate ? [wrapper] : [templateOrComponent]; + const componentDeclarations = declareComponents({ + templateOrComponent, + wrapper, + isTemplate, + excludeComponentDeclaration, + }); TestBed.configureTestingModule({ declarations: [...declarations, ...componentDeclarations], @@ -144,3 +150,15 @@ function setComponentProperties( } return fixture; } + +function declareComponents({ isTemplate, wrapper, excludeComponentDeclaration, templateOrComponent }) { + if (isTemplate) { + return [wrapper]; + } + + if (excludeComponentDeclaration) { + return []; + } + + return [templateOrComponent]; +} diff --git a/projects/testing-library/tests/render.spec.ts b/projects/testing-library/tests/render.spec.ts new file mode 100644 index 00000000..83b5126b --- /dev/null +++ b/projects/testing-library/tests/render.spec.ts @@ -0,0 +1,20 @@ +import { Component, ElementRef, OnInit, NgModule } from '@angular/core'; +import { render } from '../src/public_api'; + +@Component({ + selector: 'fixture', + template: ``, +}) +class FixtureComponent {} + +@NgModule({ + declarations: [FixtureComponent], +}) +export class FixtureModule {} + +test('should not throw if component is declared in an import', async () => { + await render(FixtureComponent, { + imports: [FixtureModule], + excludeComponentDeclaration: true, + }); +}); diff --git a/projects/testing-library/tests/wrapper.spec.ts b/projects/testing-library/tests/wrapper.spec.ts index f4d2210c..1fa3344f 100644 --- a/projects/testing-library/tests/wrapper.spec.ts +++ b/projects/testing-library/tests/wrapper.spec.ts @@ -1,4 +1,4 @@ -import { Component, Input, ElementRef, OnInit } from '@angular/core'; +import { Component, ElementRef, OnInit } from '@angular/core'; import { render } from '../src/public_api'; @Component({