2015-12-04 14:29:39 +01:00
|
|
|
import {
|
|
|
|
APP_ID,
|
|
|
|
APPLICATION_COMMON_PROVIDERS,
|
|
|
|
AppViewManager,
|
|
|
|
DirectiveResolver,
|
|
|
|
DynamicComponentLoader,
|
|
|
|
Injector,
|
|
|
|
NgZone,
|
|
|
|
Renderer,
|
|
|
|
Provider,
|
|
|
|
ViewResolver,
|
|
|
|
provide
|
|
|
|
} from 'angular2/core';
|
2015-08-28 14:39:34 -07:00
|
|
|
import {AnimationBuilder} from 'angular2/src/animate/animation_builder';
|
|
|
|
import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-10-02 07:37:23 -07:00
|
|
|
import {ProtoViewFactory} from 'angular2/src/core/linker/proto_view_factory';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {Reflector, reflector} from 'angular2/src/core/reflection/reflection';
|
2015-05-20 17:19:46 -07:00
|
|
|
import {
|
2015-07-31 12:23:50 -07:00
|
|
|
IterableDiffers,
|
|
|
|
defaultIterableDiffers,
|
|
|
|
KeyValueDiffers,
|
2015-10-01 10:07:49 -07:00
|
|
|
defaultKeyValueDiffers,
|
|
|
|
ChangeDetectorGenConfig
|
2015-08-20 14:28:25 -07:00
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
2015-12-14 20:27:31 -08:00
|
|
|
import {ExceptionHandler} from 'angular2/src/facade/exceptions';
|
2015-10-02 07:37:23 -07:00
|
|
|
import {PipeResolver} from 'angular2/src/core/linker/pipe_resolver';
|
2015-11-05 14:07:57 -08:00
|
|
|
import {XHR} from 'angular2/src/compiler/xhr';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-11-19 15:09:34 -08:00
|
|
|
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-09-08 10:14:57 -07:00
|
|
|
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
|
2015-06-24 10:54:04 +02:00
|
|
|
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
|
2015-06-22 12:14:19 -07:00
|
|
|
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
|
|
|
|
import {LocationStrategy} from 'angular2/src/router/location_strategy';
|
2015-05-07 15:38:57 +02:00
|
|
|
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-05-15 16:42:52 -07:00
|
|
|
import {TestComponentBuilder} from './test_component_builder';
|
2015-03-13 11:10:11 +01:00
|
|
|
|
2015-11-24 15:05:04 -08:00
|
|
|
import {
|
|
|
|
EventManager,
|
|
|
|
EVENT_MANAGER_PLUGINS,
|
|
|
|
ELEMENT_PROBE_PROVIDERS
|
|
|
|
} from 'angular2/platform/common_dom';
|
2015-03-13 11:10:11 +01:00
|
|
|
|
2015-11-06 17:34:07 -08:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {FunctionWrapper, Type} from 'angular2/src/facade/lang';
|
2015-03-13 11:10:11 +01:00
|
|
|
|
2015-10-02 07:37:23 -07: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 15:24:36 -08: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 12:21:29 -07:00
|
|
|
import {Serializer} from "angular2/src/web_workers/shared/serializer";
|
2015-07-15 13:26:02 -07:00
|
|
|
import {Log} from './utils';
|
2015-11-13 11:21:16 -08:00
|
|
|
import {COMPILER_PROVIDERS} from 'angular2/src/compiler/compiler';
|
2015-11-17 15:24:36 -08:00
|
|
|
import {DomRenderer_} from "angular2/src/platform/dom/dom_renderer";
|
2015-10-08 09:57:10 -07:00
|
|
|
import {DynamicComponentLoader_} from "angular2/src/core/linker/dynamic_component_loader";
|
|
|
|
import {AppViewManager_} from "angular2/src/core/linker/view_manager";
|
2015-04-07 20:54:20 -07:00
|
|
|
|
2015-03-13 11:10:11 +01:00
|
|
|
/**
|
2015-10-10 22:11:13 -07:00
|
|
|
* Returns the root injector providers.
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
|
|
|
* This must be kept in sync with the _rootBindings in application.js
|
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @returns {any[]}
|
2015-03-13 11:10:11 +01:00
|
|
|
*/
|
2015-10-10 22:11:13 -07:00
|
|
|
function _getRootProviders() {
|
2015-10-12 11:30:34 -07:00
|
|
|
return [provide(Reflector, {useValue: reflector})];
|
2015-03-13 11:10:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-10 22:11:13 -07:00
|
|
|
* Returns the application injector providers.
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
|
|
|
* This must be kept in sync with _injectorBindings() in application.js
|
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @returns {any[]}
|
2015-03-13 11:10:11 +01:00
|
|
|
*/
|
|
|
|
function _getAppBindings() {
|
2015-03-30 16:37:33 +02:00
|
|
|
var appDoc;
|
|
|
|
|
|
|
|
// The document is only available in browser environment
|
|
|
|
try {
|
2015-08-06 12:28:36 +02:00
|
|
|
appDoc = DOM.defaultDoc();
|
2015-05-20 17:19:46 -07:00
|
|
|
} catch (e) {
|
2015-03-30 16:37:33 +02:00
|
|
|
appDoc = null;
|
|
|
|
}
|
2015-07-10 16:09:18 -07:00
|
|
|
|
2015-03-13 11:10:11 +01:00
|
|
|
return [
|
2015-12-03 13:31:11 -08:00
|
|
|
APPLICATION_COMMON_PROVIDERS,
|
2015-10-28 10:06:53 -07:00
|
|
|
provide(ChangeDetectorGenConfig, {useValue: new ChangeDetectorGenConfig(true, false, true)}),
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(DOCUMENT, {useValue: appDoc}),
|
|
|
|
provide(DomRenderer, {useClass: DomRenderer_}),
|
|
|
|
provide(Renderer, {useExisting: DomRenderer}),
|
|
|
|
provide(APP_ID, {useValue: 'a'}),
|
2015-07-24 15:28:44 -07:00
|
|
|
DomSharedStylesHost,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(SharedStylesHost, {useExisting: DomSharedStylesHost}),
|
2015-04-24 17:53:06 -07:00
|
|
|
AppViewPool,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(AppViewManager, {useClass: AppViewManager_}),
|
2015-04-24 17:53:06 -07:00
|
|
|
AppViewManagerUtils,
|
2015-07-10 16:09:18 -07:00
|
|
|
Serializer,
|
2015-10-10 22:11:13 -07:00
|
|
|
ELEMENT_PROBE_PROVIDERS,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(APP_VIEW_POOL_CAPACITY, {useValue: 500}),
|
2015-10-01 10:07:49 -07:00
|
|
|
ProtoViewFactory,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(DirectiveResolver, {useClass: MockDirectiveResolver}),
|
|
|
|
provide(ViewResolver, {useClass: MockViewResolver}),
|
|
|
|
provide(IterableDiffers, {useValue: defaultIterableDiffers}),
|
|
|
|
provide(KeyValueDiffers, {useValue: defaultKeyValueDiffers}),
|
2015-07-31 12:23:50 -07:00
|
|
|
Log,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(DynamicComponentLoader, {useClass: DynamicComponentLoader_}),
|
2015-08-07 11:41:38 -07:00
|
|
|
PipeResolver,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(ExceptionHandler, {useValue: new ExceptionHandler(DOM)}),
|
|
|
|
provide(LocationStrategy, {useClass: MockLocationStrategy}),
|
2015-10-14 09:41:15 -07:00
|
|
|
provide(XHR, {useClass: DOM.getXHR()}),
|
2015-05-15 16:42:52 -07:00
|
|
|
TestComponentBuilder,
|
2015-10-12 11:30:34 -07:00
|
|
|
provide(NgZone, {useClass: MockNgZone}),
|
|
|
|
provide(AnimationBuilder, {useClass: MockAnimationBuilder}),
|
2015-09-04 14:23:13 -07:00
|
|
|
EventManager,
|
2015-10-12 11:30:34 -07:00
|
|
|
new Provider(EVENT_MANAGER_PLUGINS, {useClass: DomEventsPlugin, multi: true})
|
2015-03-13 11:10:11 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-03 13:31:11 -08:00
|
|
|
function _runtimeCompilerBindings() {
|
|
|
|
return [
|
|
|
|
provide(XHR, {useClass: DOM.getXHR()}),
|
|
|
|
COMPILER_PROVIDERS,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-14 20:27:31 -08:00
|
|
|
export function createTestInjector(providers: Array<Type | Provider | any[]>): Injector {
|
|
|
|
var rootInjector = Injector.resolveAndCreate(_getRootProviders());
|
|
|
|
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), providers));
|
2015-03-13 11:10:11 +01:00
|
|
|
}
|
|
|
|
|
2015-12-14 20:27:31 -08:00
|
|
|
export function createTestInjectorWithRuntimeCompiler(
|
|
|
|
providers: Array<Type | Provider | any[]>): Injector {
|
|
|
|
return createTestInjector(ListWrapper.concat(_runtimeCompilerBindings(), providers));
|
2015-12-03 13:31:11 -08:00
|
|
|
}
|
|
|
|
|
2015-03-13 20:52:59 +00:00
|
|
|
/**
|
2015-12-08 16:44:04 -08:00
|
|
|
* Allows injecting dependencies in `beforeEach()` and `it()`.
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2015-03-13 18:48:29 +01:00
|
|
|
* ```
|
|
|
|
* beforeEach(inject([Dependency, AClass], (dep, object) => {
|
|
|
|
* // some code that uses `dep` and `object`
|
|
|
|
* // ...
|
|
|
|
* }));
|
2015-03-13 11:10:11 +01:00
|
|
|
*
|
2015-11-18 18:46:24 -08:00
|
|
|
* it('...', inject([AClass], (object) => {
|
2015-12-08 16:44:04 -08:00
|
|
|
* object.doSomething();
|
|
|
|
* expect(...);
|
2015-03-13 18:48:29 +01:00
|
|
|
* })
|
|
|
|
* ```
|
2015-03-13 11:10:11 +01: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 11:29:19 -07:00
|
|
|
export function inject(tokens: any[], fn: Function): FunctionWithParamTokens {
|
2015-10-08 15:33:17 -07:00
|
|
|
return new FunctionWithParamTokens(tokens, fn, false);
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:46:24 -08:00
|
|
|
/**
|
2015-12-08 16:44:04 -08:00
|
|
|
* Allows injecting dependencies in `beforeEach()` and `it()`. The test must return
|
|
|
|
* a promise which will resolve when all asynchronous activity is complete.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* it('...', injectAsync([AClass], (object) => {
|
|
|
|
* return object.doSomething().then(() => {
|
|
|
|
* expect(...);
|
|
|
|
* });
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {Array} tokens
|
|
|
|
* @param {Function} fn
|
|
|
|
* @return {FunctionWithParamTokens}
|
2015-11-18 18:46:24 -08:00
|
|
|
*/
|
2015-10-08 15:33:17 -07:00
|
|
|
export function injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens {
|
|
|
|
return new FunctionWithParamTokens(tokens, fn, true);
|
2015-03-13 11:10:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class FunctionWithParamTokens {
|
2015-10-08 15:33:17 -07:00
|
|
|
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean) {}
|
2015-03-13 11:10:11 +01:00
|
|
|
|
2015-07-17 15:00:52 -07:00
|
|
|
/**
|
|
|
|
* Returns the value of the executed function.
|
|
|
|
*/
|
|
|
|
execute(injector: Injector): any {
|
2015-10-06 18:00:42 -07:00
|
|
|
var params = this._tokens.map(t => injector.get(t));
|
2015-07-17 15:00:52 -07:00
|
|
|
return FunctionWrapper.apply(this._fn, params);
|
2015-03-13 11:10:11 +01:00
|
|
|
}
|
2015-08-28 16:23:28 -07:00
|
|
|
|
|
|
|
hasToken(token: any): boolean { return this._tokens.indexOf(token) > -1; }
|
2015-03-13 11:10:11 +01:00
|
|
|
}
|