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
This commit is contained in:
parent
25a2d7b5db
commit
e9f873a365
|
@ -27,9 +27,29 @@ import {DebugElement, DebugElement_} from 'angular2/src/core/debug/debug_element
|
||||||
* Fixture for debugging and testing a component.
|
* Fixture for debugging and testing a component.
|
||||||
*/
|
*/
|
||||||
export abstract class ComponentFixture {
|
export abstract class ComponentFixture {
|
||||||
|
/**
|
||||||
|
* The DebugElement associated with the root element of this component.
|
||||||
|
*/
|
||||||
debugElement: DebugElement;
|
debugElement: DebugElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The instance of the root component class.
|
||||||
|
*/
|
||||||
|
componentInstance: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The native element at the root of the component.
|
||||||
|
*/
|
||||||
|
nativeElement: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger a change detection cycle for the component.
|
||||||
|
*/
|
||||||
abstract detectChanges(): void;
|
abstract detectChanges(): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger component destruction.
|
||||||
|
*/
|
||||||
abstract destroy(): void;
|
abstract destroy(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +63,8 @@ export class ComponentFixture_ extends ComponentFixture {
|
||||||
constructor(componentRef: ComponentRef) {
|
constructor(componentRef: ComponentRef) {
|
||||||
super();
|
super();
|
||||||
this.debugElement = new DebugElement_(internalView(<ViewRef>componentRef.hostView), 0);
|
this.debugElement = new DebugElement_(internalView(<ViewRef>componentRef.hostView), 0);
|
||||||
|
this.componentInstance = this.debugElement.componentInstance;
|
||||||
|
this.nativeElement = this.debugElement.nativeElement;
|
||||||
this._componentParentView = internalView(<ViewRef>componentRef.hostView);
|
this._componentParentView = internalView(<ViewRef>componentRef.hostView);
|
||||||
this._componentRef = componentRef;
|
this._componentRef = componentRef;
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,7 @@ export function main() {
|
||||||
tcb.createAsync(ChildComp).then((componentFixture) => {
|
tcb.createAsync(ChildComp).then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
|
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Original Child');
|
expect(componentFixture.nativeElement).toHaveText('Original Child');
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
@ -109,11 +109,11 @@ export function main() {
|
||||||
|
|
||||||
tcb.createAsync(MyIfComp).then((componentFixture) => {
|
tcb.createAsync(MyIfComp).then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf()');
|
expect(componentFixture.nativeElement).toHaveText('MyIf()');
|
||||||
|
|
||||||
componentFixture.debugElement.componentInstance.showMore = true;
|
componentFixture.componentInstance.showMore = true;
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf(More)');
|
expect(componentFixture.nativeElement).toHaveText('MyIf(More)');
|
||||||
|
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -126,7 +126,7 @@ export function main() {
|
||||||
.createAsync(MockChildComp)
|
.createAsync(MockChildComp)
|
||||||
.then((componentFixture) => {
|
.then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Mock');
|
expect(componentFixture.nativeElement).toHaveText('Mock');
|
||||||
|
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -140,7 +140,7 @@ export function main() {
|
||||||
.createAsync(ChildComp)
|
.createAsync(ChildComp)
|
||||||
.then((componentFixture) => {
|
.then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Modified Child');
|
expect(componentFixture.nativeElement).toHaveText('Modified Child');
|
||||||
|
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -153,7 +153,7 @@ export function main() {
|
||||||
.createAsync(ParentComp)
|
.createAsync(ParentComp)
|
||||||
.then((componentFixture) => {
|
.then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement).toHaveText('Parent(Mock)');
|
expect(componentFixture.nativeElement).toHaveText('Parent(Mock)');
|
||||||
|
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -168,7 +168,7 @@ export function main() {
|
||||||
.createAsync(ParentComp)
|
.createAsync(ParentComp)
|
||||||
.then((componentFixture) => {
|
.then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement)
|
expect(componentFixture.nativeElement)
|
||||||
.toHaveText('Parent(Original Child(ChildChild Mock))');
|
.toHaveText('Parent(Original Child(ChildChild Mock))');
|
||||||
|
|
||||||
async.done();
|
async.done();
|
||||||
|
@ -183,7 +183,7 @@ export function main() {
|
||||||
.createAsync(TestBindingsComp)
|
.createAsync(TestBindingsComp)
|
||||||
.then((componentFixture) => {
|
.then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement)
|
expect(componentFixture.nativeElement)
|
||||||
.toHaveText('injected value: mocked out value');
|
.toHaveText('injected value: mocked out value');
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -198,7 +198,7 @@ export function main() {
|
||||||
.createAsync(TestViewBindingsComp)
|
.createAsync(TestViewBindingsComp)
|
||||||
.then((componentFixture) => {
|
.then((componentFixture) => {
|
||||||
componentFixture.detectChanges();
|
componentFixture.detectChanges();
|
||||||
expect(componentFixture.debugElement.nativeElement)
|
expect(componentFixture.nativeElement)
|
||||||
.toHaveText('injected value: mocked out value');
|
.toHaveText('injected value: mocked out value');
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue