X Tutup
Skip to content

Commit e9f873a

Browse files
juliemrjelbourn
authored andcommitted
feat(testing): export useful properties from componentFixture
The component fixture returned from the test component builder now exports `nativeElement` and `componentInstance` members directly. They are also still available on the `debugElement`. See #5385
1 parent 25a2d7b commit e9f873a

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

modules/angular2/src/testing/test_component_builder.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,29 @@ import {DebugElement, DebugElement_} from 'angular2/src/core/debug/debug_element
2727
* Fixture for debugging and testing a component.
2828
*/
2929
export abstract class ComponentFixture {
30+
/**
31+
* The DebugElement associated with the root element of this component.
32+
*/
3033
debugElement: DebugElement;
3134

35+
/**
36+
* The instance of the root component class.
37+
*/
38+
componentInstance: any;
39+
40+
/**
41+
* The native element at the root of the component.
42+
*/
43+
nativeElement: any;
44+
45+
/**
46+
* Trigger a change detection cycle for the component.
47+
*/
3248
abstract detectChanges(): void;
49+
50+
/**
51+
* Trigger component destruction.
52+
*/
3353
abstract destroy(): void;
3454
}
3555

@@ -43,6 +63,8 @@ export class ComponentFixture_ extends ComponentFixture {
4363
constructor(componentRef: ComponentRef) {
4464
super();
4565
this.debugElement = new DebugElement_(internalView(<ViewRef>componentRef.hostView), 0);
66+
this.componentInstance = this.debugElement.componentInstance;
67+
this.nativeElement = this.debugElement.nativeElement;
4668
this._componentParentView = internalView(<ViewRef>componentRef.hostView);
4769
this._componentRef = componentRef;
4870
}

modules/angular2/test/testing/test_component_builder_spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function main() {
9999
tcb.createAsync(ChildComp).then((componentFixture) => {
100100
componentFixture.detectChanges();
101101

102-
expect(componentFixture.debugElement.nativeElement).toHaveText('Original Child');
102+
expect(componentFixture.nativeElement).toHaveText('Original Child');
103103
async.done();
104104
});
105105
}));
@@ -109,11 +109,11 @@ export function main() {
109109

110110
tcb.createAsync(MyIfComp).then((componentFixture) => {
111111
componentFixture.detectChanges();
112-
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf()');
112+
expect(componentFixture.nativeElement).toHaveText('MyIf()');
113113

114-
componentFixture.debugElement.componentInstance.showMore = true;
114+
componentFixture.componentInstance.showMore = true;
115115
componentFixture.detectChanges();
116-
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf(More)');
116+
expect(componentFixture.nativeElement).toHaveText('MyIf(More)');
117117

118118
async.done();
119119
});
@@ -126,7 +126,7 @@ export function main() {
126126
.createAsync(MockChildComp)
127127
.then((componentFixture) => {
128128
componentFixture.detectChanges();
129-
expect(componentFixture.debugElement.nativeElement).toHaveText('Mock');
129+
expect(componentFixture.nativeElement).toHaveText('Mock');
130130

131131
async.done();
132132
});
@@ -140,7 +140,7 @@ export function main() {
140140
.createAsync(ChildComp)
141141
.then((componentFixture) => {
142142
componentFixture.detectChanges();
143-
expect(componentFixture.debugElement.nativeElement).toHaveText('Modified Child');
143+
expect(componentFixture.nativeElement).toHaveText('Modified Child');
144144

145145
async.done();
146146
});
@@ -153,7 +153,7 @@ export function main() {
153153
.createAsync(ParentComp)
154154
.then((componentFixture) => {
155155
componentFixture.detectChanges();
156-
expect(componentFixture.debugElement.nativeElement).toHaveText('Parent(Mock)');
156+
expect(componentFixture.nativeElement).toHaveText('Parent(Mock)');
157157

158158
async.done();
159159
});
@@ -168,7 +168,7 @@ export function main() {
168168
.createAsync(ParentComp)
169169
.then((componentFixture) => {
170170
componentFixture.detectChanges();
171-
expect(componentFixture.debugElement.nativeElement)
171+
expect(componentFixture.nativeElement)
172172
.toHaveText('Parent(Original Child(ChildChild Mock))');
173173

174174
async.done();
@@ -183,7 +183,7 @@ export function main() {
183183
.createAsync(TestBindingsComp)
184184
.then((componentFixture) => {
185185
componentFixture.detectChanges();
186-
expect(componentFixture.debugElement.nativeElement)
186+
expect(componentFixture.nativeElement)
187187
.toHaveText('injected value: mocked out value');
188188
async.done();
189189
});
@@ -198,7 +198,7 @@ export function main() {
198198
.createAsync(TestViewBindingsComp)
199199
.then((componentFixture) => {
200200
componentFixture.detectChanges();
201-
expect(componentFixture.debugElement.nativeElement)
201+
expect(componentFixture.nativeElement)
202202
.toHaveText('injected value: mocked out value');
203203
async.done();
204204
});

0 commit comments

Comments
 (0)
X Tutup