refactor(test_injector): Provide separate methods for creating test injector with and without runtime compiler.

BREAKING CHANGE:
`createTestInjector()` does not more include the runtime compiler. Use `createTestInjectorWithRuntimeCompiler()` instead.
Closes #5583
This commit is contained in:
Tobias Bosch 2015-12-03 13:31:11 -08:00
parent 0a3a17ff10
commit 0614797d84
10 changed files with 39 additions and 17 deletions

View File

@ -57,6 +57,7 @@ import {COMPILER_PROVIDERS} from 'angular2/src/compiler/compiler';
import {DomRenderer_} from "angular2/src/platform/dom/dom_renderer"; import {DomRenderer_} from "angular2/src/platform/dom/dom_renderer";
import {DynamicComponentLoader_} from "angular2/src/core/linker/dynamic_component_loader"; import {DynamicComponentLoader_} from "angular2/src/core/linker/dynamic_component_loader";
import {AppViewManager_} from "angular2/src/core/linker/view_manager"; import {AppViewManager_} from "angular2/src/core/linker/view_manager";
import {APPLICATION_COMMON_PROVIDERS} from 'angular2/src/core/application_common_providers';
/** /**
* Returns the root injector providers. * Returns the root injector providers.
@ -87,7 +88,7 @@ function _getAppBindings() {
} }
return [ return [
COMPILER_PROVIDERS, APPLICATION_COMMON_PROVIDERS,
provide(ChangeDetectorGenConfig, {useValue: new ChangeDetectorGenConfig(true, false, true)}), provide(ChangeDetectorGenConfig, {useValue: new ChangeDetectorGenConfig(true, false, true)}),
provide(DOCUMENT, {useValue: appDoc}), provide(DOCUMENT, {useValue: appDoc}),
provide(DomRenderer, {useClass: DomRenderer_}), provide(DomRenderer, {useClass: DomRenderer_}),
@ -120,11 +121,23 @@ function _getAppBindings() {
]; ];
} }
function _runtimeCompilerBindings() {
return [
provide(XHR, {useClass: DOM.getXHR()}),
COMPILER_PROVIDERS,
];
}
export function createTestInjector(providers: Array<Type | Provider | any[]>): Injector { export function createTestInjector(providers: Array<Type | Provider | any[]>): Injector {
var rootInjector = Injector.resolveAndCreate(_getRootProviders()); var rootInjector = Injector.resolveAndCreate(_getRootProviders());
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), providers)); return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), providers));
} }
export function createTestInjectorWithRuntimeCompiler(
providers: Array<Type | Provider | any[]>): Injector {
return createTestInjector(ListWrapper.concat(_runtimeCompilerBindings(), providers));
}
/** /**
* Allows injecting dependencies in `beforeEach()` and `it()`. When using with the * Allows injecting dependencies in `beforeEach()` and `it()`. When using with the
* `angular2/testing` library, the test function will be run within a zone and will * `angular2/testing` library, the test function will be run within a zone and will

View File

@ -6,7 +6,12 @@ import {global} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection'; import {ListWrapper} from 'angular2/src/facade/collection';
import {bind} from 'angular2/src/core/di'; import {bind} from 'angular2/src/core/di';
import {createTestInjector, FunctionWithParamTokens, inject, injectAsync} from './test_injector'; import {
createTestInjectorWithRuntimeCompiler,
FunctionWithParamTokens,
inject,
injectAsync
} from './test_injector';
export {inject, injectAsync} from './test_injector'; export {inject, injectAsync} from './test_injector';
@ -150,7 +155,7 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
if (testFn instanceof FunctionWithParamTokens) { if (testFn instanceof FunctionWithParamTokens) {
jsmFn(name, (done) => { jsmFn(name, (done) => {
if (!injector) { if (!injector) {
injector = createTestInjector(testProviders); injector = createTestInjectorWithRuntimeCompiler(testProviders);
} }
var returnedTestValue = runInTestZone(() => testFn.execute(injector), done, done.fail); var returnedTestValue = runInTestZone(() => testFn.execute(injector), done, done.fail);
@ -179,7 +184,7 @@ export function beforeEach(fn: FunctionWithParamTokens | AnyTestFn): void {
jsmBeforeEach((done) => { jsmBeforeEach((done) => {
if (!injector) { if (!injector) {
injector = createTestInjector(testProviders); injector = createTestInjectorWithRuntimeCompiler(testProviders);
} }
runInTestZone(() => fn.execute(injector), done, done.fail); runInTestZone(() => fn.execute(injector), done, done.fail);

View File

@ -63,7 +63,7 @@ void testSetup() {
gns.beforeEach(() { gns.beforeEach(() {
_isCurrentTestAsync = false; _isCurrentTestAsync = false;
_testBindings.add(completerBinding); _testBindings.add(completerBinding);
_injector = createTestInjector(_testBindings); _injector = createTestInjectorWithRuntimeCompiler(_testBindings);
}, priority: 1); }, priority: 1);
} }

View File

@ -5,7 +5,11 @@ import {NgZoneZone} from 'angular2/src/core/zone/ng_zone';
import {provide} from 'angular2/src/core/di'; import {provide} from 'angular2/src/core/di';
import {createTestInjector, FunctionWithParamTokens, inject} from './test_injector'; import {
createTestInjectorWithRuntimeCompiler,
FunctionWithParamTokens,
inject
} from './test_injector';
import {browserDetection} from './utils'; import {browserDetection} from './utils';
export {inject} from './test_injector'; export {inject} from './test_injector';
@ -143,7 +147,7 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
} }
}); });
var injector = createTestInjector([...testProviders, completerProvider]); var injector = createTestInjectorWithRuntimeCompiler([...testProviders, completerProvider]);
runner.run(injector); runner.run(injector);
inIt = true; inIt = true;
@ -152,7 +156,7 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
}, timeOut); }, timeOut);
} else { } else {
jsmFn(name, () => { jsmFn(name, () => {
var injector = createTestInjector(testProviders); var injector = createTestInjectorWithRuntimeCompiler(testProviders);
runner.run(injector); runner.run(injector);
testFn.execute(injector); testFn.execute(injector);
}, timeOut); }, timeOut);
@ -163,13 +167,13 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
if ((<any>testFn).length === 0) { if ((<any>testFn).length === 0) {
jsmFn(name, () => { jsmFn(name, () => {
var injector = createTestInjector(testProviders); var injector = createTestInjectorWithRuntimeCompiler(testProviders);
runner.run(injector); runner.run(injector);
(<SyncTestFn>testFn)(); (<SyncTestFn>testFn)();
}, timeOut); }, timeOut);
} else { } else {
jsmFn(name, (done) => { jsmFn(name, (done) => {
var injector = createTestInjector(testProviders); var injector = createTestInjectorWithRuntimeCompiler(testProviders);
runner.run(injector); runner.run(injector);
(<AsyncTestFn>testFn)(done); (<AsyncTestFn>testFn)(done);
}, timeOut); }, timeOut);

View File

@ -5,7 +5,7 @@ import {
it, it,
expect, expect,
beforeEach, beforeEach,
createTestInjector, createTestInjectorWithRuntimeCompiler,
beforeEachProviders, beforeEachProviders,
SpyObject, SpyObject,
proxy proxy

View File

@ -5,7 +5,7 @@ import {
it, it,
expect, expect,
beforeEach, beforeEach,
createTestInjector, createTestInjectorWithRuntimeCompiler,
beforeEachProviders, beforeEachProviders,
SpyObject, SpyObject,
proxy proxy

View File

@ -5,7 +5,7 @@ import {
it, it,
expect, expect,
beforeEach, beforeEach,
createTestInjector, createTestInjectorWithRuntimeCompiler,
beforeEachProviders, beforeEachProviders,
SpyObject, SpyObject,
proxy proxy

View File

@ -7,7 +7,7 @@ import {
iit, iit,
expect, expect,
beforeEach, beforeEach,
createTestInjector, createTestInjectorWithRuntimeCompiler,
beforeEachProviders, beforeEachProviders,
TestComponentBuilder TestComponentBuilder
} from "angular2/testing_internal"; } from "angular2/testing_internal";
@ -101,7 +101,7 @@ export function main() {
beforeEachProviders(() => { beforeEachProviders(() => {
var uiRenderProtoViewStore = new RenderProtoViewRefStore(false); var uiRenderProtoViewStore = new RenderProtoViewRefStore(false);
uiRenderViewStore = new RenderViewWithFragmentsStore(false); uiRenderViewStore = new RenderViewWithFragmentsStore(false);
uiInjector = createTestInjector([ uiInjector = createTestInjectorWithRuntimeCompiler([
provide(RenderProtoViewRefStore, {useValue: uiRenderProtoViewStore}), provide(RenderProtoViewRefStore, {useValue: uiRenderProtoViewStore}),
provide(RenderViewWithFragmentsStore, {useValue: uiRenderViewStore}), provide(RenderViewWithFragmentsStore, {useValue: uiRenderViewStore}),
provide(DomRenderer, {useClass: DomRenderer_}), provide(DomRenderer, {useClass: DomRenderer_}),

View File

@ -5,7 +5,7 @@ import {
it, it,
expect, expect,
beforeEach, beforeEach,
createTestInjector, createTestInjectorWithRuntimeCompiler,
beforeEachProviders beforeEachProviders
} from 'angular2/testing_internal'; } from 'angular2/testing_internal';
import {SpyMessageBroker} from './spies'; import {SpyMessageBroker} from './spies';

View File

@ -73,7 +73,7 @@ dynamic _runInjectableFunction(Function fn) {
} }
if (_currentInjector == null) { if (_currentInjector == null) {
_currentInjector = createTestInjector(_currentTestProviders); _currentInjector = createTestInjectorWithRuntimeCompiler(_currentTestProviders);
} }
var injectFn = new FunctionWithParamTokens(tokens, fn, false); var injectFn = new FunctionWithParamTokens(tokens, fn, false);
return injectFn.execute(_currentInjector); return injectFn.execute(_currentInjector);