refactor(core/testing): remove deprecated TestComponentBuilder

BREAKING CHANGE: deprecated TestComponentBuilder was removed, please use TestBed instead
This commit is contained in:
Igor Minar 2016-08-19 13:49:36 -07:00
parent 3c2b2ff332
commit a29f9f3ab8
6 changed files with 3 additions and 281 deletions

View File

@ -13,6 +13,3 @@ export var TestingCompiler: typeof t.TestingCompiler = r.TestingCompiler;
export type TestingCompilerFactory = t.TestingCompilerFactory; export type TestingCompilerFactory = t.TestingCompilerFactory;
export var TestingCompilerFactory: typeof t.TestingCompilerFactory = r.TestingCompilerFactory; export var TestingCompilerFactory: typeof t.TestingCompilerFactory = r.TestingCompilerFactory;
export type TestComponentBuilder = t.TestComponentBuilder;
export var TestComponentBuilder: typeof t.TestComponentBuilder = r.TestComponentBuilder;

View File

@ -1,125 +0,0 @@
/**
* @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
*/
import {AnimationEntryMetadata, Compiler, ComponentFactory, Inject, Injectable, Injector, NgZone, Type} from '@angular/core';
import {ComponentFixture, ComponentFixtureNoNgZone, TestBed} from '@angular/core/testing';
import {ViewMetadata} from '../core_private';
import {TestComponentBuilder} from '../core_private_testing';
// export {ViewMetadata} from '../core_private';
import {DirectiveResolver} from '../index';
import {MapWrapper} from '../src/facade/collection';
import {isPresent} from '../src/facade/lang';
/**
* A TestComponentBuilder that allows overriding based on the compiler.
*
* @deprecated Use `TestBed.configureTestModule` / `TestBed.override...` / `TestBed.createComponent`
* instead.
*/
@Injectable()
export class OverridingTestComponentBuilder extends TestComponentBuilder {
/** @internal */
private _bindingsOverrides = new Map<Type<any>, any[]>();
/** @internal */
private _directiveOverrides = new Map<Type<any>, Map<Type<any>, Type<any>>>();
/** @internal */
private _templateOverrides = new Map<Type<any>, string>();
/** @internal */
private _animationOverrides = new Map<Type<any>, AnimationEntryMetadata[]>();
/** @internal */
private _viewBindingsOverrides = new Map<Type<any>, any[]>();
/** @internal */
private _viewOverrides = new Map<Type<any>, ViewMetadata>();
constructor(@Inject(TestBed) injector: Injector) { super(injector); }
/** @internal */
_clone(): OverridingTestComponentBuilder {
let clone = new OverridingTestComponentBuilder(this._injector);
clone._viewOverrides = MapWrapper.clone(this._viewOverrides);
clone._directiveOverrides = MapWrapper.clone(this._directiveOverrides);
clone._templateOverrides = MapWrapper.clone(this._templateOverrides);
clone._bindingsOverrides = MapWrapper.clone(this._bindingsOverrides);
clone._viewBindingsOverrides = MapWrapper.clone(this._viewBindingsOverrides);
return clone;
}
overrideTemplate(componentType: Type<any>, template: string): OverridingTestComponentBuilder {
let clone = this._clone();
clone._templateOverrides.set(componentType, template);
return clone;
}
overrideAnimations(componentType: Type<any>, animations: AnimationEntryMetadata[]):
TestComponentBuilder {
var clone = this._clone();
clone._animationOverrides.set(componentType, animations);
return clone;
}
overrideView(componentType: Type<any>, view: ViewMetadata): OverridingTestComponentBuilder {
let clone = this._clone();
clone._viewOverrides.set(componentType, view);
return clone;
}
overrideDirective(componentType: Type<any>, from: Type<any>, to: Type<any>):
OverridingTestComponentBuilder {
let clone = this._clone();
let overridesForComponent = clone._directiveOverrides.get(componentType);
if (!isPresent(overridesForComponent)) {
clone._directiveOverrides.set(componentType, new Map<Type<any>, Type<any>>());
overridesForComponent = clone._directiveOverrides.get(componentType);
}
overridesForComponent.set(from, to);
return clone;
}
overrideProviders(type: Type<any>, providers: any[]): OverridingTestComponentBuilder {
let clone = this._clone();
clone._bindingsOverrides.set(type, providers);
return clone;
}
overrideViewProviders(type: Type<any>, providers: any[]): OverridingTestComponentBuilder {
let clone = this._clone();
clone._viewBindingsOverrides.set(type, providers);
return clone;
}
createAsync<T>(rootComponentType: Type<T>): Promise<ComponentFixture<T>> {
this._applyMetadataOverrides();
return super.createAsync(rootComponentType);
}
createSync<T>(rootComponentType: Type<T>): ComponentFixture<T> {
this._applyMetadataOverrides();
return super.createSync(rootComponentType);
}
private _applyMetadataOverrides() {
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
this._viewOverrides.forEach((view, type) => { mockDirectiveResolver.setView(type, view); });
this._templateOverrides.forEach(
(template, type) => mockDirectiveResolver.setInlineTemplate(type, template));
this._animationOverrides.forEach(
(animationsEntry, type) => mockDirectiveResolver.setAnimations(type, animationsEntry));
this._directiveOverrides.forEach((overrides, component) => {
overrides.forEach(
(to, from) => { mockDirectiveResolver.overrideViewDirective(component, from, to); });
});
this._bindingsOverrides.forEach(
(bindings, type) => mockDirectiveResolver.setProvidersOverride(type, bindings));
this._viewBindingsOverrides.forEach(
(bindings, type) => mockDirectiveResolver.setViewProvidersOverride(type, bindings));
}
}

