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' ;
3
3
import { getQueriesForElement , prettyDOM , fireEvent , FireObject , FireFunction } from 'dom-testing-library' ;
4
4
5
- import { RenderResult , RenderOptions , ComponentInput } from './models' ;
5
+ import { RenderResult , RenderOptions } from './models' ;
6
+ import { By } from '@angular/platform-browser' ;
6
7
7
8
@Component ( { selector : 'wrapper-component' , template : '' } )
8
9
class WrapperComponent implements OnInit {
@@ -13,10 +14,8 @@ class WrapperComponent implements OnInit {
13
14
}
14
15
}
15
16
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 > ;
18
17
export async function render < T > (
19
- templateOrComponent : string | ComponentInput < T > ,
18
+ templateOrComponent : string | Type < T > ,
20
19
{
21
20
detectChanges = true ,
22
21
declarations = [ ] ,
@@ -25,7 +24,8 @@ export async function render<T>(
25
24
schemas = [ ] ,
26
25
queries,
27
26
wrapper = WrapperComponent ,
28
- } : RenderOptions ,
27
+ componentProperties = { } ,
28
+ } : RenderOptions < T > ,
29
29
) : Promise < RenderResult > {
30
30
const isTemplate = typeof templateOrComponent === 'string' ;
31
31
const testComponent = isTemplate ? [ wrapper ] : [ ] ;
@@ -38,8 +38,8 @@ export async function render<T>(
38
38
} ) ;
39
39
40
40
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 } ) ;
43
43
44
44
await TestBed . compileComponents ( ) ;
45
45
@@ -71,23 +71,63 @@ export async function render<T>(
71
71
/**
72
72
* Creates the wrapper component and sets its the template to the to-be-tested component
73
73
*/
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 > {
75
84
TestBed . overrideComponent ( wrapper , {
76
85
set : {
77
86
template : template ,
78
87
} ,
79
88
} ) ;
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 ;
81
100
}
82
101
83
102
/**
84
- * Creates the components and sets its properties via the provided properties from `componentInput`
103
+ * Creates the components and sets its properties
85
104
*/
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 > {
88
113
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 ] ;
91
131
}
92
132
return fixture ;
93
133
}
0 commit comments