component-test-helper.ts
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Component } from "ng-forward";
import { TestComponentBuilder, ngClass } from 'ng-forward/cjs/testing/test-component-builder';
import { INgForwardJQuery } from "ng-forward/cjs/util/jqlite-extensions";
import { ComponentFixture } from 'ng-forward/cjs/testing/test-component-builder';
/**
* @ngdoc object
* @name spec.ComponentTestHelper
* @description
*
* Helper class for creating tests. It encapsulates the TestComponentBuilder initialization,
* allowing the test to be DRY. To use, one must declare a beforeEach function in the
* test, and inside construct this object like:
*
* <pre>
* let helper = let helper : ComponentTestHelper;
* beforeEach( (done) => {
* helper = new ComponentTestHelper(cls, tcb);
* }
* </pre>
*/
export class ComponentTestHelper<T extends any> {
/**
* @ngdoc property
* @name mockComponent
* @propertyOf spec.ComponentTestHelper
* @description
* The component we are mocking.
*/
mockComponent: ngClass;
/**
* @ngdoc property
* @name tcb
* @propertyOf spec.ComponentTestHelper
* @description
* The NgForward TestComponentBuilder
*/
tcb: TestComponentBuilder;
/**
* @ngdoc property
* @name component
* @propertyOf spec.ComponentTestHelper
* @description
* The parsed component instance
*/
component: T;
/**
* @ngdoc property
* @name debugElement
* @propertyOf spec.ComponentTestHelper
* @description
* The debugElement representing a JQuery element attached to the component
* on mock page.
*/
debugElement: INgForwardJQuery;
/**
* @ngdoc method
* @name constructor
* @methodOf spec.ComponentTestHelper
* @description
* The constructor for this component.
*/
constructor(mockComponent: ngClass, done: Function) {
this.mockComponent = mockComponent;
this.tcb = new TestComponentBuilder();
this.init(done);
}
/**
* @ngdoc method
* @name init
* @methodOf spec.ComponentTestHelper
* @description
* The initializer function. It is called inside the constructor
*/
init(done: Function): any {
let promisse = this.tcb.createAsync(this.mockComponent) as Promise<ComponentFixture>;
return promisse.then((fixture: any) => {
// Fire all angular events and parsing
fixture.detectChanges();
// The main debug element
this.debugElement = fixture.debugElement;
this.component = <T>this.debugElement.componentViewChildren[0].componentInstance;
let mockObj = new this.mockComponent();
Object.keys(mockObj).forEach((key: any) => {
(<any>this.component)[key] = <any>mockObj[key];
});
}).then(() => {
// Force the resolution of components and sync
done();
});
}
/**
* @ngdoc method
* @name all
* @methodOf spec.ComponentTestHelper
* @description
* Return all elements matching the given selector
*/
all(selector: string): INgForwardJQuery[] {
return this.debugElement.queryAll(selector);
}
/**
* @ngdoc method
* @name find
* @methodOf spec.ComponentTestHelper
* @description
* Return the first element matching the given selector
*/
find(selector: string): INgForwardJQuery {
return this.all(selector)[0];
}
/**
* @ngdoc method
* @name findChildren
* @methodOf spec.ComponentTestHelper
* @description
* Return the first element of parent element that matches the given selector
*/
findChildren(parentSelector: string, childSelector: string) {
let parentComponent = this.find(parentSelector);
return parentComponent.find(childSelector)[0];
}
}
export function createClass({
template = '<div></div>',
directives = <any[]>[],
providers = <any[]>[],
properties = <any>{}
}): any {
@Component({ selector: 'component-test-helper-container', template, directives, providers })
class Test {
constructor() {
Object.keys(properties).forEach((key: any) => {
(<any>this)[key] = <any>properties[key];
});
}
}
return Test;
}