feat(ivy): implement TestBed (#25369)

PR Close #25369
This commit is contained in:
Victor Berchet
2018-08-06 14:09:38 -07:00
committed by Ben Lesh
parent 85106375ac
commit 14ac7ad6b4
37 changed files with 1543 additions and 363 deletions

View File

@ -6,56 +6,31 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ApplicationInitStatus, CompilerOptions, Component, Directive, InjectionToken, Injector, NgModule, NgModuleFactory, NgModuleRef, NgZone, Optional, Pipe, PlatformRef, Provider, SchemaMetadata, SkipSelf, StaticProvider, Type, ɵAPP_ROOT as APP_ROOT, ɵDepFlags as DepFlags, ɵNodeFlags as NodeFlags, ɵclearOverrides as clearOverrides, ɵgetComponentViewDefinitionFactory as getComponentViewDefinitionFactory, ɵoverrideComponentView as overrideComponentView, ɵoverrideProvider as overrideProvider, ɵstringify as stringify} from '@angular/core';
import {ApplicationInitStatus, CompilerOptions, Component, Directive, Injector, NgModule, NgModuleFactory, NgModuleRef, NgZone, Optional, Pipe, PlatformRef, Provider, SchemaMetadata, SkipSelf, StaticProvider, Type, ɵAPP_ROOT as APP_ROOT, ɵDepFlags as DepFlags, ɵNodeFlags as NodeFlags, ɵclearOverrides as clearOverrides, ɵgetComponentViewDefinitionFactory as getComponentViewDefinitionFactory, ɵivyEnabled as ivyEnabled, ɵoverrideComponentView as overrideComponentView, ɵoverrideProvider as overrideProvider, ɵstringify as stringify} from '@angular/core';
import {AsyncTestCompleter} from './async_test_completer';
import {ComponentFixture} from './component_fixture';
import {MetadataOverride} from './metadata_override';
import {TestBedRender3, _getTestBedRender3} from './r3_test_bed';
import {ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, TestComponentRenderer, TestModuleMetadata} from './test_bed_common';
import {TestingCompiler, TestingCompilerFactory} from './test_compiler';
const UNDEFINED = new Object();
/**
* An abstract class for inserting the root test component element in a platform independent way.
*
* @experimental
*/
export class TestComponentRenderer {
insertRootElement(rootElementId: string) {}
}
let _nextRootElementId = 0;
/**
* @experimental
*/
export const ComponentFixtureAutoDetect =
new InjectionToken<boolean[]>('ComponentFixtureAutoDetect');
/**
* @experimental
*/
export const ComponentFixtureNoNgZone = new InjectionToken<boolean[]>('ComponentFixtureNoNgZone');
/**
* @experimental
*/
export type TestModuleMetadata = {
providers?: any[],
declarations?: any[],
imports?: any[],
schemas?: Array<SchemaMetadata|any[]>,
aotSummaries?: () => any[],
};
/**
* @description
* Configures and initializes environment for unit testing and provides methods for
* creating components and services in unit tests.
*
* TestBed is the primary api for writing unit tests for Angular applications and libraries.
* `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
*
* Note: Use `TestBed` in tests. It will be set to either `TestBedViewEngine` or `TestBedRender3`
* according to the compiler used.
*/
export class TestBed implements Injector {
export class TestBedViewEngine implements Injector {
/**
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
* angular module. These are common to every test in the suite.
@ -70,8 +45,9 @@ export class TestBed implements Injector {
* @experimental
*/
static initTestEnvironment(
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed {
const testBed = getTestBed();
ngModule: Type<any>|Type<any>[], platform: PlatformRef,
aotSummaries?: () => any[]): TestBedViewEngine {
const testBed = _getTestBedViewEngine();
testBed.initTestEnvironment(ngModule, platform, aotSummaries);
return testBed;
}
@ -81,10 +57,10 @@ export class TestBed implements Injector {
*
* @experimental
*/
static resetTestEnvironment() { getTestBed().resetTestEnvironment(); }
static resetTestEnvironment(): void { _getTestBedViewEngine().resetTestEnvironment(); }
static resetTestingModule(): typeof TestBed {
getTestBed().resetTestingModule();
_getTestBedViewEngine().resetTestingModule();
return TestBed;
}
@ -93,7 +69,7 @@ export class TestBed implements Injector {
* which are defined in test_injector.js
*/
static configureCompiler(config: {providers?: any[]; useJit?: boolean;}): typeof TestBed {
getTestBed().configureCompiler(config);
_getTestBedViewEngine().configureCompiler(config);
return TestBed;
}
@ -102,7 +78,7 @@ export class TestBed implements Injector {
* which are defined in test_injector.js
*/
static configureTestingModule(moduleDef: TestModuleMetadata): typeof TestBed {
getTestBed().configureTestingModule(moduleDef);
_getTestBedViewEngine().configureTestingModule(moduleDef);
return TestBed;
}
@ -114,29 +90,29 @@ export class TestBed implements Injector {
static compileComponents(): Promise<any> { return getTestBed().compileComponents(); }
static overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): typeof TestBed {
getTestBed().overrideModule(ngModule, override);
_getTestBedViewEngine().overrideModule(ngModule, override);
return TestBed;
}
static overrideComponent(component: Type<any>, override: MetadataOverride<Component>):
typeof TestBed {
getTestBed().overrideComponent(component, override);
_getTestBedViewEngine().overrideComponent(component, override);
return TestBed;
}
static overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>):
typeof TestBed {
getTestBed().overrideDirective(directive, override);
_getTestBedViewEngine().overrideDirective(directive, override);
return TestBed;
}
static overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): typeof TestBed {
getTestBed().overridePipe(pipe, override);
_getTestBedViewEngine().overridePipe(pipe, override);
return TestBed;
}
static overrideTemplate(component: Type<any>, template: string): typeof TestBed {
getTestBed().overrideComponent(component, {set: {template, templateUrl: null !}});
_getTestBedViewEngine().overrideComponent(component, {set: {template, templateUrl: null !}});
return TestBed;
}
@ -148,11 +124,10 @@ export class TestBed implements Injector {
*/
static overrideTemplateUsingTestingModule(component: Type<any>, template: string):
typeof TestBed {
getTestBed().overrideTemplateUsingTestingModule(component, template);
_getTestBedViewEngine().overrideTemplateUsingTestingModule(component, template);
return TestBed;
}
/**
* Overwrites all providers for the given token with the given provider definition.
*
@ -168,7 +143,7 @@ export class TestBed implements Injector {
useValue?: any,
deps?: any[],
}): typeof TestBed {
getTestBed().overrideProvider(token, provider as any);
_getTestBedViewEngine().overrideProvider(token, provider as any);
return TestBed;
}
@ -187,16 +162,16 @@ export class TestBed implements Injector {
useValue?: any,
deps?: any[],
}): typeof TestBed {
getTestBed().deprecatedOverrideProvider(token, provider as any);
_getTestBedViewEngine().deprecatedOverrideProvider(token, provider as any);
return TestBed;
}
static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
return getTestBed().get(token, notFoundValue);
return _getTestBedViewEngine().get(token, notFoundValue);
}
static createComponent<T>(component: Type<T>): ComponentFixture<T> {
return getTestBed().createComponent(component);
return _getTestBedViewEngine().createComponent(component);
}
private _instantiated: boolean = false;
@ -243,7 +218,7 @@ export class TestBed implements Injector {
* @experimental
*/
initTestEnvironment(
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]) {
ngModule: Type<any>|Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void {
if (this.platform || this.ngModule) {
throw new Error('Cannot set base providers because it has already been called');
}
@ -259,14 +234,14 @@ export class TestBed implements Injector {
*
* @experimental
*/
resetTestEnvironment() {
resetTestEnvironment(): void {
this.resetTestingModule();
this.platform = null !;
this.ngModule = null !;
this._testEnvAotSummaries = () => [];
}
resetTestingModule() {
resetTestingModule(): void {
clearOverrides();
this._aotSummaries = [];
this._templateOverrides = [];
@ -300,12 +275,12 @@ export class TestBed implements Injector {
this._activeFixtures = [];
}
configureCompiler(config: {providers?: any[], useJit?: boolean}) {
configureCompiler(config: {providers?: any[], useJit?: boolean}): void {
this._assertNotInstantiated('TestBed.configureCompiler', 'configure the compiler');
this._compilerOptions.push(config);
}
configureTestingModule(moduleDef: TestModuleMetadata) {
configureTestingModule(moduleDef: TestModuleMetadata): void {
this._assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
if (moduleDef.providers) {
this._providers.push(...moduleDef.providers);
@ -336,7 +311,7 @@ export class TestBed implements Injector {
});
}
private _initIfNeeded() {
private _initIfNeeded(): void {
if (this._instantiated) {
return;
}
@ -425,7 +400,7 @@ export class TestBed implements Injector {
}
}
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND): any {
this._initIfNeeded();
if (token === TestBed) {
return this;
@ -574,13 +549,32 @@ export class TestBed implements Injector {
}
}
let _testBed: TestBed = null !;
/**
* @description
* Configures and initializes environment for unit testing and provides methods for
* creating components and services in unit tests.
*
* `TestBed` is the primary api for writing unit tests for Angular applications and libraries.
*
* Note: Use `TestBed` in tests. It will be set to either `TestBedViewEngine` or `TestBedRender3`
* according to the compiler used.
*/
export const TestBed = ivyEnabled ? TestBedRender3 : TestBedViewEngine;
/**
* Returns a singleton of the applicable `TestBed`.
*
* It will be either an instance of `TestBedViewEngine` or `TestBedRender3`.
*
* @experimental
*/
export function getTestBed(): TestBed {
return _testBed = _testBed || new TestBed();
export const getTestBed: () => TestBedRender3 | TestBedViewEngine =
ivyEnabled ? _getTestBedRender3 : _getTestBedViewEngine;
let testBed: TestBedViewEngine;
function _getTestBedViewEngine(): TestBedViewEngine {
return testBed = testBed || new TestBedViewEngine();
}
/**