X Tutup
Skip to content

Commit 2aaef81

Browse files
committed
Revert "refactor(testing): move common testing logic into test_injector"
This reverts commit b88a6d9.
1 parent 3191fd1 commit 2aaef81

13 files changed

+102
-119
lines changed

modules/angular2/src/testing/test_injector.ts

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
defaultKeyValueDiffers,
2424
ChangeDetectorGenConfig
2525
} from 'angular2/src/core/change_detection/change_detection';
26-
import {BaseException, ExceptionHandler} from 'angular2/src/facade/exceptions';
26+
import {ExceptionHandler} from 'angular2/src/facade/exceptions';
2727
import {PipeResolver} from 'angular2/src/core/linker/pipe_resolver';
2828
import {XHR} from 'angular2/src/compiler/xhr';
2929

@@ -131,56 +131,14 @@ function _runtimeCompilerBindings() {
131131
];
132132
}
133133

134-
/**
135-
* Configures an injector suitable for testing.
136-
*/
137-
export class TestInjector {
138-
private _instantiated: boolean = false;
139-
140-
private _injector: Injector = null;
141-
142-
private _providers: Array<Type | Provider | any[]> = [];
143-
144-
reset() {
145-
this._injector = null;
146-
this._providers = [];
147-
this._instantiated = false;
148-
}
149-
150-
addProviders(providers: Array<Type | Provider | any[]>) {
151-
if (this._instantiated) {
152-
throw new BaseException('Cannot add providers after test injector is instantiated');
153-
}
154-
this._providers = ListWrapper.concat(this._providers, providers);
155-
}
156-
157-
createInjector() {
158-
var rootInjector = Injector.resolveAndCreate(_getRootProviders());
159-
this._injector = rootInjector.resolveAndCreateChild(ListWrapper.concat(
160-
ListWrapper.concat(_getAppBindings(), _runtimeCompilerBindings()), this._providers));
161-
this._instantiated = true;
162-
return this._injector;
163-
}
164-
165-
execute(fn: FunctionWithParamTokens): any {
166-
if (!this._instantiated) {
167-
this.createInjector();
168-
}
169-
return fn.execute(this._injector);
170-
}
134+
export function createTestInjector(providers: Array<Type | Provider | any[]>): Injector {
135+
var rootInjector = Injector.resolveAndCreate(_getRootProviders());
136+
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), providers));
171137
}
172138

