UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

224 lines 9.79 kB
'use strict';var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); switch (arguments.length) { case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); } }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var di_1 = require('angular2/src/core/di'); var lang_1 = require('angular2/src/facade/lang'); var collection_1 = require('angular2/src/facade/collection'); var directive_resolver_1 = require('angular2/src/core/linker/directive_resolver'); var view_resolver_1 = require('angular2/src/core/linker/view_resolver'); var view_ref_1 = require('angular2/src/core/linker/view_ref'); var dynamic_component_loader_1 = require('angular2/src/core/linker/dynamic_component_loader'); var utils_1 = require('./utils'); var dom_tokens_1 = require('angular2/src/platform/dom/dom_tokens'); var dom_adapter_1 = require('angular2/src/platform/dom/dom_adapter'); var debug_element_1 = require('angular2/src/core/debug/debug_element'); /** * @deprecated Use ComponentFixture */ var RootTestComponent = (function () { function RootTestComponent() { } return RootTestComponent; })(); exports.RootTestComponent = RootTestComponent; var ComponentFixture = (function (_super) { __extends(ComponentFixture, _super); function ComponentFixture() { _super.apply(this, arguments); } return ComponentFixture; })(RootTestComponent); exports.ComponentFixture = ComponentFixture; var ComponentFixture_ = (function (_super) { __extends(ComponentFixture_, _super); function ComponentFixture_(componentRef) { _super.call(this); this.debugElement = new debug_element_1.DebugElement_(view_ref_1.internalView(componentRef.hostView), 0); this._componentParentView = view_ref_1.internalView(componentRef.hostView); this._componentRef = componentRef; } ComponentFixture_.prototype.detectChanges = function () { this._componentParentView.changeDetector.detectChanges(); this._componentParentView.changeDetector.checkNoChanges(); }; ComponentFixture_.prototype.destroy = function () { this._componentRef.dispose(); }; return ComponentFixture_; })(ComponentFixture); exports.ComponentFixture_ = ComponentFixture_; var _nextRootElementId = 0; /** * Builds a ComponentFixture for use in component level tests. */ var TestComponentBuilder = (function () { function TestComponentBuilder(_injector) { this._injector = _injector; /** @internal */ this._bindingsOverrides = new Map(); /** @internal */ this._directiveOverrides = new Map(); /** @internal */ this._templateOverrides = new Map(); /** @internal */ this._viewBindingsOverrides = new Map(); /** @internal */ this._viewOverrides = new Map(); } /** @internal */ TestComponentBuilder.prototype._clone = function () { var clone = new TestComponentBuilder(this._injector); clone._viewOverrides = collection_1.MapWrapper.clone(this._viewOverrides); clone._directiveOverrides = collection_1.MapWrapper.clone(this._directiveOverrides); clone._templateOverrides = collection_1.MapWrapper.clone(this._templateOverrides); return clone; }; /** * Overrides only the html of a {@link ComponentMetadata}. * All the other properties of the component's {@link ViewMetadata} are preserved. * * @param {Type} component * @param {string} html * * @return {TestComponentBuilder} */ TestComponentBuilder.prototype.overrideTemplate = function (componentType, template) { var clone = this._clone(); clone._templateOverrides.set(componentType, template); return clone; }; /** * Overrides a component's {@link ViewMetadata}. * * @param {Type} component * @param {view} View * * @return {TestComponentBuilder} */ TestComponentBuilder.prototype.overrideView = function (componentType, view) { var clone = this._clone(); clone._viewOverrides.set(componentType, view); return clone; }; /** * Overrides the directives from the component {@link ViewMetadata}. * * @param {Type} component * @param {Type} from * @param {Type} to * * @return {TestComponentBuilder} */ TestComponentBuilder.prototype.overrideDirective = function (componentType, from, to) { var clone = this._clone(); var overridesForComponent = clone._directiveOverrides.get(componentType); if (!lang_1.isPresent(overridesForComponent)) { clone._directiveOverrides.set(componentType, new Map()); overridesForComponent = clone._directiveOverrides.get(componentType); } overridesForComponent.set(from, to); return clone; }; /** * Overrides one or more injectables configured via `providers` metadata property of a directive * or * component. * Very useful when certain providers need to be mocked out. * * The providers specified via this method are appended to the existing `providers` causing the * duplicated providers to * be overridden. * * @param {Type} component * @param {any[]} providers * * @return {TestComponentBuilder} */ TestComponentBuilder.prototype.overrideProviders = function (type, providers) { var clone = this._clone(); clone._bindingsOverrides.set(type, providers); return clone; }; /** * @deprecated */ TestComponentBuilder.prototype.overrideBindings = function (type, providers) { return this.overrideProviders(type, providers); }; /** * Overrides one or more injectables configured via `providers` metadata property of a directive * or * component. * Very useful when certain providers need to be mocked out. * * The providers specified via this method are appended to the existing `providers` causing the * duplicated providers to * be overridden. * * @param {Type} component * @param {any[]} providers * * @return {TestComponentBuilder} */ TestComponentBuilder.prototype.overrideViewProviders = function (type, providers) { var clone = this._clone(); clone._viewBindingsOverrides.set(type, providers); return clone; }; /** * @deprecated */ TestComponentBuilder.prototype.overrideViewBindings = function (type, providers) { return this.overrideViewProviders(type, providers); }; /** * Builds and returns a ComponentFixture. * * @return {Promise<ComponentFixture>} */ TestComponentBuilder.prototype.createAsync = function (rootComponentType) { var mockDirectiveResolver = this._injector.get(directive_resolver_1.DirectiveResolver); var mockViewResolver = this._injector.get(view_resolver_1.ViewResolver); this._viewOverrides.forEach(function (view, type) { return mockViewResolver.setView(type, view); }); this._templateOverrides.forEach(function (template, type) { return mockViewResolver.setInlineTemplate(type, template); }); this._directiveOverrides.forEach(function (overrides, component) { overrides.forEach(function (to, from) { mockViewResolver.overrideViewDirective(component, from, to); }); }); this._bindingsOverrides.forEach(function (bindings, type) { return mockDirectiveResolver.setBindingsOverride(type, bindings); }); this._viewBindingsOverrides.forEach(function (bindings, type) { return mockDirectiveResolver.setViewBindingsOverride(type, bindings); }); var rootElId = "root" + _nextRootElementId++; var rootEl = utils_1.el("<div id=\"" + rootElId + "\"></div>"); var doc = this._injector.get(dom_tokens_1.DOCUMENT); // TODO(juliemr): can/should this be optional? var oldRoots = dom_adapter_1.DOM.querySelectorAll(doc, '[id^=root]'); for (var i = 0; i < oldRoots.length; i++) { dom_adapter_1.DOM.remove(oldRoots[i]); } dom_adapter_1.DOM.appendChild(doc.body, rootEl); return this._injector.get(dynamic_component_loader_1.DynamicComponentLoader) .loadAsRoot(rootComponentType, "#" + rootElId, this._injector) .then(function (componentRef) { return new ComponentFixture_(componentRef); }); }; TestComponentBuilder = __decorate([ di_1.Injectable(), __metadata('design:paramtypes', [di_1.Injector]) ], TestComponentBuilder); return TestComponentBuilder; })(); exports.TestComponentBuilder = TestComponentBuilder; //# sourceMappingURL=test_component_builder.js.map