2016-04-12 12:40:37 -04:00
|
|
|
import {
|
2016-04-26 19:38:54 -04:00
|
|
|
OpaqueToken,
|
2016-04-12 12:40:37 -04:00
|
|
|
ComponentRef,
|
2016-04-30 13:52:04 -04:00
|
|
|
ComponentFactory,
|
|
|
|
ComponentResolver,
|
2016-04-12 12:40:37 -04:00
|
|
|
Injector,
|
|
|
|
Injectable,
|
|
|
|
ViewMetadata,
|
|
|
|
ElementRef,
|
2016-04-13 20:05:17 -04:00
|
|
|
ChangeDetectorRef,
|
2016-04-26 19:38:54 -04:00
|
|
|
NgZone,
|
2016-04-28 20:50:03 -04:00
|
|
|
NgZoneError,
|
|
|
|
DebugElement,
|
|
|
|
getDebugNode
|
|
|
|
} from '@angular/core';
|
|
|
|
import {DirectiveResolver, ViewResolver} from '../index';
|
|
|
|
|
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
2016-05-03 14:26:07 -04:00
|
|
|
import {Type, isPresent, isBlank, IS_DART, scheduleMicroTask} from '../src/facade/lang';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {PromiseWrapper, ObservableWrapper, PromiseCompleter} from '../src/facade/async';
|
|
|
|
import {ListWrapper, MapWrapper} from '../src/facade/collection';
|
|
|
|
|
|
|
|
import {tick} from '@angular/core/testing';
|
|
|
|
/**
|
|
|
|
* An abstract class for inserting the root test component element in a platform independent way.
|
|
|
|
*/
|
|
|
|
export class TestComponentRenderer {
|
|
|
|
insertRootElement(rootElementId: string) {}
|
|
|
|
}
|
2015-11-03 16:51:33 -05:00
|
|
|
|
2016-04-26 19:38:54 -04:00
|
|
|
export var ComponentFixtureAutoDetect = new OpaqueToken("ComponentFixtureAutoDetect");
|
|
|
|
export var ComponentFixtureNoNgZone = new OpaqueToken("ComponentFixtureNoNgZone");
|
|
|
|
|
2015-11-03 16:51:33 -05:00
|
|
|
/**
|
2015-12-02 14:54:10 -05:00
|
|
|
* Fixture for debugging and testing a component.
|
2015-11-03 16:51:33 -05:00
|
|
|
*/
|
2016-04-30 13:52:04 -04:00
|
|
|
export class ComponentFixture<T> {
|
2015-11-30 15:49:22 -05:00
|
|
|
/**
|
|
|
|
* The DebugElement associated with the root element of this component.
|
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
debugElement: DebugElement;
|
|
|
|
|
2015-11-30 15:49:22 -05:00
|
|
|
/**
|
|
|
|
* The instance of the root component class.
|
|
|
|
*/
|
|
|
|
componentInstance: any;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The native element at the root of the component.
|
|
|
|
*/
|
|
|
|
nativeElement: any;
|
|
|
|
|
2016-01-14 00:35:21 -05:00
|
|
|
/**
|
|
|
|
* The ElementRef for the element at the root of the component.
|
|
|
|
*/
|
|
|
|
elementRef: ElementRef;
|
|
|
|
|
2015-11-30 15:49:22 -05:00
|
|
|
/**
|
2016-04-13 20:05:17 -04:00
|
|
|
* The ComponentRef for the component
|
2015-11-30 15:49:22 -05:00
|
|
|
*/
|
2016-04-30 13:52:04 -04:00
|
|
|
componentRef: ComponentRef<T>;
|
2015-11-30 15:49:22 -05:00
|
|
|
|
|
|
|
/**
|
2016-04-13 20:05:17 -04:00
|
|
|
* The ChangeDetectorRef for the component
|
2015-11-30 15:49:22 -05:00
|
|
|
*/
|
2016-04-13 20:05:17 -04:00
|
|
|
changeDetectorRef: ChangeDetectorRef;
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2016-04-26 19:38:54 -04:00
|
|
|
/**
|
|
|
|
* The NgZone in which this component was instantiated.
|
|
|
|
*/
|
|
|
|
ngZone: NgZone;
|
|
|
|
|
|
|
|
private _autoDetect: boolean;
|
|
|
|
|
|
|
|
private _isStable: boolean = true;
|
|
|
|
private _completer: PromiseCompleter<any> = null;
|
|
|
|
private _onUnstableSubscription = null;
|
|
|
|
private _onStableSubscription = null;
|
|
|
|
private _onMicrotaskEmptySubscription = null;
|
|
|
|
private _onErrorSubscription = null;
|
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
constructor(componentRef: ComponentRef<T>, ngZone: NgZone, autoDetect: boolean) {
|
2016-04-13 20:05:17 -04:00
|
|
|
this.changeDetectorRef = componentRef.changeDetectorRef;
|
|
|
|
this.elementRef = componentRef.location;
|
|
|
|
this.debugElement = <DebugElement>getDebugNode(this.elementRef.nativeElement);
|
|
|
|
this.componentInstance = componentRef.instance;
|
|
|
|
this.nativeElement = this.elementRef.nativeElement;
|
|
|
|
this.componentRef = componentRef;
|
2016-04-26 19:38:54 -04:00
|
|
|
this.ngZone = ngZone;
|
|
|
|
this._autoDetect = autoDetect;
|
|
|
|
|
|
|
|
if (ngZone != null) {
|
|
|
|
this._onUnstableSubscription =
|
|
|
|
ObservableWrapper.subscribe(ngZone.onUnstable, (_) => { this._isStable = false; });
|
|
|
|
this._onMicrotaskEmptySubscription =
|
|
|
|
ObservableWrapper.subscribe(ngZone.onMicrotaskEmpty, (_) => {
|
|
|
|
if (this._autoDetect) {
|
|
|
|
// Do a change detection run with checkNoChanges set to true to check
|
|
|
|
// there are no changes on the second run.
|
|
|
|
this.detectChanges(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._onStableSubscription = ObservableWrapper.subscribe(ngZone.onStable, (_) => {
|
|
|
|
this._isStable = true;
|
2016-05-03 14:26:07 -04:00
|
|
|
// Check whether there are no pending macrotasks in a microtask so that ngZone gets a chance
|
|
|
|
// to update the state of pending macrotasks.
|
|
|
|
scheduleMicroTask(() => {
|
|
|
|
if (!this.ngZone.hasPendingMacrotasks) {
|
|
|
|
if (this._completer != null) {
|
|
|
|
this._completer.resolve(true);
|
|
|
|
this._completer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-04-26 19:38:54 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
this._onErrorSubscription = ObservableWrapper.subscribe(
|
|
|
|
ngZone.onError, (error: NgZoneError) => { throw error.error; });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _tick(checkNoChanges: boolean) {
|
|
|
|
this.changeDetectorRef.detectChanges();
|
|
|
|
if (checkNoChanges) {
|
|
|
|
this.checkNoChanges();
|
|
|
|
}
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
|
|
|
|
2016-04-13 20:05:17 -04:00
|
|
|
/**
|
|
|
|
* Trigger a change detection cycle for the component.
|
|
|
|
*/
|
2016-01-06 17:13:44 -05:00
|
|
|
detectChanges(checkNoChanges: boolean = true): void {
|
2016-04-26 19:38:54 -04:00
|
|
|
if (this.ngZone != null) {
|
|
|
|
// Run the change detection inside the NgZone so that any async tasks as part of the change
|
|
|
|
// detection are captured by the zone and can be waited for in isStable.
|
|
|
|
this.ngZone.run(() => { this._tick(checkNoChanges); });
|
|
|
|
} else {
|
|
|
|
// Running without zone. Just do the change detection.
|
|
|
|
this._tick(checkNoChanges);
|
2016-01-06 17:13:44 -05:00
|
|
|
}
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
|
|
|
|
2016-04-26 19:38:54 -04:00
|
|
|
/**
|
|
|
|
* Do a change detection run to make sure there were no changes.
|
|
|
|
*/
|
2016-04-13 20:05:17 -04:00
|
|
|
checkNoChanges(): void { this.changeDetectorRef.checkNoChanges(); }
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-04-26 19:38:54 -04:00
|
|
|
/**
|
|
|
|
* Set whether the fixture should autodetect changes.
|
|
|
|
*
|
|
|
|
* Also runs detectChanges once so that any existing change is detected.
|
|
|
|
*/
|
|
|
|
autoDetectChanges(autoDetect: boolean = true) {
|
|
|
|
if (this.ngZone == null) {
|
|
|
|
throw new BaseException('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set');
|
|
|
|
}
|
|
|
|
this._autoDetect = autoDetect;
|
|
|
|
this.detectChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the fixture is currently stable or has async tasks that have not been completed
|
|
|
|
* yet.
|
|
|
|
*/
|
2016-05-03 14:26:07 -04:00
|
|
|
isStable(): boolean { return this._isStable && !this.ngZone.hasPendingMacrotasks; }
|
2016-04-26 19:38:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a promise that resolves when the fixture is stable.
|
|
|
|
*
|
|
|
|
* This can be used to resume testing after events have triggered asynchronous activity or
|
|
|
|
* asynchronous change detection.
|
|
|
|
*/
|
|
|
|
whenStable(): Promise<any> {
|
2016-05-03 14:26:07 -04:00
|
|
|
if (this.isStable()) {
|
2016-04-26 19:38:54 -04:00
|
|
|
return PromiseWrapper.resolve(false);
|
2016-05-03 14:26:07 -04:00
|
|
|
} else if (this._completer !== null) {
|
|
|
|
return this._completer.promise;
|
2016-04-26 19:38:54 -04:00
|
|
|
} else {
|
|
|
|
this._completer = new PromiseCompleter<any>();
|
|
|
|
return this._completer.promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:05:17 -04:00
|
|
|
/**
|
|
|
|
* Trigger component destruction.
|
|
|
|
*/
|
2016-04-26 19:38:54 -04:00
|
|
|
destroy(): void {
|
|
|
|
this.componentRef.destroy();
|
|
|
|
if (this._onUnstableSubscription != null) {
|
|
|
|
ObservableWrapper.dispose(this._onUnstableSubscription);
|
|
|
|
this._onUnstableSubscription = null;
|
|
|
|
}
|
|
|
|
if (this._onStableSubscription != null) {
|
|
|
|
ObservableWrapper.dispose(this._onStableSubscription);
|
|
|
|
this._onStableSubscription = null;
|
|
|
|
}
|
|
|
|
if (this._onMicrotaskEmptySubscription != null) {
|
|
|
|
ObservableWrapper.dispose(this._onMicrotaskEmptySubscription);
|
|
|
|
this._onMicrotaskEmptySubscription = null;
|
|
|
|
}
|
|
|
|
if (this._onErrorSubscription != null) {
|
|
|
|
ObservableWrapper.dispose(this._onErrorSubscription);
|
|
|
|
this._onErrorSubscription = null;
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:02:20 -04:00
|
|
|
var _nextRootElementId = 0;
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
/**
|
2015-10-31 12:50:19 -04:00
|
|
|
* Builds a ComponentFixture for use in component level tests.
|
2015-05-15 19:42:52 -04:00
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class TestComponentBuilder {
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-29 14:11:06 -04:00
|
|
|
_bindingsOverrides = new Map<Type, any[]>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-29 14:11:06 -04:00
|
|
|
_directiveOverrides = new Map<Type, Map<Type, Type>>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-29 14:11:06 -04:00
|
|
|
_templateOverrides = new Map<Type, string>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-29 14:11:06 -04:00
|
|
|
_viewBindingsOverrides = new Map<Type, any[]>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-29 14:11:06 -04:00
|
|
|
_viewOverrides = new Map<Type, ViewMetadata>();
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private _injector: Injector) {}
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-05-15 19:42:52 -04:00
|
|
|
_clone(): TestComponentBuilder {
|
2016-04-26 19:38:54 -04:00
|
|
|
let clone = new TestComponentBuilder(this._injector);
|
2015-05-15 19:42:52 -04:00
|
|
|
clone._viewOverrides = MapWrapper.clone(this._viewOverrides);
|
|
|
|
clone._directiveOverrides = MapWrapper.clone(this._directiveOverrides);
|
|
|
|
clone._templateOverrides = MapWrapper.clone(this._templateOverrides);
|
2016-01-06 17:13:44 -05:00
|
|
|
clone._bindingsOverrides = MapWrapper.clone(this._bindingsOverrides);
|
|
|
|
clone._viewBindingsOverrides = MapWrapper.clone(this._viewBindingsOverrides);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides only the html of a {@link ComponentMetadata}.
|
|
|
|
* All the other properties of the component's {@link ViewMetadata} are preserved.
|
2015-05-15 19:42:52 -04:00
|
|
|
*/
|
|
|
|
overrideTemplate(componentType: Type, template: string): TestComponentBuilder {
|
2016-04-26 19:38:54 -04:00
|
|
|
let clone = this._clone();
|
2015-06-17 19:21:40 -04:00
|
|
|
clone._templateOverrides.set(componentType, template);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides a component's {@link ViewMetadata}.
|
2015-05-15 19:42:52 -04:00
|
|
|
*/
|
2015-08-14 13:03:45 -04:00
|
|
|
overrideView(componentType: Type, view: ViewMetadata): TestComponentBuilder {
|
2016-04-26 19:38:54 -04:00
|
|
|
let clone = this._clone();
|
2015-06-17 19:21:40 -04:00
|
|
|
clone._viewOverrides.set(componentType, view);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides the directives from the component {@link ViewMetadata}.
|
2015-05-15 19:42:52 -04:00
|
|
|
*/
|
|
|
|
overrideDirective(componentType: Type, from: Type, to: Type): TestComponentBuilder {
|
2016-04-26 19:38:54 -04:00
|
|
|
let clone = this._clone();
|
|
|
|
let overridesForComponent = clone._directiveOverrides.get(componentType);
|
2015-05-15 19:42:52 -04:00
|
|
|
if (!isPresent(overridesForComponent)) {
|
2015-09-29 14:11:06 -04:00
|
|
|
clone._directiveOverrides.set(componentType, new Map<Type, Type>());
|
2015-06-17 19:21:40 -04:00
|
|
|
overridesForComponent = clone._directiveOverrides.get(componentType);
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
2015-06-17 19:21:40 -04:00
|
|
|
overridesForComponent.set(from, to);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:14:57 -04:00
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Overrides one or more injectables configured via `providers` metadata property of a directive
|
|
|
|
* or
|
2015-09-08 13:14:57 -04:00
|
|
|
* component.
|
2015-10-11 01:11:13 -04:00
|
|
|
* Very useful when certain providers need to be mocked out.
|
2015-09-08 13:14:57 -04:00
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The providers specified via this method are appended to the existing `providers` causing the
|
|
|
|
* duplicated providers to
|
2015-09-08 13:14:57 -04:00
|
|
|
* be overridden.
|
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
overrideProviders(type: Type, providers: any[]): TestComponentBuilder {
|
2016-04-26 19:38:54 -04:00
|
|
|
let clone = this._clone();
|
2015-10-11 01:11:13 -04:00
|
|
|
clone._bindingsOverrides.set(type, providers);
|
2015-09-08 13:14:57 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
overrideBindings(type: Type, providers: any[]): TestComponentBuilder {
|
|
|
|
return this.overrideProviders(type, providers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides one or more injectables configured via `providers` metadata property of a directive
|
|
|
|
* or
|
2015-09-08 13:14:57 -04:00
|
|
|
* component.
|
2015-10-11 01:11:13 -04:00
|
|
|
* Very useful when certain providers need to be mocked out.
|
2015-09-08 13:14:57 -04:00
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The providers specified via this method are appended to the existing `providers` causing the
|
|
|
|
* duplicated providers to
|
2015-09-08 13:14:57 -04:00
|
|
|
* be overridden.
|
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
overrideViewProviders(type: Type, providers: any[]): TestComponentBuilder {
|
2016-04-26 19:38:54 -04:00
|
|
|
let clone = this._clone();
|
2015-10-11 01:11:13 -04:00
|
|
|
clone._viewBindingsOverrides.set(type, providers);
|
2015-09-08 13:14:57 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
overrideViewBindings(type: Type, providers: any[]): TestComponentBuilder {
|
|
|
|
return this.overrideViewProviders(type, providers);
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
private _create<C>(ngZone: NgZone, componentFactory: ComponentFactory<C>): ComponentFixture<C> {
|
|
|
|
let rootElId = `root${_nextRootElementId++}`;
|
2016-04-28 20:50:03 -04:00
|
|
|
var testComponentRenderer: TestComponentRenderer = this._injector.get(TestComponentRenderer);
|
|
|
|
testComponentRenderer.insertRootElement(rootElId);
|
2016-04-30 13:52:04 -04:00
|
|
|
|
|
|
|
var componentRef = componentFactory.create(this._injector, [], `#${rootElId}`);
|
|
|
|
let autoDetect: boolean = this._injector.get(ComponentFixtureAutoDetect, false);
|
|
|
|
return new ComponentFixture<any /*C*/>(componentRef, ngZone, autoDetect);
|
|
|
|
}
|
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
/**
|
2015-10-31 12:50:19 -04:00
|
|
|
* Builds and returns a ComponentFixture.
|
2015-05-15 19:42:52 -04:00
|
|
|
*/
|
2016-04-30 13:52:04 -04:00
|
|
|
createAsync(rootComponentType: Type): Promise<ComponentFixture<any>> {
|
2016-04-26 19:38:54 -04:00
|
|
|
let noNgZone = IS_DART || this._injector.get(ComponentFixtureNoNgZone, false);
|
|
|
|
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
|
|
|
|
|
|
|
|
let initComponent = () => {
|
|
|
|
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
|
|
|
|
let mockViewResolver = this._injector.get(ViewResolver);
|
|
|
|
this._viewOverrides.forEach((view, type) => mockViewResolver.setView(type, view));
|
|
|
|
this._templateOverrides.forEach((template, type) =>
|
|
|
|
mockViewResolver.setInlineTemplate(type, template));
|
|
|
|
this._directiveOverrides.forEach((overrides, component) => {
|
|
|
|
overrides.forEach(
|
|
|
|
(to, from) => { mockViewResolver.overrideViewDirective(component, from, to); });
|
|
|
|
});
|
|
|
|
this._bindingsOverrides.forEach(
|
|
|
|
(bindings, type) => mockDirectiveResolver.setBindingsOverride(type, bindings));
|
|
|
|
this._viewBindingsOverrides.forEach(
|
|
|
|
(bindings, type) => mockDirectiveResolver.setViewBindingsOverride(type, bindings));
|
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
let promise: Promise<ComponentFactory<any>> =
|
|
|
|
this._injector.get(ComponentResolver).resolveComponent(rootComponentType);
|
|
|
|
return promise.then(componentFactory => this._create(ngZone, componentFactory));
|
2016-04-26 19:38:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return ngZone == null ? initComponent() : ngZone.run(initComponent);
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
createFakeAsync(rootComponentType: Type): ComponentFixture<any> {
|
2016-04-26 19:38:54 -04:00
|
|
|
let result;
|
|
|
|
let error;
|
2016-01-06 17:13:44 -05:00
|
|
|
PromiseWrapper.then(this.createAsync(rootComponentType), (_result) => { result = _result; },
|
|
|
|
(_error) => { error = _error; });
|
|
|
|
tick();
|
|
|
|
if (isPresent(error)) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-04-30 13:52:04 -04:00
|
|
|
|
|
|
|
createSync<C>(componentFactory: ComponentFactory<C>): ComponentFixture<C> {
|
|
|
|
let noNgZone = IS_DART || this._injector.get(ComponentFixtureNoNgZone, false);
|
|
|
|
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
|
|
|
|
|
|
|
|
let initComponent = () => this._create(ngZone, componentFactory);
|
|
|
|
return ngZone == null ? initComponent() : ngZone.run(initComponent);
|
|
|
|
}
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|