2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
import {AnimationEntryMetadata, Compiler, ComponentFactory, Inject, Injectable, Injector, NgZone, Type, ViewMetadata} from '@angular/core';
|
2016-07-20 13:51:21 -04:00
|
|
|
import {ComponentFixture, ComponentFixtureNoNgZone, TestBed, TestComponentBuilder} from '@angular/core/testing';
|
2016-04-28 20:50:03 -04:00
|
|
|
|
2016-07-28 09:31:26 -04:00
|
|
|
import {DirectiveResolver} from '../index';
|
2016-06-24 20:35:01 -04:00
|
|
|
import {MapWrapper} from '../src/facade/collection';
|
2016-08-10 21:21:28 -04:00
|
|
|
import {isPresent} from '../src/facade/lang';
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
|
2016-07-20 13:51:21 -04:00
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
/**
|
2016-06-24 20:35:01 -04:00
|
|
|
* A TestComponentBuilder that allows overriding based on the compiler.
|
2016-07-28 07:54:49 -04:00
|
|
|
*
|
|
|
|
* @deprecated Use `TestBed.configureTestModule` / `TestBed.override...` / `TestBed.createComponent`
|
|
|
|
* instead.
|
|
|
|
*/
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
2016-06-24 20:35:01 -04:00
|
|
|
export class OverridingTestComponentBuilder extends TestComponentBuilder {
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-08-10 21:21:28 -04:00
|
|
|
_bindingsOverrides = new Map<Type<any>, any[]>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-08-10 21:21:28 -04:00
|
|
|
_directiveOverrides = new Map<Type<any>, Map<Type<any>, Type<any>>>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-08-10 21:21:28 -04:00
|
|
|
_templateOverrides = new Map<Type<any>, string>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-08-10 21:21:28 -04:00
|
|
|
_animationOverrides = new Map<Type<any>, AnimationEntryMetadata[]>();
|
2016-05-25 15:46:22 -04:00
|
|
|
/** @internal */
|
2016-08-10 21:21:28 -04:00
|
|
|
_viewBindingsOverrides = new Map<Type<any>, any[]>();
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-08-10 21:21:28 -04:00
|
|
|
_viewOverrides = new Map<Type<any>, ViewMetadata>();
|
2015-09-29 14:11:06 -04:00
|
|
|
|
2016-07-20 13:51:21 -04:00
|
|
|
constructor(@Inject(TestBed) injector: Injector) { super(injector); }
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2016-06-24 20:35:01 -04:00
|
|
|
_clone(): OverridingTestComponentBuilder {
|
|
|
|
let clone = new OverridingTestComponentBuilder(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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideTemplate(componentType: Type<any>, template: string): OverridingTestComponentBuilder {
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideAnimations(componentType: Type<any>, animations: AnimationEntryMetadata[]):
|
2016-06-08 19:38:52 -04:00
|
|
|
TestComponentBuilder {
|
2016-05-25 15:46:22 -04:00
|
|
|
var clone = this._clone();
|
|
|
|
clone._animationOverrides.set(componentType, animations);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideView(componentType: Type<any>, view: ViewMetadata): OverridingTestComponentBuilder {
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideDirective(componentType: Type<any>, from: Type<any>, to: Type<any>):
|
|
|
|
OverridingTestComponentBuilder {
|
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)) {
|
2016-08-10 21:21:28 -04:00
|
|
|
clone._directiveOverrides.set(componentType, new Map<Type<any>, Type<any>>());
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideProviders(type: Type<any>, providers: any[]): OverridingTestComponentBuilder {
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideViewProviders(type: Type<any>, providers: any[]): OverridingTestComponentBuilder {
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
createAsync<T>(rootComponentType: Type<T>): Promise<ComponentFixture<T>> {
|
2016-06-24 11:46:43 -04:00
|
|
|
this._applyMetadataOverrides();
|
2016-07-28 07:54:49 -04:00
|
|
|
return super.createAsync(rootComponentType);
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
createSync<T>(rootComponentType: Type<T>): ComponentFixture<T> {
|
2016-06-24 11:46:43 -04:00
|
|
|
this._applyMetadataOverrides();
|
2016-07-28 07:54:49 -04:00
|
|
|
return super.createSync(rootComponentType);
|
2016-06-24 11:46:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private _applyMetadataOverrides() {
|
|
|
|
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
|
2016-07-28 09:31:26 -04:00
|
|
|
this._viewOverrides.forEach((view, type) => { mockDirectiveResolver.setView(type, view); });
|
2016-06-24 11:46:43 -04:00
|
|
|
this._templateOverrides.forEach(
|
2016-07-28 09:31:26 -04:00
|
|
|
(template, type) => mockDirectiveResolver.setInlineTemplate(type, template));
|
2016-06-24 11:46:43 -04:00
|
|
|
this._animationOverrides.forEach(
|
2016-07-28 09:31:26 -04:00
|
|
|
(animationsEntry, type) => mockDirectiveResolver.setAnimations(type, animationsEntry));
|
2016-06-24 11:46:43 -04:00
|
|
|
this._directiveOverrides.forEach((overrides, component) => {
|
|
|
|
overrides.forEach(
|
2016-07-28 09:31:26 -04:00
|
|
|
(to, from) => { mockDirectiveResolver.overrideViewDirective(component, from, to); });
|
2016-06-24 11:46:43 -04:00
|
|
|
});
|
|
|
|
this._bindingsOverrides.forEach(
|
|
|
|
(bindings, type) => mockDirectiveResolver.setProvidersOverride(type, bindings));
|
|
|
|
this._viewBindingsOverrides.forEach(
|
|
|
|
(bindings, type) => mockDirectiveResolver.setViewProvidersOverride(type, bindings));
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
|
|
|
}
|