173-
var _testInjector: TestInjector = null;
174-
175-
/**
176-
* Retrieve the {@link TestInjector}, possibly creating one if it doesn't
177-
* exist yet.
178-
*/
179-
export function getTestInjector() {
180-
if (_testInjector == null) {
181-
_testInjector = new TestInjector();
182-
}
183-
return _testInjector;
139+
export function createTestInjectorWithRuntimeCompiler(
140+
providers: Array<Type | Provider | any[]>): Injector {
141+
return createTestInjector(ListWrapper.concat(_runtimeCompilerBindings(), providers));
184142
}
185143

186144
/**
@@ -216,17 +174,12 @@ export function inject(tokens: any[], fn: Function): FunctionWithParamTokens {
216174
}
217175

218176
/**
219-
* Use {@link inject} instead, which now supports both synchronous and asynchronous tests.
220-
*
221-
* @deprecated
177+
* @deprecated Use inject instead, which now supports both synchronous and asynchronous tests.
222178
*/
223179
export function injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens {
224180
return new FunctionWithParamTokens(tokens, fn, true);
225181
}
226182

227-
/**
228-
* A testing function with parameters which will be injected. See {@link inject} for details.
229-
*/
230183
export class FunctionWithParamTokens {
231184
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean) {}
232185

modules/angular2/src/testing/testing.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import {ListWrapper} from 'angular2/src/facade/collection';
77
import {bind} from 'angular2/core';
88

99
import {
10+
createTestInjectorWithRuntimeCompiler,
1011
FunctionWithParamTokens,
1112
inject,
12-
injectAsync,
13-
TestInjector,
14-
getTestInjector
13+
injectAsync
1514
} from './test_injector';
1615

1716
export {inject, injectAsync} from './test_injector';
@@ -93,10 +92,14 @@ var jsmIt = _global.it;
9392
var jsmIIt = _global.fit;
9493
var jsmXIt = _global.xit;
9594

96-
var testInjector: TestInjector = getTestInjector();
95+
var testProviders;
96+
var injector;
9797

9898
// Reset the test providers before each test.
99-
jsmBeforeEach(() => { testInjector.reset(); });
99+
jsmBeforeEach(() => {
100+
testProviders = [];
101+
injector = null;
102+
});
100103

101104
/**
102105
* Allows overriding default providers of the test injector,
@@ -112,9 +115,8 @@ export function beforeEachProviders(fn): void {
112115
jsmBeforeEach(() => {
113116
var providers = fn();
114117
if (!providers) return;
115-
try {
116-
testInjector.addProviders(providers);
117-
} catch (e) {
118+
testProviders = [...testProviders, ...providers];
119+
if (injector !== null) {
118120
throw new Error('beforeEachProviders was called after the injector had ' +
119121
'been used in a beforeEach or it block. This invalidates the ' +
120122
'test injector');
@@ -186,7 +188,11 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
186188

187189
if (testFn instanceof FunctionWithParamTokens) {
188190
jsmFn(name, (done) => {
189-
var returnedTestValue = runInTestZone(() => testInjector.execute(testFn), done, done.fail);
191+
if (!injector) {
192+
injector = createTestInjectorWithRuntimeCompiler(testProviders);
193+
}
194+
195+
var returnedTestValue = runInTestZone(() => testFn.execute(injector), done, done.fail);
190196
if (_isPromiseLike(returnedTestValue)) {
191197
(<Promise<any>>returnedTestValue).then(null, (err) => { done.fail(err); });
192198
}
@@ -215,7 +221,13 @@ export function beforeEach(fn: FunctionWithParamTokens | AnyTestFn): void {
215221
// The test case uses inject(). ie `beforeEach(inject([ClassA], (a) => { ...
216222
// }));`
217223

218-
jsmBeforeEach((done) => { runInTestZone(() => testInjector.execute(fn), done, done.fail); });
224+
jsmBeforeEach((done) => {
225+
if (!injector) {
226+
injector = createTestInjectorWithRuntimeCompiler(testProviders);
227+
}
228+
229+
runInTestZone(() => fn.execute(injector), done, done.fail);
230+
});
219231
} else {
220232
// The test case doesn't use inject(). ie `beforeEach((done) => { ... }));`
221233
if ((<any>fn).length === 0) {

modules/angular2/src/testing/testing_internal.dart

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,20 @@ import 'package:angular2/src/core/reflection/reflection.dart';
2121
import 'package:angular2/src/core/reflection/reflection_capabilities.dart';
2222

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

2627
import 'test_injector.dart';
2728
export 'test_injector.dart' show inject;
2829

29-
TestInjector _testInjector = getTestInjector();
30+
List _testBindings = [];
31+
Injector _injector;
3032
bool _isCurrentTestAsync;
31-
Future _currentTestFuture;
3233
bool _inIt = false;
3334

3435
class AsyncTestCompleter {
3536
final _completer = new Completer();
3637

37-
AsyncTestCompleter() {
38-
_currentTestFuture = this.future;
39-
}
40-
4138
void done() {
4239
_completer.complete();
4340
}
@@ -53,11 +50,10 @@ void testSetup() {
5350
// - Priority 1: create the test injector to be used in beforeEach() and it()
5451

5552
gns.beforeEach(() {
56-
_testInjector.reset();
57-
_currentTestFuture = null;
53+
_testBindings.clear();
5854
}, priority: 3);
5955

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

6763
gns.beforeEach(() {
6864
_isCurrentTestAsync = false;
69-
_testInjector.addProviders([completerProvider]);
65+
_testBindings.add(completerBinding);
66+
_injector = createTestInjectorWithRuntimeCompiler(_testBindings);
7067
}, priority: 1);
7168
}
7269

7370
/**
74-
* Allows overriding default providers defined in test_injector.js.
71+
* Allows overriding default bindings defined in test_injector.js.
7572
*
76-
* The given function must return a list of DI providers.
73+
* The given function must return a list of DI bindings.
7774
*
7875
* Example:
7976
*
@@ -84,8 +81,8 @@ void testSetup() {
8481
*/
8582
void beforeEachProviders(Function fn) {
8683
gns.beforeEach(() {
87-
var providers = fn();
88-
if (providers != null) _testInjector.addProviders(providers);
84+
var bindings = fn();
85+
if (bindings != null) _testBindings.addAll(bindings);
8986
}, priority: 2);
9087
}
9188

@@ -98,7 +95,7 @@ void beforeEach(fn) {
9895
if (fn is! FunctionWithParamTokens) fn =
9996
new FunctionWithParamTokens([], fn, false);
10097
gns.beforeEach(() {
101-
_testInjector.execute(fn);
98+
fn.execute(_injector);
10299
});
103100
}
104101

@@ -107,9 +104,9 @@ void _it(gnsFn, name, fn) {
107104
new FunctionWithParamTokens([], fn, false);
108105
gnsFn(name, () {
109106
_inIt = true;
110-
_testInjector.execute(fn);
107+
fn.execute(_injector);
111108
_inIt = false;
112-
if (_isCurrentTestAsync) return _currentTestFuture;
109+
if (_isCurrentTestAsync) return _injector.get(AsyncTestCompleter).future;
113110
});
114111
}
115112

modules/angular2/src/testing/testing_internal.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import {NgZoneZone} from 'angular2/src/core/zone/ng_zone';
55

66
import {provide} from 'angular2/core';
77

8-
import {TestInjector, getTestInjector, FunctionWithParamTokens, inject} from './test_injector';
8+
import {
9+
createTestInjectorWithRuntimeCompiler,
10+
FunctionWithParamTokens,
11+
inject
12+
} from './test_injector';
913
import {browserDetection} from './utils';
1014

1115
export {inject} from './test_injector';
@@ -44,7 +48,7 @@ var inIt = false;
4448
jasmine.DEFAULT_TIMEOUT_INTERVAL = 500;
4549
var globalTimeOut = browserDetection.isSlow ? 3000 : jasmine.DEFAULT_TIMEOUT_INTERVAL;
4650

47-
var testInjector = getTestInjector();
51+
var testProviders;
4852

4953
/**
5054
* Mechanism to run `beforeEach()` functions of Angular tests.
@@ -58,17 +62,16 @@ class BeforeEachRunner {
5862

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

61-
run(): void {
62-
if (this._parent) this._parent.run();
65+
run(injector): void {
66+
if (this._parent) this._parent.run(injector);
6367
this._fns.forEach((fn) => {
64-
return isFunction(fn) ? (<SyncTestFn>fn)() :
65-
(testInjector.execute(<FunctionWithParamTokens>fn));
68+
return isFunction(fn) ? (<SyncTestFn>fn)() : (<FunctionWithParamTokens>fn).execute(injector);
6669
});
6770
}
6871
}
6972

7073
// Reset the test providers before each test
71-
jsmBeforeEach(() => { testInjector.reset(); });
74+
jsmBeforeEach(() => { testProviders = []; });
7275

7376
function _describe(jsmFn, ...args) {
7477
var parentRunner = runnerStack.length === 0 ? null : runnerStack[runnerStack.length - 1];
@@ -117,7 +120,7 @@ export function beforeEachProviders(fn): void {
117120
jsmBeforeEach(() => {
118121
var providers = fn();
119122
if (!providers) return;
120-
testInjector.addProviders(providers);
123+
testProviders = [...testProviders, ...providers];
121124
});
122125
}
123126

@@ -147,17 +150,18 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
147150
}
148151
});
149152

150-
testInjector.addProviders([completerProvider]);
151-
runner.run();
153+
var injector = createTestInjectorWithRuntimeCompiler([...testProviders, completerProvider]);
154+
runner.run(injector);
152155

153156
inIt = true;
154-
testInjector.execute(testFn);
157+
testFn.execute(injector);
155158
inIt = false;
156159
}, timeOut);
157160
} else {
158161
jsmFn(name, () => {
159-
runner.run();
160-
testInjector.execute(testFn);
162+
var injector = createTestInjectorWithRuntimeCompiler(testProviders);
163+
runner.run(injector);
164+
testFn.execute(injector);
161165
}, timeOut);
162166
}
163167

@@ -166,12 +170,14 @@ function _it(jsmFn: Function, name: string, testFn: FunctionWithParamTokens | An
166170

167171
if ((<any>testFn).length === 0) {
168172
jsmFn(name, () => {
169-
runner.run();
173+
var injector = createTestInjectorWithRuntimeCompiler(testProviders);
174+
runner.run(injector);
170175
(<SyncTestFn>testFn)();
171176
}, timeOut);
172177
} else {
173178
jsmFn(name, (done) => {
174-
runner.run();
179+
var injector = createTestInjectorWithRuntimeCompiler(testProviders);
180+
runner.run(injector);
175181
(<AsyncTestFn>testFn)(done);
176182
}, timeOut);
177183
}

modules/angular2/test/web_workers/debug_tools/multi_client_server_message_bus.server.spec.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import "package:angular2/testing_internal.dart"
1111
iit,
1212
expect,
1313
beforeEach,
14+
createTestInjector,
1415
beforeEachProviders,
1516
SpyObject,
1617
proxy;

modules/angular2/test/web_workers/debug_tools/single_client_server_message_bus.server.spec.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "package:angular2/testing_internal.dart"
1010
it,
1111
expect,
1212
beforeEach,
13+
createTestInjector,
1314
beforeEachProviders,
1415
SpyObject,
1516
proxy;

modules/angular2/test/web_workers/debug_tools/web_socket_message_bus_spec.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "package:angular2/testing_internal.dart"
88
it,
99
expect,
1010
beforeEach,
11+
createTestInjector,
1112
beforeEachProviders,
1213
SpyObject,
1314
proxy;

modules/angular2/test/web_workers/shared/message_bus_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
it,
66
expect,
77
beforeEach,
8+
createTestInjectorWithRuntimeCompiler,
89
beforeEachProviders,
910
SpyObject,
1011
proxy

modules/angular2/test/web_workers/shared/service_message_broker_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
it,
66
expect,
77
beforeEach,
8+
createTestInjectorWithRuntimeCompiler,
89
beforeEachProviders,
910
SpyObject,
1011
proxy

modules/angular2/test/web_workers/worker/event_dispatcher_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
it,
66
expect,
77
beforeEach,
8+
createTestInjectorWithRuntimeCompiler,
89
beforeEachProviders,
910
SpyObject,
1011
proxy

0 commit comments

Comments
 (0)
X Tutup