UNPKG

angularjs-testbed

Version:

Testing abstraction library inspired by [@angular/core/testing](https://angular.io/api/core/testing) for angularjs projects written in typescript with decorators

210 lines (202 loc) 8.03 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('angular'), require('tslib'), require('angular-mocks'), require('angular-ts-decorators')) : typeof define === 'function' && define.amd ? define(['exports', 'angular', 'tslib', 'angular-mocks', 'angular-ts-decorators'], factory) : (factory((global['angularjs-testbed'] = global['angularjs-testbed'] || {}),global.angular,global.tslib,null,global['angular-ts-decorators'])); }(this, (function (exports,angular,tslib_1,angularMocks,angularTsDecorators) { 'use strict'; var By = /** @class */ (function () { function By() { } By.css = function (selector) { return selector; }; return By; }()); var DebugElement = /** @class */ (function () { function DebugElement(selector) { var de = angular.element(selector); de.query = this.query; de.nativeElement = de[0]; return de; } DebugElement.prototype.query = function (selector) { return new DebugElement(selector); }; return DebugElement; }()); var ComponentFixture = /** @class */ (function () { function ComponentFixture(element$$1) { this.element = element$$1; this._isDestroyed = false; this.componentInstance = element$$1.controller(element$$1.componentName); // componentName is attached to element in TestBed _compileComponent method this.debugElement = new DebugElement(element$$1); this.nativeElement = this.debugElement.nativeElement; } /** * Trigger a change detection cycle for the component. */ ComponentFixture.prototype.detectChanges = function () { this.element.scope().$digest(); }; /** * Trigger component destruction. */ ComponentFixture.prototype.destroy = function () { if (!this._isDestroyed) { this.element.detach(); this._isDestroyed = true; } }; return ComponentFixture; }()); var _testBed = null; /** @internal */ var DynamicTestModuleId = 'DynamicTestModule'; // let _nextRootElementId = 0; /** * @whatItDoes Configures and initializes environment for unit testing and provides methods for * creating components and services in unit tests. * @description * * TestBed is the primary api for writing unit tests for Angular applications and libraries. */ var TestBed = /** @class */ (function () { function TestBed() { this._providers = []; this._declarations = []; this._imports = []; this._activeFixtures = []; this._moduleRef = null; this._instantiated = false; } /** * Allows overriding default providers, directives, pipes, modules of the test injector, * which are defined in test_injector.js */ TestBed.configureTestingModule = function (moduleDef) { getTestBed().configureTestingModule(moduleDef); return TestBed; }; TestBed.resetTestingModule = function () { getTestBed().resetTestingModule(); return TestBed; }; TestBed.compileComponents = function () { return getTestBed().compileComponents(); }; TestBed.createComponent = function (component) { return getTestBed().createComponent(component); }; TestBed.get = function (token) { token = typeof token === 'string' ? token : angularTsDecorators.getTypeName(token); return getTestBed().get(token); }; TestBed.prototype.get = function (token) { this._initIfNeeded(); if (token === TestBed) { return this; } var result = null; angular.mock.inject(["" + token, function (token) { result = token; }]); return result; }; TestBed.prototype.configureTestingModule = function (moduleDef) { var _a, _b, _c; if (moduleDef.providers) { (_a = this._providers).push.apply(_a, moduleDef.providers); } if (moduleDef.declarations) { (_b = this._declarations).push.apply(_b, moduleDef.declarations); } if (moduleDef.imports) { (_c = this._imports).push.apply(_c, moduleDef.imports); } }; TestBed.prototype.resetTestingModule = function () { this._moduleRef = null; this._providers = []; this._declarations = []; this._imports = []; this._instantiated = false; this._activeFixtures.forEach(function (fixture) { try { fixture.destroy(); } catch (e) { console.error('Error during cleanup of component', fixture.componentInstance); } }); this._activeFixtures = []; }; TestBed.prototype.compileComponents = function () { this._initIfNeeded(); }; TestBed.prototype.createComponent = function (component) { var _this = this; this._initIfNeeded(); // const componentFactory = null; // this._compiler.getComponentFactory(component); // // if (!componentFactory) { // throw new Error( // `Cannot create the component ${component['name']} as it was not imported into the testing module!`); // } // const rootElId = `root${_nextRootElementId++}`; // testComponentRenderer.insertRootElement(rootElId); var initComponent = function () { // const componentRef = componentFactory.create(null, [], `#${rootElId}`, this._moduleRef); var componentRef = _this._compileComponent(component); return new ComponentFixture(componentRef); }; var fixture = initComponent(); this._activeFixtures.push(fixture); return fixture; }; TestBed.prototype._initIfNeeded = function () { if (this._instantiated) { return; } this._moduleRef = this._createModule(); this._instantiated = true; }; TestBed.prototype._createModule = function () { // const providers = this._providers.concat([{provide: TestBed, useValue: this}]); // const declarations = [...this._declarations]; // const imports = [this.ngModule, this._imports]; var providers = this._providers; var declarations = this._declarations; var imports = this._imports; var DynamicTestModule = /** @class */ (function () { function DynamicTestModule() { } DynamicTestModule = tslib_1.__decorate([ angularTsDecorators.NgModule({ id: DynamicTestModuleId, providers: providers, declarations: declarations, imports: imports }) ], DynamicTestModule); return DynamicTestModule; }()); return angular.mock.module(DynamicTestModuleId); }; TestBed.prototype._compileComponent = function (component) { var componentName = angularTsDecorators.getTypeName(component); var selector = angularTsDecorators.camelToKebab(componentName); var $div = "<" + selector + "></" + selector + ">"; var element$$1 = null; angular.mock.inject(function ($compile, $rootScope) { var $scope = $rootScope.$new(); element$$1 = $compile($div)($scope); }); element$$1.componentName = angularTsDecorators.kebabToCamel(componentName); return element$$1; }; return TestBed; }()); function getTestBed() { return _testBed = _testBed || new TestBed(); } exports.By = By; exports.ComponentFixture = ComponentFixture; exports.DebugElement = DebugElement; exports.DynamicTestModuleId = DynamicTestModuleId; exports.TestBed = TestBed; exports.getTestBed = getTestBed; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=angularjs-testbed.umd.js.map