X Tutup
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
49 changes: 42 additions & 7 deletions modules/angular2/src/testing/test_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
defaultKeyValueDiffers,
ChangeDetectorGenConfig
} from 'angular2/src/core/change_detection/change_detection';
import {ExceptionHandler} from 'angular2/src/facade/exceptions';
import {BaseException, ExceptionHandler} from 'angular2/src/facade/exceptions';
import {PipeResolver} from 'angular2/src/core/linker/pipe_resolver';
import {XHR} from 'angular2/src/compiler/xhr';

Expand Down Expand Up @@ -131,14 +131,49 @@ function _runtimeCompilerBindings() {
];
}

export function createTestInjector(providers: Array<Type | Provider | any[]>): Injector {
var rootInjector = Injector.resolveAndCreate(_getRootProviders());
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), providers));
export class TestInjector {
private _instantiated: boolean = false;

private _injector: Injector = null;

private _providers: Array<Type | Provider | any[]> = [];

reset() {
this._injector = null;
this._providers = [];
this._instantiated = false;
}

addProviders(providers: Array<Type | Provider | any[]>) {
if (this._instantiated) {
throw new BaseException('Cannot add providers after test injector is instantiated');
}
this._providers = ListWrapper.concat(this._providers, providers);
}

createInjector() {
var rootInjector = Injector.resolveAndCreate(_getRootProviders());
this._injector = rootInjector.resolveAndCreateChild(ListWrapper.concat(
ListWrapper.concat(_getAppBindings(), _runtimeCompilerBindings()), this._providers));
this._instantiated = true;
return this._injector;
}

execute(fn: FunctionWithParamTokens): any {
if (!this._instantiated) {
this.createInjector();
}
return fn.execute(this._injector);
}
}

