2015-12-04 08:29:39 -05:00
|
|
|
import {
|
|
|
|
APP_ID,
|
|
|
|
APPLICATION_COMMON_PROVIDERS,
|
|
|
|
AppViewManager,
|
|
|
|
DirectiveResolver,
|
|
|
|
DynamicComponentLoader,
|
|
|
|
Injector,
|
|
|
|
NgZone,
|
|
|
|
Renderer,
|
|
|
|
Provider,
|
|
|
|
ViewResolver,
|
|
|
|
provide
|
|
|
|
} from 'angular2/core';
|
2015-08-28 17:39:34 -04:00
|
|
|
import {AnimationBuilder} from 'angular2/src/animate/animation_builder';
|
|
|
|
import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-10-02 10:37:23 -04:00
|
|
|
import {ProtoViewFactory} from 'angular2/src/core/linker/proto_view_factory';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {Reflector, reflector} from 'angular2/src/core/reflection/reflection';
|
2015-05-20 20:19:46 -04:00
|
|
|
import {
|
2015-07-31 15:23:50 -04:00
|
|
|
IterableDiffers,
|
|
|
|
defaultIterableDiffers,
|
|
|
|
KeyValueDiffers,
|
2015-10-01 13:07:49 -04:00
|
|
|
defaultKeyValueDiffers,
|
|
|
|
ChangeDetectorGenConfig
|
2015-08-20 17:28:25 -04:00
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
2015-12-08 22:03:21 -05:00
|
|
|
import {BaseException, ExceptionHandler} from 'angular2/src/facade/exceptions';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {PipeResolver} from 'angular2/src/core/linker/pipe_resolver';
|
2015-11-05 17:07:57 -05:00
|
|
|
import {XHR} from 'angular2/src/compiler/xhr';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-11-19 18:09:34 -05:00
|
|
|
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-09-08 13:14:57 -04:00
|
|
|
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
|
2015-06-24 04:54:04 -04:00
|
|
|
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
|
2015-06-22 15:14:19 -04:00
|
|
|
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
|
|
|
|
import {LocationStrategy} from 'angular2/src/router/location_strategy';
|
2015-05-07 09:38:57 -04:00
|
|
|
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
import {TestComponentBuilder} from './test_component_builder';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-11-24 18:05:04 -05:00
|
|
|
import {
|
|
|
|
EventManager,
|
|
|
|
EVENT_MANAGER_PLUGINS,
|
|
|
|
ELEMENT_PROBE_PROVIDERS
|
|
|
|
} from 'angular2/platform/common_dom';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {FunctionWrapper, Type} from 'angular2/src/facade/lang';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-10-02 10:37:23 -04:00
|
|
|
import {AppViewPool, APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/linker/view_pool';
|
|
|
|
import {AppViewManagerUtils} from 'angular2/src/core/linker/view_manager_utils';
|
2015-11-17 18:24:36 -05:00
|
|
|
|
|
|
|
import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens';
|
|
|
|
import {DomRenderer} from 'angular2/src/platform/dom/dom_renderer';
|
|
|
|
import {DomSharedStylesHost} from 'angular2/src/platform/dom/shared_styles_host';
|
|
|
|
import {SharedStylesHost} from 'angular2/src/platform/dom/shared_styles_host';
|
|
|
|
import {DomEventsPlugin} from 'angular2/src/platform/dom/events/dom_events';
|
|
|
|
|
2015-08-21 15:21:29 -04:00
|
|
|
import {Serializer} from "angular2/src/web_workers/shared/serializer";
|
2015-07-15 16:26:02 -04:00
|
|
|
import {Log} from './utils';
|
2015-11-13 14:21:16 -05:00
|
|
|
import {COMPILER_PROVIDERS} from 'angular2/src/compiler/compiler';
|
2015-11-17 18:24:36 -05:00
|
|
|
import {DomRenderer_} from "angular2/src/platform/dom/dom_renderer";
|
2015-10-08 12:57:10 -04:00
|
|
|
import {DynamicComponentLoader_} from "angular2/src/core/linker/dynamic_component_loader";
|
|
|
|
import {AppViewManager_} from "angular2/src/core/linker/view_manager";
|
2015-04-07 23:54:20 -04:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Returns the root injector providers.
|
2015-03-13 06:10:11 -04:00
|
|
|
*
|
|
|
|
* This must be kept in sync with the _rootBindings in application.js
|
|
|
|
*
|
2015-04-10 06:45:02 -04:00
|
|
|
* @returns {any[]}
|
2015-03-13 06:10:11 -04:00
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
function _getRootProviders() {
|
2015-10-12 14:30:34 -04:00
|
|
|
return [provide(Reflector, {useValue: reflector})];
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Returns the application injector providers.
|
2015-03-13 06:10:11 -04:00
|
|
|
*
|
|
|
|
* This must be kept in sync with _injectorBindings() in application.js
|
|
|
|
*
|
2015-04-10 06:45:02 -04:00
|
|
|
* @returns {any[]}
|
2015-03-13 06:10:11 -04:00
|
|
|
*/
|
|
|
|
function _getAppBindings() {
|
2015-03-30 10:37:33 -04:00
|
|
|
var appDoc;
|
|
|
|
|
|
|
|
// The document is only available in browser environment
|
|
|
|
try {
|
2015-08-06 06:28:36 -04:00
|
|
|
appDoc = DOM.defaultDoc();
|
2015-05-20 20:19:46 -04:00
|
|
|
} catch (e) {
|
2015-03-30 10:37:33 -04:00
|
|
|
appDoc = null;
|
|
|
|
}
|
2015-07-10 19:09:18 -04:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
return [
|
2015-12-03 16:31:11 -05:00
|
|
|
APPLICATION_COMMON_PROVIDERS,
|
2015-10-28 13:06:53 -04:00
|
|
|
provide(ChangeDetectorGenConfig, {useValue: new ChangeDetectorGenConfig(true, false, true)}),
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(DOCUMENT, {useValue: appDoc}),
|
|
|
|
provide(DomRenderer, {useClass: DomRenderer_}),
|
|
|
|
provide(Renderer, {useExisting: DomRenderer}),
|
|
|
|
provide(APP_ID, {useValue: 'a'}),
|
2015-07-24 18:28:44 -04:00
|
|
|
DomSharedStylesHost,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(SharedStylesHost, {useExisting: DomSharedStylesHost}),
|
2015-04-24 20:53:06 -04:00
|
|
|
AppViewPool,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(AppViewManager, {useClass: AppViewManager_}),
|
2015-04-24 20:53:06 -04:00
|
|
|
AppViewManagerUtils,
|
2015-07-10 19:09:18 -04:00
|
|
|
Serializer,
|
2015-10-11 01:11:13 -04:00
|
|
|
ELEMENT_PROBE_PROVIDERS,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(APP_VIEW_POOL_CAPACITY, {useValue: 500}),
|
2015-10-01 13:07:49 -04:00
|
|
|
ProtoViewFactory,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(DirectiveResolver, {useClass: MockDirectiveResolver}),
|
|
|
|
provide(ViewResolver, {useClass: MockViewResolver}),
|
|
|
|
provide(IterableDiffers, {useValue: defaultIterableDiffers}),
|
|
|
|
provide(KeyValueDiffers, {useValue: defaultKeyValueDiffers}),
|
2015-07-31 15:23:50 -04:00
|
|
|
Log,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(DynamicComponentLoader, {useClass: DynamicComponentLoader_}),
|
2015-08-07 14:41:38 -04:00
|
|
|
PipeResolver,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(ExceptionHandler, {useValue: new ExceptionHandler(DOM)}),
|
|
|
|
provide(LocationStrategy, {useClass: MockLocationStrategy}),
|
2015-10-14 12:41:15 -04:00
|
|
|
provide(XHR, {useClass: DOM.getXHR()}),
|
2015-05-15 19:42:52 -04:00
|
|
|
TestComponentBuilder,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(NgZone, {useClass: MockNgZone}),
|
|
|
|
provide(AnimationBuilder, {useClass: MockAnimationBuilder}),
|
2015-09-04 17:23:13 -04:00
|
|
|
EventManager,
|
2015-10-12 14:30:34 -04:00
|
|
|
new Provider(EVENT_MANAGER_PLUGINS, {useClass: DomEventsPlugin, multi: true})
|
2015-03-13 06:10:11 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-03 16:31:11 -05:00
|
|
|
function _runtimeCompilerBindings() {
|
|
|
|
return [
|
|
|
|
provide(XHR, {useClass: DOM.getXHR()}),
|
|
|
|
COMPILER_PROVIDERS,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* Configures an injector suitable for testing.
|
|
|
|
*/
|
2015-12-08 22:03:21 -05:00
|
|
|
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);
|
|
|
|
}
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|
|
|
|
|
2015-12-08 22:03:21 -05:00
|
|
|
var _testInjector: TestInjector = null;
|
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* Retrieve the {@link TestInjector}, possibly creating one if it doesn't
|
|
|
|
* exist yet.
|
|
|
|
*/
|
2015-12-08 22:03:21 -05:00
|
|
|
export function getTestInjector() {
|
|
|
|
if (_testInjector == null) {
|
|
|
|
_testInjector = new TestInjector();
|
|
|
|
}
|
|
|
|
return _testInjector;
|
2015-12-03 16:31:11 -05:00
|
|
|
}
|
|
|
|
|
2015-03-13 16:52:59 -04:00
|
|
|
/**
|
2015-11-18 21:46:24 -05:00
|
|
|
* 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
|
|
|
|
* automatically complete when all asynchronous tests have finished.
|
2015-03-13 06:10:11 -04:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2015-03-13 13:48:29 -04:00
|
|
|
* ```
|
|
|
|
* beforeEach(inject([Dependency, AClass], (dep, object) => {
|
|
|
|
* // some code that uses `dep` and `object`
|
|
|
|
* // ...
|
|
|
|
* }));
|
2015-03-13 06:10:11 -04:00
|
|
|
*
|
2015-11-18 21:46:24 -05:00
|
|
|
* it('...', inject([AClass], (object) => {
|
2015-03-13 13:48:29 -04:00
|
|
|
* object.doSomething().then(() => {
|
|
|
|
* expect(...);
|
|
|
|
* });
|
|
|
|
* })
|
|
|
|
* ```
|
2015-03-13 06:10:11 -04:00
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* - inject is currently a function because of some Traceur limitation the syntax should eventually
|
|
|
|
* becomes `it('...', @Inject (object: AClass, async: AsyncTestCompleter) => { ... });`
|
|
|
|
*
|
|
|
|
* @param {Array} tokens
|
|
|
|
* @param {Function} fn
|
|
|
|
* @return {FunctionWithParamTokens}
|
|
|
|
*/
|
2015-08-28 14:29:19 -04:00
|
|
|
export function inject(tokens: any[], fn: Function): FunctionWithParamTokens {
|
2015-10-08 18:33:17 -04:00
|
|
|
return new FunctionWithParamTokens(tokens, fn, false);
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:46:24 -05:00
|
|
|
/**
|
2015-12-03 18:49:09 -05:00
|
|
|
* Use {@link inject} instead, which now supports both synchronous and asynchronous tests.
|
|
|
|
*
|
|
|
|
* @deprecated
|
2015-11-18 21:46:24 -05:00
|
|
|
*/
|
2015-10-08 18:33:17 -04:00
|
|
|
export function injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens {
|
|
|
|
return new FunctionWithParamTokens(tokens, fn, true);
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* A testing function with parameters which will be injected. See {@link inject} for details.
|
|
|
|
*/
|
2015-03-13 06:10:11 -04:00
|
|
|
export class FunctionWithParamTokens {
|
2015-10-08 18:33:17 -04:00
|
|
|
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean) {}
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-07-17 18:00:52 -04:00
|
|
|
/**
|
|
|
|
* Returns the value of the executed function.
|
|
|
|
*/
|
|
|
|
execute(injector: Injector): any {
|
2015-10-06 21:00:42 -04:00
|
|
|
var params = this._tokens.map(t => injector.get(t));
|
2015-07-17 18:00:52 -04:00
|
|
|
return FunctionWrapper.apply(this._fn, params);
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|
2015-08-28 19:23:28 -04:00
|
|
|
|
|
|
|
hasToken(token: any): boolean { return this._tokens.indexOf(token) > -1; }
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|