1- import { Component , OnInit , ElementRef } from '@angular/core' ;
1+ import { Component , OnInit , ElementRef , Type } from '@angular/core' ;
22import { TestBed } from '@angular/core/testing' ;
33import { getQueriesForElement , prettyDOM , fireEvent , FireObject , FireFunction } from 'dom-testing-library' ;
44
5- import { RenderResult , Options , ComponentInput } from './models' ;
5+ import { RenderResult , RenderOptions , ComponentInput } from './models' ;
66
7- @Component ( { selector : 'test -component' , template : '' } )
8- class TestComponent implements OnInit {
7+ @Component ( { selector : 'wrapper -component' , template : '' } )
8+ class WrapperComponent implements OnInit {
99 constructor ( private elemtRef : ElementRef ) { }
1010
1111 ngOnInit ( ) {
1212 this . elemtRef . nativeElement . removeAttribute ( 'ng-version' ) ;
1313 }
1414}
1515
16- export async function render < T > ( template : string , options : Options ) : Promise < RenderResult > ;
17- export async function render < T > ( component : ComponentInput < T > , options : Options ) : Promise < RenderResult > ;
16+ export async function render < T > ( template : string , renderOptions : RenderOptions ) : Promise < RenderResult > ;
17+ export async function render < T > ( component : ComponentInput < T > , renderOptions : RenderOptions ) : Promise < RenderResult > ;
1818export async function render < T > (
1919 templateOrComponent : string | ComponentInput < T > ,
20- { detectChanges = true , declarations = [ ] , providers = [ ] , imports = [ ] , schemas = [ ] } : Options ,
20+ {
21+ detectChanges = true ,
22+ declarations = [ ] ,
23+ imports = [ ] ,
24+ providers = [ ] ,
25+ schemas = [ ] ,
26+ queries,
27+ wrapper = WrapperComponent ,
28+ } : RenderOptions ,
2129) : Promise < RenderResult > {
2230 const isTemplate = typeof templateOrComponent === 'string' ;
23- const testComponent = isTemplate ? [ TestComponent ] : [ ] ;
31+ const testComponent = isTemplate ? [ wrapper ] : [ ] ;
2432
2533 TestBed . configureTestingModule ( {
2634 declarations : [ ...declarations , ...testComponent ] ,
@@ -30,7 +38,7 @@ export async function render<T>(
3038 } ) ;
3139
3240 const fixture = isTemplate
33- ? createTestComponentFixture ( < string > templateOrComponent )
41+ ? createWrapperComponentFixture ( wrapper , < string > templateOrComponent )
3442 : createComponentFixture ( < ComponentInput < T > > templateOrComponent ) ;
3543
3644 await TestBed . compileComponents ( ) ;
@@ -55,20 +63,26 @@ export async function render<T>(
5563 fixture,
5664 container : fixture . nativeElement ,
5765 debug : ( element = fixture . nativeElement ) => console . log ( prettyDOM ( element ) ) ,
58- ...getQueriesForElement ( fixture . nativeElement ) ,
66+ ...getQueriesForElement ( fixture . nativeElement , queries ) ,
5967 ...eventsWithDetectChanges ,
6068 } as any ;
6169}
6270
63- function createTestComponentFixture ( template : string ) {
64- TestBed . overrideComponent ( TestComponent , {
71+ /**
72+ * Creates the wrapper component and sets its the template to the to-be-tested component
73+ */
74+ function createWrapperComponentFixture < T > ( wrapper : Type < T > , template : string ) {
75+ TestBed . overrideComponent ( wrapper , {
6576 set : {
6677 template : template ,
6778 } ,
6879 } ) ;
69- return TestBed . createComponent ( TestComponent ) ;
80+ return TestBed . createComponent ( wrapper ) ;
7081}
7182
83+ /**
84+ * Creates the components and sets its properties via the provided properties from `componentInput`
85+ */
7286function createComponentFixture < T > ( componentInput : ComponentInput < T > ) {
7387 const { component, parameters = { } } = componentInput ;
7488 const fixture = TestBed . createComponent ( component ) ;
0 commit comments