2015-10-11 01:11:13 -04:00
|
|
|
import {provide, Provider} from 'angular2/src/core/di';
|
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-09-10 18:25:36 -04:00
|
|
|
import {ExceptionHandler} from 'angular2/src/core/facade/exceptions';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {ViewResolver} from 'angular2/src/core/linker/view_resolver';
|
|
|
|
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
|
|
|
|
import {PipeResolver} from 'angular2/src/core/linker/pipe_resolver';
|
|
|
|
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
|
2015-10-02 12:30:36 -04:00
|
|
|
import {XHR} from 'angular2/src/core/compiler/xhr';
|
2015-05-07 09:38:57 -04:00
|
|
|
import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-09-04 17:23:13 -04:00
|
|
|
import {
|
|
|
|
EventManager,
|
|
|
|
DomEventsPlugin,
|
|
|
|
EVENT_MANAGER_PLUGINS
|
|
|
|
} from 'angular2/src/core/render/dom/events/event_manager';
|
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-09-04 01:01:36 -04:00
|
|
|
import {Injector} from 'angular2/src/core/di';
|
2015-10-11 01:11:13 -04:00
|
|
|
import {ELEMENT_PROBE_PROVIDERS} from 'angular2/src/core/debug';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-08-28 14:29:19 -04:00
|
|
|
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {FunctionWrapper, Type} from 'angular2/src/core/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 {AppViewManager} from 'angular2/src/core/linker/view_manager';
|
|
|
|
import {AppViewManagerUtils} from 'angular2/src/core/linker/view_manager_utils';
|
2015-10-01 23:47:49 -04:00
|
|
|
import {Renderer} from 'angular2/src/core/render/api';
|
2015-07-15 13:55:44 -04:00
|
|
|
import {
|
|
|
|
DomRenderer,
|
2015-08-11 00:42:47 -04:00
|
|
|
DOCUMENT,
|
2015-07-24 18:28:44 -04:00
|
|
|
SharedStylesHost,
|
2015-10-01 23:47:49 -04:00
|
|
|
DomSharedStylesHost
|
2015-08-20 17:28:25 -04:00
|
|
|
} from 'angular2/src/core/render/render';
|
2015-10-02 14:10:08 -04:00
|
|
|
import {APP_ID} from 'angular2/src/core/application_tokens';
|
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-10-11 01:11:13 -04:00
|
|
|
import {compilerProviders} from 'angular2/src/core/compiler/compiler';
|
2015-10-08 12:57:10 -04:00
|
|
|
import {DomRenderer_} from "angular2/src/core/render/dom/dom_renderer";
|
|
|
|
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-10-11 01:11:13 -04:00
|
|
|
compilerProviders(),
|
|
|
|
provide(ChangeDetectorGenConfig,
|
2015-10-12 14:30:34 -04:00
|
|
|
{useValue: new ChangeDetectorGenConfig(true, true, false, true)}),
|
|
|
|
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-10-11 01:11:13 -04: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 06:10:11 -04:00
|
|
|
}
|
|
|
|
|
2015-03-13 16:52:59 -04:00
|
|
|
/**
|
2015-03-13 13:48:29 -04:00
|
|
|
* Allows injecting dependencies in `beforeEach()` and `it()`.
|
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-03-13 13:48:29 -04:00
|
|
|
* it('...', inject([AClass, AsyncTestCompleter], (object, async) => {
|
|
|
|
* object.doSomething().then(() => {
|
|
|
|
* expect(...);
|
|
|
|
* async.done();
|
|
|
|
* });
|
|
|
|
* })
|
|
|
|
* ```
|
2015-03-13 06:10:11 -04:00
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* - injecting an `AsyncTestCompleter` allow completing async tests - this is the equivalent of
|
|
|
|
* adding a `done` parameter in Jasmine,
|
|
|
|
* - 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens {
|
|
|
|
return new FunctionWithParamTokens(tokens, fn, true);
|
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
|
|
|
}
|