Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions packages/core/testing/src/r3_test_bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import {
Component,
Directive,
InjectFlags,
InjectionToken,
Injector,
NgModule,
NgZone,
Expand Down Expand Up @@ -167,7 +169,14 @@ export class TestBedRender3 implements Injector, TestBed {
return TestBedRender3 as any as TestBedStatic;
}

static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND): any {
static get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/**
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
*/
static get(token: any, notFoundValue?: any): any;
static get(
token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND,
flags: InjectFlags = InjectFlags.Default): any {
return _getTestBedRender3().get(token, notFoundValue);
}

Expand Down Expand Up @@ -254,12 +263,18 @@ export class TestBedRender3 implements Injector, TestBed {

compileComponents(): Promise<any> { return this.compiler.compileComponents(); }

get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND): any {
get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/**
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
*/
get(token: any, notFoundValue?: any): any;
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND,
flags: InjectFlags = InjectFlags.Default): any {
if (token === TestBedRender3) {
return this;
}
const result = this.testModuleRef.injector.get(token, UNDEFINED);
return result === UNDEFINED ? this.compiler.injector.get(token, notFoundValue) : result;
const result = this.testModuleRef.injector.get(token, UNDEFINED, flags);
return result === UNDEFINED ? this.compiler.injector.get(token, notFoundValue, flags) : result;
}

execute(tokens: any[], fn: Function, context?: any): any {
Expand Down Expand Up @@ -338,9 +353,11 @@ export class TestBedRender3 implements Injector, TestBed {
`It looks like '${stringify(type)}' has not been IVY compiled - it has no 'ngComponentDef' field`);
}

const noNgZone: boolean = this.get(ComponentFixtureNoNgZone, false);
const autoDetect: boolean = this.get(ComponentFixtureAutoDetect, false);
const ngZone: NgZone = noNgZone ? null : this.get(NgZone, null);
// TODO: Don't cast as `any`, proper type is boolean[]
const noNgZone = this.get(ComponentFixtureNoNgZone as any, false);
// TODO: Don't cast as `any`, proper type is boolean[]
const autoDetect: boolean = this.get(ComponentFixtureAutoDetect as any, false);
const ngZone: NgZone|null = noNgZone ? null : this.get(NgZone as Type<NgZone|null>, null);
const componentFactory = new ComponentFactory(componentDef);
const initComponent = () => {
const componentRef =
Expand Down
40 changes: 30 additions & 10 deletions packages/core/testing/src/test_bed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

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, ɵInjectableDef as InjectableDef, ɵNodeFlags as NodeFlags, ɵclearOverrides as clearOverrides, ɵgetInjectableDef as getInjectableDef, ɵivyEnabled as ivyEnabled, ɵoverrideComponentView as overrideComponentView, ɵoverrideProvider as overrideProvider, ɵstringify as stringify} from '@angular/core';
import {ApplicationInitStatus, CompilerOptions, Component, Directive, InjectFlags, InjectionToken, Injector, NgModule, NgModuleFactory, NgModuleRef, NgZone, Optional, Pipe, PlatformRef, Provider, SchemaMetadata, SkipSelf, StaticProvider, Type, ɵAPP_ROOT as APP_ROOT, ɵDepFlags as DepFlags, ɵInjectableDef as InjectableDef, ɵNodeFlags as NodeFlags, ɵclearOverrides as clearOverrides, ɵgetInjectableDef as getInjectableDef, ɵ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';
Expand Down Expand Up @@ -56,6 +56,10 @@ export interface TestBed {

compileComponents(): Promise<any>;

get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/**
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
*/
get(token: any, notFoundValue?: any): any;

execute(tokens: any[], fn: Function, context?: any): any;
Expand Down Expand Up @@ -239,8 +243,16 @@ export class TestBedViewEngine implements Injector, TestBed {
return TestBedViewEngine as any as TestBedStatic;
}

static get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND) {
return _getTestBedViewEngine().get(token, notFoundValue);
static get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/**
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
* @suppress {duplicate}
*/
static get(token: any, notFoundValue?: any): any;
static get(
token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND,
flags: InjectFlags = InjectFlags.Default): any {
return _getTestBedViewEngine().get(token, notFoundValue, flags);
}

static createComponent<T>(component: Type<T>): ComponentFixture<T> {
Expand Down Expand Up @@ -469,15 +481,21 @@ export class TestBedViewEngine implements Injector, TestBed {
}
}

get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND): any {
get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/**
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
*/
get(token: any, notFoundValue?: any): any;
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND,
flags: InjectFlags = InjectFlags.Default): any {
this._initIfNeeded();
if (token === TestBed) {
return this;
}
// Tests can inject things from the ng module and from the compiler,
// but the ng module can't inject things from the compiler and vice versa.
const result = this._moduleRef.injector.get(token, UNDEFINED);
return result === UNDEFINED ? this._compiler.injector.get(token, notFoundValue) : result;
const result = this._moduleRef.injector.get(token, UNDEFINED, flags);
return result === UNDEFINED ? this._compiler.injector.get(token, notFoundValue, flags) : result;
}

execute(tokens: any[], fn: Function, context?: any): any {
Expand Down Expand Up @@ -599,9 +617,11 @@ export class TestBedViewEngine implements Injector, TestBed {
`Cannot create the component ${stringify(component)} as it was not imported into the testing module!`);
}

const noNgZone = this.get(ComponentFixtureNoNgZone, false);
const autoDetect: boolean = this.get(ComponentFixtureAutoDetect, false);
const ngZone: NgZone = noNgZone ? null : this.get(NgZone, null);
// TODO: Don't cast as `any`, proper type is boolean[]
const noNgZone = this.get(ComponentFixtureNoNgZone as any, false);
// TODO: Don't cast as `any`, proper type is boolean[]
const autoDetect: boolean = this.get(ComponentFixtureAutoDetect as any, false);
const ngZone: NgZone|null = noNgZone ? null : this.get(NgZone as Type<NgZone|null>, null);
const testComponentRenderer: TestComponentRenderer = this.get(TestComponentRenderer);
const rootElId = `root${_nextRootElementId++}`;
testComponentRenderer.insertRootElement(rootElId);
Expand Down Expand Up @@ -732,4 +752,4 @@ export function withModule(moduleDef: TestModuleMetadata, fn?: Function | null):
};
}
return new InjectSetupWrapper(() => moduleDef);
}
}
6 changes: 5 additions & 1 deletion packages/core/testing/src/test_bed_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component, Directive, InjectionToken, NgModule, Pipe, PlatformRef, SchemaMetadata, Type} from '@angular/core';
import {Component, Directive, InjectFlags, InjectionToken, NgModule, Pipe, PlatformRef, SchemaMetadata, Type} from '@angular/core';

