2015-09-03 22:01:36 -07:00
|
|
|
import {bind, Binding} from 'angular2/src/core/di';
|
|
|
|
import {DEFAULT_PIPES} from 'angular2/src/core/pipes';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-03-13 11:10:11 +01:00
|
|
|
import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler';
|
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 {
|
|
|
|
Parser,
|
|
|
|
Lexer,
|
|
|
|
ChangeDetection,
|
|
|
|
DynamicChangeDetection,
|
2015-07-31 12:23:50 -07:00
|
|
|
IterableDiffers,
|
|
|
|
defaultIterableDiffers,
|
|
|
|
KeyValueDiffers,
|
|
|
|
defaultKeyValueDiffers
|
2015-08-20 14:28:25 -07:00
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
2015-03-13 11:10:11 +01:00
|
|
|
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {ViewLoader} from 'angular2/src/core/render/dom/compiler/view_loader';
|
2015-06-24 10:54:04 +02:00
|
|
|
import {ViewResolver} from 'angular2/src/core/compiler/view_resolver';
|
2015-05-11 17:59:39 -07:00
|
|
|
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
|
2015-08-07 11:41:38 -07:00
|
|
|
import {PipeResolver} from 'angular2/src/core/compiler/pipe_resolver';
|
2015-04-06 13:19:30 -07:00
|
|
|
import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {XHR} from 'angular2/src/core/render/xhr';
|
2015-03-13 11:10:11 +01:00
|
|
|
import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {UrlResolver} from 'angular2/src/core/services/url_resolver';
|
|
|
|
import {AppRootUrl} from 'angular2/src/core/services/app_root_url';
|
|
|
|
import {AnchorBasedAppRootUrl} from 'angular2/src/core/services/anchor_based_app_root_url';
|
|
|
|
import {StyleUrlResolver} from 'angular2/src/core/render/dom/compiler/style_url_resolver';
|
|
|
|
import {StyleInliner} from 'angular2/src/core/render/dom/compiler/style_inliner';
|
2015-05-07 15:38:57 +02:00
|
|
|
import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-08-20 14:28:25 -07:00
|
|
|
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-09-04 14:23:13 -07:00
|
|
|
import {
|
|
|
|
EventManager,
|
|
|
|
DomEventsPlugin,
|
|
|
|
EVENT_MANAGER_PLUGINS
|
|
|
|
} from 'angular2/src/core/render/dom/events/event_manager';
|
2015-03-30 16:37:33 +02:00
|
|
|
|
2015-06-24 10:54:04 +02:00
|
|
|
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {MockXHR} from 'angular2/src/core/render/xhr_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-09-03 22:01:36 -07:00
|
|
|
import {Injector} from 'angular2/src/core/di';
|
|
|
|
import {ELEMENT_PROBE_BINDINGS} from 'angular2/src/core/debug';
|
2015-03-13 11:10:11 +01:00
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {FunctionWrapper, Type} from 'angular2/src/core/facade/lang';
|
2015-03-13 11:10:11 +01:00
|
|
|
|
2015-04-24 17:53:06 -07:00
|
|
|
import {AppViewPool, APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/compiler/view_pool';
|
|
|
|
import {AppViewManager} from 'angular2/src/core/compiler/view_manager';
|
|
|
|
import {AppViewManagerUtils} from 'angular2/src/core/compiler/view_manager_utils';
|
2015-04-07 20:54:20 -07:00
|
|
|
import {ProtoViewFactory} from 'angular2/src/core/compiler/proto_view_factory';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {RenderCompiler, Renderer} from 'angular2/src/core/render/api';
|
2015-07-15 10:55:44 -07:00
|
|
|
import {
|
|
|
|
DomRenderer,
|
2015-08-10 21:42:47 -07:00
|
|
|
DOCUMENT,
|
2015-07-24 15:28:44 -07:00
|
|
|
DefaultDomCompiler,
|
2015-08-10 21:42:47 -07:00
|
|
|
APP_ID,
|
2015-07-24 15:28:44 -07:00
|
|
|
SharedStylesHost,
|
2015-07-31 10:58:24 -07:00
|
|
|
DomSharedStylesHost,
|
2015-08-10 21:42:47 -07:00
|
|
|
MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE,
|
2015-07-31 10:58:24 -07:00
|
|
|
TemplateCloner
|
2015-08-20 14:28:25 -07:00
|
|
|
} from 'angular2/src/core/render/render';
|
|
|
|
import {ElementSchemaRegistry} from 'angular2/src/core/render/dom/schema/element_schema_registry';
|
|
|
|
import {
|
|
|
|
DomElementSchemaRegistry
|
|
|
|
} from 'angular2/src/core/render/dom/schema/dom_element_schema_registry';
|
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-04-07 20:54:20 -07:00
|
|
|
|
2015-03-13 11:10:11 +01:00
|
|
|
/**
|
|
|
|
* Returns the root injector bindings.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
function _getRootBindings() {
|
|
|
|
return [
|
2015-05-20 17:19:46 -07:00
|
|
|
bind(Reflector)
|
|
|
|
.toValue(reflector),
|
2015-03-13 11:10:11 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the application injector bindings.
|
|
|
|
*
|
|
|
|
* 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-08-10 21:42:47 -07:00
|
|
|
bind(DOCUMENT)
|
2015-05-20 17:19:46 -07:00
|
|
|
.toValue(appDoc),
|
2015-05-06 10:49:42 -07:00
|
|
|
DomRenderer,
|
|
|
|
bind(Renderer).toAlias(DomRenderer),
|
2015-08-10 21:42:47 -07:00
|
|
|
bind(APP_ID).toValue('a'),
|
2015-07-31 10:58:24 -07:00
|
|
|
TemplateCloner,
|
2015-08-10 21:42:47 -07:00
|
|
|
bind(MAX_IN_MEMORY_ELEMENTS_PER_TEMPLATE).toValue(-1),
|
2015-07-24 15:28:44 -07:00
|
|
|
DefaultDomCompiler,
|
2015-05-06 10:49:42 -07:00
|
|
|
bind(RenderCompiler).toAlias(DefaultDomCompiler),
|
2015-07-29 14:01:18 +02:00
|
|
|
bind(ElementSchemaRegistry).toValue(new DomElementSchemaRegistry()),
|
2015-07-24 15:28:44 -07:00
|
|
|
DomSharedStylesHost,
|
|
|
|
bind(SharedStylesHost).toAlias(DomSharedStylesHost),
|
2015-04-07 20:54:20 -07:00
|
|
|
ProtoViewFactory,
|
2015-04-24 17:53:06 -07:00
|
|
|
AppViewPool,
|
|
|
|
AppViewManager,
|
|
|
|
AppViewManagerUtils,
|
2015-07-10 16:09:18 -07:00
|
|
|
Serializer,
|
2015-08-18 14:46:35 -07:00
|
|
|
ELEMENT_PROBE_BINDINGS,
|
2015-04-24 17:53:06 -07:00
|
|
|
bind(APP_VIEW_POOL_CAPACITY).toValue(500),
|
2015-03-13 11:10:11 +01:00
|
|
|
Compiler,
|
|
|
|
CompilerCache,
|
2015-06-24 10:54:04 +02:00
|
|
|
bind(ViewResolver).toClass(MockViewResolver),
|
2015-08-07 11:41:38 -07:00
|
|
|
DEFAULT_PIPES,
|
2015-07-31 12:23:50 -07:00
|
|
|
bind(IterableDiffers).toValue(defaultIterableDiffers),
|
|
|
|
bind(KeyValueDiffers).toValue(defaultKeyValueDiffers),
|
2015-08-20 15:11:12 -07:00
|
|
|
bind(ChangeDetection).toValue(new DynamicChangeDetection()),
|
2015-07-31 12:23:50 -07:00
|
|
|
Log,
|
2015-06-24 10:43:36 +02:00
|
|
|
ViewLoader,
|
2015-04-06 13:19:30 -07:00
|
|
|
DynamicComponentLoader,
|
2015-05-11 17:59:39 -07:00
|
|
|
DirectiveResolver,
|
2015-08-07 11:41:38 -07:00
|
|
|
PipeResolver,
|
2015-03-13 11:10:11 +01:00
|
|
|
Parser,
|
|
|
|
Lexer,
|
2015-07-23 18:00:19 -07:00
|
|
|
bind(ExceptionHandler).toValue(new ExceptionHandler(DOM)),
|
2015-06-22 12:14:19 -07:00
|
|
|
bind(LocationStrategy).toClass(MockLocationStrategy),
|
2015-03-31 16:55:46 +02:00
|
|
|
bind(XHR).toClass(MockXHR),
|
2015-03-13 11:10:11 +01:00
|
|
|
ComponentUrlMapper,
|
|
|
|
UrlResolver,
|
2015-07-10 16:09:18 -07:00
|
|
|
AnchorBasedAppRootUrl,
|
|
|
|
bind(AppRootUrl).toAlias(AnchorBasedAppRootUrl),
|
2015-03-13 11:10:11 +01:00
|
|
|
StyleUrlResolver,
|
2015-03-30 16:37:33 +02:00
|
|
|
StyleInliner,
|
2015-05-15 16:42:52 -07:00
|
|
|
TestComponentBuilder,
|
2015-05-07 15:38:57 +02:00
|
|
|
bind(NgZone).toClass(MockNgZone),
|
2015-09-04 14:23:13 -07:00
|
|
|
EventManager,
|
|
|
|
new Binding(EVENT_MANAGER_PLUGINS, {toClass: DomEventsPlugin, multi: true})
|
2015-03-13 11:10:11 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
export function createTestInjector(bindings: Array<Type | Binding | any[]>): Injector {
|
2015-04-10 17:05:31 -07:00
|
|
|
var rootInjector = Injector.resolveAndCreate(_getRootBindings());
|
|
|
|
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), bindings));
|
2015-03-13 11:10:11 +01:00
|
|
|
}
|
|
|
|
|
2015-03-13 20:52:59 +00:00
|
|
|
/**
|
2015-03-13 18:48:29 +01: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-03-13 18:48:29 +01:00
|
|
|
* it('...', inject([AClass, AsyncTestCompleter], (object, async) => {
|
|
|
|
* object.doSomething().then(() => {
|
|
|
|
* expect(...);
|
|
|
|
* async.done();
|
|
|
|
* });
|
|
|
|
* })
|
|
|
|
* ```
|
2015-03-13 11:10:11 +01: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 11:29:19 -07:00
|
|
|
export function inject(tokens: any[], fn: Function): FunctionWithParamTokens {
|
2015-03-13 11:10:11 +01:00
|
|
|
return new FunctionWithParamTokens(tokens, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FunctionWithParamTokens {
|
2015-08-28 16:23:28 -07:00
|
|
|
constructor(private _tokens: any[], private _fn: Function) {}
|
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-03-13 11:10:11 +01:00
|
|
|
var params = ListWrapper.map(this._tokens, (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
|
|
|
}
|