View File

@ -7,19 +7,15 @@
*/ */
import * as test_compiler from './testing/test_compiler'; import * as test_compiler from './testing/test_compiler';
import * as test_component_builder from './testing/test_component_builder';
export declare namespace __core_private_testing_types__ { export declare namespace __core_private_testing_types__ {
export type TestingCompiler = test_compiler.TestingCompiler; export type TestingCompiler = test_compiler.TestingCompiler;
export var TestingCompiler: typeof test_compiler.TestingCompiler; export var TestingCompiler: typeof test_compiler.TestingCompiler;
export type TestingCompilerFactory = test_compiler.TestingCompilerFactory; export type TestingCompilerFactory = test_compiler.TestingCompilerFactory;
export var TestingCompilerFactory: typeof test_compiler.TestingCompilerFactory; export var TestingCompilerFactory: typeof test_compiler.TestingCompilerFactory;
export type TestComponentBuilder = test_component_builder.TestComponentBuilder;
export var TestComponentBuilder: typeof test_component_builder.TestComponentBuilder;
} }
export var __core_private_testing__ = { export var __core_private_testing__ = {
TestingCompiler: test_compiler.TestingCompiler, TestingCompiler: test_compiler.TestingCompiler,
TestingCompilerFactory: test_compiler.TestingCompilerFactory, TestingCompilerFactory: test_compiler.TestingCompilerFactory,
TestComponentBuilder: test_component_builder.TestComponentBuilder,
}; };

View File