import {ComponentFixture} from './component_fixture';
import {MetadataOverride} from './metadata_override';
Expand Down Expand Up @@ -130,6 +130,10 @@ export interface TestBedStatic {
deps?: any[],
}): TestBedStatic;

get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/**
* @deprecated from v8.0.0 use Type<T> or InjectionToken<T>
*/
get(token: any, notFoundValue?: any): any;

createComponent<T>(component: Type<T>): ComponentFixture<T>;
Expand Down
18 changes: 10 additions & 8 deletions tools/public_api_guard/core/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ export interface TestBed {
useValue?: any;
deps?: any[];
}): void;
deprecatedOverrideProvider(token: any, provider: {
useValue: any;
}): void;
/** @deprecated */ deprecatedOverrideProvider(token: any, provider: {
useFactory: Function;
deps: any[];
}): void;
deprecatedOverrideProvider(token: any, provider: {
useValue: any;
}): void;
execute(tokens: any[], fn: Function, context?: any): any;
get(token: any, notFoundValue?: any): any;
get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/** @deprecated */ get(token: any, notFoundValue?: any): any;
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void;
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void;
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void;
Expand All @@ -82,11 +83,11 @@ export interface TestBed {
deps?: any[];
}): void;
overrideProvider(token: any, provider: {
useFactory: Function;
deps: any[];
useValue: any;
}): void;
overrideProvider(token: any, provider: {
useValue: any;
useFactory: Function;
deps: any[];
}): void;
overrideTemplateUsingTestingModule(component: Type<any>, template: string): void;
resetTestEnvironment(): void;
Expand Down Expand Up @@ -116,7 +117,8 @@ export interface TestBedStatic {
useFactory: Function;
deps: any[];
}): void;
get(token: any, notFoundValue?: any): any;
/** @deprecated */ get(token: any, notFoundValue?: any): any;
get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed;
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBedStatic;
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBedStatic;
Expand Down