refactor(testing): move common testing logic into test_injector

Before, all test framework wrappers (internal for dart and js/ts,
angular2_test for dart and testing for js/ts) had similar logic to
keep track of current global test injector and test provider list.
This change wraps that logic into one class managed by the test
injector.

Closes #5819
This commit is contained in:
Julie Ralph
2015-12-08 19:03:21 -08:00
parent 19396d14cc
commit b88a6d983f
13 changed files with 107 additions and 102 deletions
@@ -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';
@@ -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.
@@ -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];
@@ -117,7 +114,7 @@ export function beforeEachProviders(fn): void {
jsmBeforeEach(() => {
var providers = fn();
if (!providers) return;
testProviders = [...testProviders, ...providers];
testInjector.addProviders(providers);
});
}
@@ -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);
}
@@ -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);
}