@ -1,144 +0,0 @@
/**
* @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
*/
import {AnimationEntryMetadata, Compiler, ComponentFactory, Injectable, Injector, NgZone, OpaqueToken} from '../index';
import {isPresent} from '../src/facade/lang';
import {ViewMetadata} from '../src/metadata/view';
import {Type} from '../src/type';
import {ComponentFixture} from './component_fixture';
import {tick} from './fake_async';
import {ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, TestComponentRenderer} from './test_bed';
var _nextRootElementId = 0;
/**
* Builds a ComponentFixture for use in component level tests.
*
* @deprecated Use `TestBed.configureTestModule` / `TestBed.override...` / `TestBed.createComponent`
* instead.
*/
@Injectable()
export class TestComponentBuilder {
constructor(protected _injector: Injector) {}
/**
* Overrides only the html of a {@link ComponentMetadata}.
* All the other properties of the component's {@link ViewMetadata} are preserved.
*/
overrideTemplate(componentType: Type<any>, template: string): TestComponentBuilder {
throw new Error(
'overrideTemplate is not supported in this implementation of TestComponentBuilder.');
}
/**
* Overrides a component's {@link ViewMetadata}.
*/
overrideView(componentType: Type<any>, view: ViewMetadata): TestComponentBuilder {
throw new Error(
'overrideView is not supported in this implementation of TestComponentBuilder.');
}
/**
* Overrides the directives from the component {@link ViewMetadata}.
*/
overrideDirective(componentType: Type<any>, from: Type<any>, to: Type<any>):
TestComponentBuilder {
throw new Error(
'overrideDirective is not supported in this implementation of TestComponentBuilder.');
}
/**
* Overrides one or more injectables configured via `providers` metadata property of a directive
* or
* component.
* Very useful when certain providers need to be mocked out.
*
* The providers specified via this method are appended to the existing `providers` causing the
* duplicated providers to
* be overridden.
*/
overrideProviders(type: Type<any>, providers: any[]): TestComponentBuilder {
throw new Error(
'overrideProviders is not supported in this implementation of TestComponentBuilder.');
}
/**
* Overrides one or more injectables configured via `providers` metadata property of a directive
* or
* component.
* Very useful when certain providers need to be mocked out.
*
* The providers specified via this method are appended to the existing `providers` causing the
* duplicated providers to
* be overridden.
*/
overrideViewProviders(type: Type<any>, providers: any[]): TestComponentBuilder {
throw new Error(
'overrideViewProviders is not supported in this implementation of TestComponentBuilder.');
}
overrideAnimations(componentType: Type<any>, animations: AnimationEntryMetadata[]):
TestComponentBuilder {
throw new Error(
'overrideAnimations is not supported in this implementation of TestComponentBuilder.');
}
protected createFromFactory<C>(ngZone: NgZone, componentFactory: ComponentFactory<C>):
ComponentFixture<C> {
let rootElId = `root${_nextRootElementId++}`;
var testComponentRenderer: TestComponentRenderer = this._injector.get(TestComponentRenderer);
testComponentRenderer.insertRootElement(rootElId);
var componentRef = componentFactory.create(this._injector, [], `#${rootElId}`);
let autoDetect: boolean = this._injector.get(ComponentFixtureAutoDetect, false);
return new ComponentFixture<any /*C*/>(componentRef, ngZone, autoDetect);
}
/**
* Builds and returns a ComponentFixture.
*/
createAsync<T>(rootComponentType: Type<T>): Promise<ComponentFixture<T>> {
let noNgZone = this._injector.get(ComponentFixtureNoNgZone, false);
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
let compiler: Compiler = this._injector.get(Compiler);
let initComponent = () => {
let promise: Promise<ComponentFactory<any>> =
compiler.compileComponentAsync(rootComponentType);
return promise.then(componentFactory => this.createFromFactory(ngZone, componentFactory));
};
return ngZone == null ? initComponent() : ngZone.run(initComponent);
}
createFakeAsync<T>(rootComponentType: Type<T>): ComponentFixture<T> {
let result: any /** TODO #9100 */;
let error: any /** TODO #9100 */;
this.createAsync(rootComponentType)
.then((_result) => { result = _result; }, (_error) => { error = _error; });
tick();
if (isPresent(error)) {
throw error;
}
return result;
}
createSync<T>(rootComponentType: Type<T>): ComponentFixture<T> {
let noNgZone = this._injector.get(ComponentFixtureNoNgZone, false);
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
let compiler: Compiler = this._injector.get(Compiler);
let initComponent = () => {
return this.createFromFactory(ngZone, compiler.compileComponentSync(rootComponentType));
};
return ngZone == null ? initComponent() : ngZone.run(initComponent);
}
}

View File

@ -7,7 +7,7 @@
*/ */
import {StringMapWrapper} from '../src/facade/collection'; import {StringMapWrapper} from '../src/facade/collection';
import {Math, global, isFunction, isPromise} from '../src/facade/lang'; import {Math, global, isPromise} from '../src/facade/lang';
import {AsyncTestCompleter} from './async_test_completer'; import {AsyncTestCompleter} from './async_test_completer';
import {TestBed, getTestBed, inject} from './test_bed'; import {TestBed, getTestBed, inject} from './test_bed';
@ -17,7 +17,6 @@ export {MockAnimationPlayer} from './mock_animation_player';
export {inject} from './test_bed'; export {inject} from './test_bed';
export * from './logger'; export * from './logger';
export * from './ng_zone_mock'; export * from './ng_zone_mock';
export * from './test_component_builder';
export var proxy: ClassDecorator = (t: any /** TODO #9100 */) => t; export var proxy: ClassDecorator = (t: any /** TODO #9100 */) => t;

View File

@ -6,9 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {OverridingTestComponentBuilder, platformCoreDynamicTesting} from '@angular/compiler/testing'; import {platformCoreDynamicTesting} from '@angular/compiler/testing';
import {NgModule, PlatformRef, createPlatformFactory} from '@angular/core'; import {NgModule, PlatformRef, createPlatformFactory} from '@angular/core';
import {TestComponentRenderer, __core_private_testing__ as r, __core_private_testing_types__ as t} from '@angular/core/testing'; import {TestComponentRenderer} from '@angular/core/testing';
import {BrowserTestingModule} from '@angular/platform-browser/testing'; import {BrowserTestingModule} from '@angular/platform-browser/testing';
import {INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS} from './src/platform_providers'; import {INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS} from './src/platform_providers';
@ -31,7 +31,6 @@ export const platformBrowserDynamicTesting = createPlatformFactory(
@NgModule({ @NgModule({
exports: [BrowserTestingModule], exports: [BrowserTestingModule],
providers: [ providers: [
{provide: r.TestComponentBuilder, useClass: OverridingTestComponentBuilder},
{provide: TestComponentRenderer, useClass: DOMTestComponentRenderer}, {provide: TestComponentRenderer, useClass: DOMTestComponentRenderer},
] ]
}) })