1
- import { Component , OnInit , ElementRef } from '@angular/core' ;
1
+ import { Component , OnInit , ElementRef , Type } from '@angular/core' ;
2
2
import { TestBed } from '@angular/core/testing' ;
3
3
import { getQueriesForElement , prettyDOM , fireEvent , FireObject , FireFunction } from 'dom-testing-library' ;
4
4
5
- import { RenderResult , Options , ComponentInput } from './models' ;
5
+ import { RenderResult , RenderOptions , ComponentInput } from './models' ;
6
6
7
- @Component ( { selector : 'test -component' , template : '' } )
8
- class TestComponent implements OnInit {
7
+ @Component ( { selector : 'wrapper -component' , template : '' } )
8
+ class WrapperComponent implements OnInit {
9
9
constructor ( private elemtRef : ElementRef ) { }
10
10
11
11
ngOnInit ( ) {
12
12
this . elemtRef . nativeElement . removeAttribute ( 'ng-version' ) ;
13
13
}
14
14
}
15
15
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 > ;
18
18
export async function render < T > (
19
19
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 ,
21
29
) : Promise < RenderResult > {
22
30
const isTemplate = typeof templateOrComponent === 'string' ;
23
- const testComponent = isTemplate ? [ TestComponent ] : [ ] ;
31
+ const testComponent = isTemplate ? [ wrapper ] : [ ] ;
24
32
25
33
TestBed . configureTestingModule ( {
26
34
declarations : [ ...declarations , ...testComponent ] ,
@@ -30,7 +38,7 @@ export async function render<T>(
30
38
} ) ;
31
39
32
40
const fixture = isTemplate
33
- ? createTestComponentFixture ( < string > templateOrComponent )
41
+ ? createWrapperComponentFixture ( wrapper , < string > templateOrComponent )
34
42
: createComponentFixture ( < ComponentInput < T > > templateOrComponent ) ;
35
43
36
44
await TestBed . compileComponents ( ) ;
@@ -55,20 +63,26 @@ export async function render<T>(
55
63
fixture,
56
64
container : fixture . nativeElement ,
57
65
debug : ( element = fixture . nativeElement ) => console . log ( prettyDOM ( element ) ) ,
58
- ...getQueriesForElement ( fixture . nativeElement ) ,
66
+ ...getQueriesForElement ( fixture . nativeElement , queries ) ,
59
67
...eventsWithDetectChanges ,
60
68
} as any ;
61
69
}
62
70
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 , {
65
76
set : {
66
77
template : template ,
67
78
} ,
68
79
} ) ;
69
- return TestBed . createComponent ( TestComponent ) ;
80
+ return TestBed . createComponent ( wrapper ) ;
70
81
}
71
82
83
+ /**
84
+ * Creates the components and sets its properties via the provided properties from `componentInput`
85
+ */
72
86
function createComponentFixture < T > ( componentInput : ComponentInput < T > ) {
73
87
const { component, parameters = { } } = componentInput ;
74
88
const fixture = TestBed . createComponent ( component ) ;
0 commit comments