1- import { Component , OnInit , ElementRef , Type } from '@angular/core' ;
2- import { TestBed } from '@angular/core/testing' ;
1+ import { Component , OnInit , ElementRef , Type , DebugElement } from '@angular/core' ;
2+ import { TestBed , ComponentFixture } from '@angular/core/testing' ;
33import { getQueriesForElement , prettyDOM , fireEvent , FireObject , FireFunction } from 'dom-testing-library' ;
44
5- import { RenderResult , RenderOptions , ComponentInput } from './models' ;
5+ import { RenderResult , RenderOptions } from './models' ;
6+ import { By } from '@angular/platform-browser' ;
67
78@Component ( { selector : 'wrapper-component' , template : '' } )
89class WrapperComponent implements OnInit {
@@ -13,10 +14,8 @@ class WrapperComponent implements OnInit {
1314 }
1415}
1516
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 > ;
1817export async function render < T > (
19- templateOrComponent : string | ComponentInput < T > ,
18+ templateOrComponent : string | Type < T > ,
2019 {
2120 detectChanges = true ,
2221 declarations = [ ] ,
@@ -25,7 +24,8 @@ export async function render<T>(
2524 schemas = [ ] ,
2625 queries,
2726 wrapper = WrapperComponent ,
28- } : RenderOptions ,
27+ componentProperties = { } ,
28+ } : RenderOptions < T > ,
2929) : Promise < RenderResult > {
3030 const isTemplate = typeof templateOrComponent === 'string' ;
3131 const testComponent = isTemplate ? [ wrapper ] : [ ] ;
@@ -38,8 +38,8 @@ export async function render<T>(
3838 } ) ;
3939
4040 const fixture = isTemplate
41- ? createWrapperComponentFixture ( wrapper , < string > templateOrComponent )
42- : createComponentFixture ( < ComponentInput < T > > templateOrComponent ) ;
41+ ? createWrapperComponentFixture ( templateOrComponent as string , { wrapper, componentProperties } )
42+ : createComponentFixture ( templateOrComponent as Type < T > , { componentProperties } ) ;
4343
4444 await TestBed . compileComponents ( ) ;
4545
@@ -71,23 +71,63 @@ export async function render<T>(
7171/**
7272 * Creates the wrapper component and sets its the template to the to-be-tested component
7373 */
74- function createWrapperComponentFixture < T > ( wrapper : Type < T > , template : string ) {
74+ function createWrapperComponentFixture < T > (
75+ template : string ,
76+ {
77+ wrapper,
78+ componentProperties,
79+ } : {
80+ wrapper : RenderOptions < T > [ 'wrapper' ] ;
81+ componentProperties : RenderOptions < T > [ 'componentProperties' ] ;
82+ } ,
83+ ) : ComponentFixture < any > {
7584 TestBed . overrideComponent ( wrapper , {
7685 set : {
7786 template : template ,
7887 } ,
7988 } ) ;
80- return TestBed . createComponent ( wrapper ) ;
89+
90+ const fixture = TestBed . createComponent ( wrapper ) ;
91+ // get the component selector, e.g. <foo color="green"> and <foo> results in foo
92+ const componentSelector = template . match ( / \< ( .* ?) \ / ) || template . match ( / \< ( .* ?) \> / ) ;
93+ if ( ! componentSelector ) {
94+ throw Error ( `Template ${ template } is not valid.` ) ;
95+ }
96+
97+ const sut = fixture . debugElement . query ( By . css ( componentSelector [ 1 ] ) ) ;
98+ setComponentProperties ( sut , { componentProperties } ) ;
99+ return fixture ;
81100}
82101
83102/**
84- * Creates the components and sets its properties via the provided properties from `componentInput`
103+ * Creates the components and sets its properties
85104 */
86- function createComponentFixture < T > ( componentInput : ComponentInput < T > ) {
87- const { component, parameters = { } } = componentInput ;
105+ function createComponentFixture < T > (
106+ component : Type < T > ,
107+ {
108+ componentProperties = { } ,
109+ } : {
110+ componentProperties : RenderOptions < T > [ 'componentProperties' ] ;
111+ } ,
112+ ) : ComponentFixture < T > {
88113 const fixture = TestBed . createComponent ( component ) ;
89- for ( const key of Object . keys ( parameters ) ) {
90- fixture . componentInstance [ key ] = parameters [ key ] ;
114+ setComponentProperties ( fixture , { componentProperties } ) ;
115+ return fixture ;
116+ }
117+
118+ /**
119+ * Set the component properties
120+ */
121+ function setComponentProperties < T > (
122+ fixture : ComponentFixture < T > | DebugElement ,
123+ {
124+ componentProperties = { } ,
125+ } : {
126+ componentProperties : RenderOptions < T > [ 'componentProperties' ] ;
127+ } ,
128+ ) {
129+ for ( const key of Object . keys ( componentProperties ) ) {
130+ fixture . componentInstance [ key ] = componentProperties [ key ] ;
91131 }
92132 return fixture ;
93133}
0 commit comments