2015-05-20 20:19:46 -04:00
|
|
|
import {bind, Binding} from 'angular2/di';
|
2015-03-30 10:37:33 -04:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
import {Compiler, CompilerCache} from 'angular2/src/core/compiler/compiler';
|
|
|
|
import {Reflector, reflector} from 'angular2/src/reflection/reflection';
|
2015-05-20 20:19:46 -04:00
|
|
|
import {
|
|
|
|
Parser,
|
|
|
|
Lexer,
|
|
|
|
ChangeDetection,
|
|
|
|
DynamicChangeDetection,
|
|
|
|
PipeRegistry,
|
|
|
|
defaultPipeRegistry
|
|
|
|
} from 'angular2/change_detection';
|
2015-03-13 06:10:11 -04:00
|
|
|
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
|
2015-04-02 17:40:49 -04:00
|
|
|
import {TemplateLoader} from 'angular2/src/render/dom/compiler/template_loader';
|
2015-03-13 06:10:11 -04:00
|
|
|
import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver';
|
2015-05-11 20:59:39 -04:00
|
|
|
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
|
2015-04-06 16:19:30 -04:00
|
|
|
import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader';
|
2015-04-07 23:54:20 -04:00
|
|
|
import {ShadowDomStrategy} from 'angular2/src/render/dom/shadow_dom/shadow_dom_strategy';
|
2015-05-20 20:19:46 -04:00
|
|
|
import {
|
|
|
|
EmulatedUnscopedShadowDomStrategy
|
|
|
|
} from 'angular2/src/render/dom/shadow_dom/emulated_unscoped_shadow_dom_strategy';
|
2015-06-09 13:21:25 -04:00
|
|
|
import {XHR} from 'angular2/src/render/xhr';
|
2015-03-13 06:10:11 -04:00
|
|
|
import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
|
2015-04-02 12:52:00 -04:00
|
|
|
import {UrlResolver} from 'angular2/src/services/url_resolver';
|
2015-04-02 17:40:49 -04:00
|
|
|
import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver';
|
|
|
|
import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner';
|
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
|
|
|
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
|
|
|
|
import {EventManager, DomEventsPlugin} from 'angular2/src/render/dom/events/event_manager';
|
|
|
|
|
|
|
|
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
|
2015-06-09 13:21:25 -04:00
|
|
|
import {MockXHR} from 'angular2/src/render/xhr_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
|
|
|
|
|
|
|
import {TestBed} from './test_bed';
|
2015-05-15 19:42:52 -04:00
|
|
|
import {TestComponentBuilder} from './test_component_builder';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
|
|
|
import {Injector} from 'angular2/di';
|
|
|
|
|
|
|
|
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
2015-05-20 20:19:46 -04:00
|
|
|
import {FunctionWrapper, Type} from 'angular2/src/facade/lang';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-04-24 20:53:06 -04: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-05-28 18:02:20 -04:00
|
|
|
import {ELEMENT_PROBE_CONFIG} from 'angular2/debug';
|
2015-04-07 23:54:20 -04:00
|
|
|
import {ProtoViewFactory} from 'angular2/src/core/compiler/proto_view_factory';
|
2015-05-06 13:17:38 -04:00
|
|
|
import {RenderCompiler, Renderer} from 'angular2/src/render/api';
|
2015-05-06 13:49:42 -04:00
|
|
|
import {DomRenderer, DOCUMENT_TOKEN} from 'angular2/src/render/dom/dom_renderer';
|
|
|
|
import {DefaultDomCompiler} from 'angular2/src/render/dom/compiler/compiler';
|
2015-04-07 23:54:20 -04:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
/**
|
|
|
|
* Returns the root injector bindings.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
function _getRootBindings() {
|
|
|
|
return [
|
2015-05-20 20:19:46 -04:00
|
|
|
bind(Reflector)
|
|
|
|
.toValue(reflector),
|
2015-03-13 06:10:11 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the application injector bindings.
|
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
appDoc = DOM.defaultDoc();
|
2015-05-20 20:19:46 -04:00
|
|
|
} catch (e) {
|
2015-03-30 10:37:33 -04:00
|
|
|
appDoc = null;
|
|
|
|
}
|
2015-03-13 06:10:11 -04:00
|
|
|
return [
|
2015-05-20 20:19:46 -04:00
|
|
|
bind(DOCUMENT_TOKEN)
|
|
|
|
.toValue(appDoc),
|
|
|
|
bind(ShadowDomStrategy)
|
2015-06-18 04:07:43 -04:00
|
|
|
.toFactory((styleInliner, styleUrlResolver, doc) => new EmulatedUnscopedShadowDomStrategy(
|
|
|
|
styleInliner, styleUrlResolver, doc.head),
|
|
|
|
[StyleInliner, StyleUrlResolver, DOCUMENT_TOKEN]),
|
2015-05-06 13:49:42 -04:00
|
|
|
DomRenderer,
|
|
|
|
DefaultDomCompiler,
|
|
|
|
bind(Renderer).toAlias(DomRenderer),
|
|
|
|
bind(RenderCompiler).toAlias(DefaultDomCompiler),
|
2015-04-07 23:54:20 -04:00
|
|
|
ProtoViewFactory,
|
2015-04-24 20:53:06 -04:00
|
|
|
AppViewPool,
|
|
|
|
AppViewManager,
|
|
|
|
AppViewManagerUtils,
|
2015-05-28 18:02:20 -04:00
|
|
|
ELEMENT_PROBE_CONFIG,
|
2015-04-24 20:53:06 -04:00
|
|
|
bind(APP_VIEW_POOL_CAPACITY).toValue(500),
|
2015-03-13 06:10:11 -04:00
|
|
|
Compiler,
|
|
|
|
CompilerCache,
|
2015-03-30 10:37:33 -04:00
|
|
|
bind(TemplateResolver).toClass(MockTemplateResolver),
|
2015-04-13 12:50:02 -04:00
|
|
|
bind(PipeRegistry).toValue(defaultPipeRegistry),
|
|
|
|
bind(ChangeDetection).toClass(DynamicChangeDetection),
|
2015-03-13 06:10:11 -04:00
|
|
|
TemplateLoader,
|
2015-04-06 16:19:30 -04:00
|
|
|
DynamicComponentLoader,
|
2015-05-11 20:59:39 -04:00
|
|
|
DirectiveResolver,
|
2015-03-13 06:10:11 -04:00
|
|
|
Parser,
|
|
|
|
Lexer,
|
|
|
|
ExceptionHandler,
|
2015-06-22 15:14:19 -04:00
|
|
|
bind(LocationStrategy).toClass(MockLocationStrategy),
|
2015-03-31 10:55:46 -04:00
|
|
|
bind(XHR).toClass(MockXHR),
|
2015-03-13 06:10:11 -04:00
|
|
|
ComponentUrlMapper,
|
|
|
|
UrlResolver,
|
|
|
|
StyleUrlResolver,
|
2015-03-30 10:37:33 -04:00
|
|
|
StyleInliner,
|
|
|
|
TestBed,
|
2015-05-15 19:42:52 -04:00
|
|
|
TestComponentBuilder,
|
2015-05-07 09:38:57 -04:00
|
|
|
bind(NgZone).toClass(MockNgZone),
|
2015-05-20 20:19:46 -04:00
|
|
|
bind(EventManager)
|
2015-06-03 16:42:57 -04:00
|
|
|
.toFactory(
|
|
|
|
(zone) => {
|
|
|
|
var plugins = [
|
|
|
|
new DomEventsPlugin(),
|
|
|
|
];
|
|
|
|
return new EventManager(plugins, zone);
|
|
|
|
},
|
|
|
|
[NgZone]),
|
2015-03-13 06:10:11 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:19:46 -04:00
|
|
|
export function createTestInjector(bindings: List<Type | Binding | List<any>>): Injector {
|
2015-04-10 20:05:31 -04:00
|
|
|
var rootInjector = Injector.resolveAndCreate(_getRootBindings());
|
|
|
|
return rootInjector.resolveAndCreateChild(ListWrapper.concat(_getAppBindings(), bindings));
|
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-04-14 19:26:49 -04:00
|
|
|
* @exportedAs angular2/test
|
2015-03-13 06:10:11 -04:00
|
|
|
*/
|
2015-05-20 20:19:46 -04:00
|
|
|
export function inject(tokens: List<any>, fn: Function): FunctionWithParamTokens {
|
2015-03-13 06:10:11 -04:00
|
|
|
return new FunctionWithParamTokens(tokens, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FunctionWithParamTokens {
|
2015-05-20 20:19:46 -04:00
|
|
|
_tokens: List<any>;
|
2015-03-13 06:10:11 -04:00
|
|
|
_fn: Function;
|
|
|
|
|
2015-05-20 20:19:46 -04:00
|
|
|
constructor(tokens: List<any>, fn: Function) {
|
2015-03-13 06:10:11 -04:00
|
|
|
this._tokens = tokens;
|
|
|
|
this._fn = fn;
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:19:46 -04:00
|
|
|
execute(injector: Injector): void {
|
2015-03-13 06:10:11 -04:00
|
|
|
var params = ListWrapper.map(this._tokens, (t) => injector.get(t));
|
|
|
|
FunctionWrapper.apply(this._fn, params);
|
|
|
|
}
|
|
|
|
}
|