export function createTestInjectorWithRuntimeCompiler(
providers: Array<Type | Provider | any[]>): Injector {
return createTestInjector(ListWrapper.concat(_runtimeCompilerBindings(), providers));
var _testInjector: TestInjector = null;

export function getTestInjector() {
if (_testInjector == null) {
_testInjector = new TestInjector();
}
return _testInjector;
}

/**
Expand Down
32 changes: 10 additions & 22 deletions modules/angular2/src/testing/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {ListWrapper} from 'angular2/src/facade/collection';
import {bind} from 'angular2/core';

import {
createTestInjectorWithRuntimeCompiler,
FunctionWithParamTokens,
inject,
injectAsync
injectAsync,
TestInjector,
getTestInjector
} from './test_injector';

export {inject, injectAsync} from './test_injector';
Expand Down Expand Up @@ -80,14 +81,10 @@ var jsmIt = _global.it;
var jsmIIt = _global.fit;
var jsmXIt = _global.xit;

var testProviders;
var injector;
var testInjector: TestInjector = getTestInjector();

// Reset the test providers before each test.
jsmBeforeEach(() => {
testProviders = [];
injector = null;
});
jsmBeforeEach(() => { testInjector.reset(); });

/**
* Allows overriding default providers of the test injector,
Expand All @@ -103,8 +100,9 @@ export function beforeEachProviders(fn): void {
jsmBeforeEach(() => {
var providers = fn();
if (!providers) return;
testProviders = [...testProviders, ...providers];
if (injector !== null) {
try {
testInjector.addProviders(providers);
} catch (e) {
throw new Error('beforeEachProviders was called after the injector had ' +
'been used in a beforeEach or it block. This invalidates the ' +
'test injector');
Expand Down Expand Up @@ -176,11 +174,7 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An

if (testFn instanceof FunctionWithParamTokens) {
jsmFn(name, (done) => {
if (!injector) {
injector = createTestInjectorWithRuntimeCompiler(testProviders);
}

var returnedTestValue = runInTestZone(() => testFn.execute(injector), done, done.fail);
var returnedTestValue = runInTestZone(() => testInjector.execute(testFn), done, done.fail);
if (_isPromiseLike(returnedTestValue)) {
(<Promise<any>>returnedTestValue).then(null, (err) => { done.fail(err); });
}
Expand Down Expand Up @@ -209,13 +203,7 @@ export function beforeEach(fn: FunctionWithParamTokens | AnyTestFn): void {
// The test case uses inject(). ie `beforeEach(inject([ClassA], (a) => { ...
// }));`

jsmBeforeEach((done) => {
if (!injector) {
injector = createTestInjectorWithRuntimeCompiler(testProviders);
}

runInTestZone(() => fn.execute(injector), done, done.fail);
});
jsmBeforeEach((done) => { runInTestZone(() => testInjector.execute(fn), done, done.fail); });
} else {
// The test case doesn't use inject(). ie `beforeEach((done) => { ... }));`
if ((<any>fn).length === 0) {
Expand Down
31 changes: 17 additions & 14 deletions modules/angular2/src/testing/testing_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/core/reflection/reflection_capabilities.dart';

import 'package:angular2/src/core/di/provider.dart' show bind;
import 'package:angular2/src/core/di/injector.dart' show Injector;
import 'package:angular2/src/facade/collection.dart' show StringMapWrapper;

import 'test_injector.dart';
export 'test_injector.dart' show inject;

List _testBindings = [];
Injector _injector;
TestInjector _testInjector = getTestInjector();
bool _isCurrentTestAsync;
Future _currentTestFuture;
bool _inIt = false;

class AsyncTestCompleter {
final _completer = new Completer();

AsyncTestCompleter() {
_currentTestFuture = this.future;
}

void done() {
_completer.complete();
}
Expand All @@ -50,10 +53,11 @@ void testSetup() {
// - Priority 1: create the test injector to be used in beforeEach() and it()

gns.beforeEach(() {
_testBindings.clear();
_testInjector.reset();
_currentTestFuture = null;
}, priority: 3);

var completerBinding = bind(AsyncTestCompleter).toFactory(() {
var completerProvider = bind(AsyncTestCompleter).toFactory(() {
// Mark the test as async when an AsyncTestCompleter is injected in an it(),
if (!_inIt) throw 'AsyncTestCompleter can only be injected in an "it()"';
_isCurrentTestAsync = true;
Expand All @@ -62,15 +66,14 @@ void testSetup() {

gns.beforeEach(() {
_isCurrentTestAsync = false;
_testBindings.add(completerBinding);
_injector = createTestInjectorWithRuntimeCompiler(_testBindings);
_testInjector.addProviders([completerProvider]);
}, priority: 1);
}

/**
* Allows overriding default bindings defined in test_injector.js.
* Allows overriding default providers defined in test_injector.js.
*
* The given function must return a list of DI bindings.
* The given function must return a list of DI providers.
*
* Example:
*
Expand All @@ -81,8 +84,8 @@ void testSetup() {
*/
void beforeEachProviders(Function fn) {
gns.beforeEach(() {
var bindings = fn();
if (bindings != null) _testBindings.addAll(bindings);
var providers = fn();
if (providers != null) _testInjector.addProviders(providers);
}, priority: 2);
}

Expand All @@ -95,7 +98,7 @@ void beforeEach(fn) {
if (fn is! FunctionWithParamTokens) fn =
new FunctionWithParamTokens([], fn, false);
gns.beforeEach(() {
fn.execute(_injector);
_testInjector.execute(fn);
});
}

Expand All @@ -104,9 +107,9 @@ void _it(gnsFn, name, fn) {
new FunctionWithParamTokens([], fn, false);
gnsFn(name, () {
_inIt = true;
fn.execute(_injector);
_testInjector.execute(fn);
_inIt = false;
if (_isCurrentTestAsync) return _injector.get(AsyncTestCompleter).future;
if (_isCurrentTestAsync) return _currentTestFuture;
});
}

Expand Down
36 changes: 15 additions & 21 deletions modules/angular2/src/testing/testing_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import {NgZoneZone} from 'angular2/src/core/zone/ng_zone';

import {provide} from 'angular2/core';

import {
createTestInjectorWithRuntimeCompiler,
FunctionWithParamTokens,
inject
} from './test_injector';
import {TestInjector, getTestInjector, FunctionWithParamTokens, inject} from './test_injector';
import {browserDetection} from './utils';

export {inject} from './test_injector';
Expand Down Expand Up @@ -45,7 +41,7 @@ var inIt = false;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 500;
var globalTimeOut = browserDetection.isSlow ? 3000 : jasmine.DEFAULT_TIMEOUT_INTERVAL;

var testProviders;
var testInjector = getTestInjector();

/**
* Mechanism to run `beforeEach()` functions of Angular tests.
Expand All @@ -59,16 +55,17 @@ class BeforeEachRunner {

beforeEach(fn: FunctionWithParamTokens | SyncTestFn): void { this._fns.push(fn); }

run(injector): void {
if (this._parent) this._parent.run(injector);
run(): void {
if (this._parent) this._parent.run();
this._fns.forEach((fn) => {
return isFunction(fn) ? (<SyncTestFn>fn)() : (<FunctionWithParamTokens>fn).execute(injector);
return isFunction(fn) ? (<SyncTestFn>fn)() :
(testInjector.execute(<FunctionWithParamTokens>fn));
});
}
}

// Reset the test providers before each test
jsmBeforeEach(() => { testProviders = []; });
jsmBeforeEach(() => { testInjector.reset(); });

function _describe(jsmFn, ...args) {
var parentRunner = runnerStack.length === 0 ? null : runnerStack[runnerStack.length - 1];
Expand Down Expand Up @@ -117,7 +114,7 @@ export function beforeEachProviders(fn): void {
jsmBeforeEach(() => {
var providers = fn();
if (!providers) return;
testProviders = [...testProviders, ...providers];
testInjector.addProviders(providers);
});
}

Expand Down Expand Up @@ -147,18 +144,17 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
}
});

var injector = createTestInjectorWithRuntimeCompiler([...testProviders, completerProvider]);
runner.run(injector);
testInjector.addProviders([completerProvider]);
runner.run();

inIt = true;
testFn.execute(injector);
testInjector.execute(testFn);
inIt = false;
}, timeOut);
} else {
jsmFn(name, () => {
var injector = createTestInjectorWithRuntimeCompiler(testProviders);
runner.run(injector);
testFn.execute(injector);
runner.run();
testInjector.execute(testFn);
}, timeOut);
}

Expand All @@ -167,14 +163,12 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An

if ((<any>testFn).length === 0) {
jsmFn(name, () => {
var injector = createTestInjectorWithRuntimeCompiler(testProviders);
runner.run(injector);
runner.run();
(<SyncTestFn>testFn)();
}, timeOut);
} else {
jsmFn(name, (done) => {
var injector = createTestInjectorWithRuntimeCompiler(testProviders);
runner.run(injector);
runner.run();
(<AsyncTestFn>testFn)(done);
}, timeOut);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import "package:angular2/testing_internal.dart"
iit,
expect,
beforeEach,
createTestInjector,
beforeEachProviders,
SpyObject,
proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import "package:angular2/testing_internal.dart"
it,
expect,
beforeEach,
createTestInjector,
beforeEachProviders,
SpyObject,
proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import "package:angular2/testing_internal.dart"
it,
expect,
beforeEach,
createTestInjector,
beforeEachProviders,
SpyObject,
proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
it,
expect,
beforeEach,
createTestInjectorWithRuntimeCompiler,
beforeEachProviders,
SpyObject,
proxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
it,
expect,
beforeEach,
createTestInjectorWithRuntimeCompiler,
beforeEachProviders,
SpyObject,
proxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
it,
expect,
beforeEach,
createTestInjectorWithRuntimeCompiler,
beforeEachProviders,
SpyObject,
proxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
iit,
expect,
beforeEach,
createTestInjectorWithRuntimeCompiler,
beforeEachProviders,
TestInjector,
TestComponentBuilder
} from "angular2/testing_internal";
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
Expand Down Expand Up @@ -102,12 +102,14 @@ export function main() {
beforeEachProviders(() => {
var uiRenderProtoViewStore = new RenderProtoViewRefStore(false);
uiRenderViewStore = new RenderViewWithFragmentsStore(false);
uiInjector = createTestInjectorWithRuntimeCompiler([
var testInjector = new TestInjector();
testInjector.addProviders([
provide(RenderProtoViewRefStore, {useValue: uiRenderProtoViewStore}),
provide(RenderViewWithFragmentsStore, {useValue: uiRenderViewStore}),
provide(DomRenderer, {useClass: DomRenderer_}),
provide(Renderer, {useExisting: DomRenderer})
]);
uiInjector = testInjector.createInjector();
var uiSerializer = uiInjector.get(Serializer);
var domRenderer = uiInjector.get(DomRenderer);
var workerRenderProtoViewStore = new RenderProtoViewRefStore(true);
Expand Down
Loading
X Tutup