UNPKG

@universis/common

Version:

Universis - common directives and services

173 lines (163 loc) 6.75 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs'), require('@angular/common/http/testing'), require('@angular/core'), require('@angular/common'), require('@angular/common/http')) : typeof define === 'function' && define.amd ? define('@universis/common/testing', ['exports', 'rxjs', '@angular/common/http/testing', '@angular/core', '@angular/common', '@angular/common/http'], factory) : (factory((global.universis = global.universis || {}, global.universis.common = global.universis.common || {}, global.universis.common.testing = {}),global.rxjs,global.ng.common.http.testing,global.ng.core,global.ng.common,global.ng.common.http)); }(this, (function (exports,rxjs,testing,core,common,http) { 'use strict'; var ApiTestingController = /** @class */ (function () { function ApiTestingController() { // initialize map this.matches = new Map(); } /** * Prepares a request mapper based on the given request match * @param match */ ApiTestingController.prototype.match = function (match) { // set temp request and prepare to get request mapper this._match = match; return this; }; /** * Maps a request (based on an in-process request match) * with an instance of TestRequest class * @param mapper */ ApiTestingController.prototype.map = function (mapper) { var _this = this; if (this._match) { // search if match.url already exists var keys = Array.from(this.matches.keys()); // find key by method and url var key = keys.find(function (value) { return value.method === _this._match.method && value.url === _this._match.url; }); if (key) { // update existing item this.matches.set(key, mapper); } else { // add new this.matches.set(this._match, mapper); } this._match = null; return this; } throw new TypeError('Request match cannot be empty at this context. ' + 'Call CommonTestHttpController.match() first.'); }; /** * Finds a request mapper based on the given request matcj * @param req */ ApiTestingController.prototype.find = function (req) { // get keys var keys = Array.from(this.matches.keys()); // find key by method and url var key = keys.find(function (value) { return value.method === req.method && value.url === req.url; }); // and return request mapper if any if (key) { return this.matches.get(key); } }; ApiTestingController.decorators = [ { type: core.Injectable } ]; /** @nocollapse */ ApiTestingController.ctorParameters = function () { return []; }; return ApiTestingController; }()); var ApiTestingInterceptor = /** @class */ (function () { function ApiTestingInterceptor(httpController) { this.httpController = httpController; // } ApiTestingInterceptor.prototype.intercept = function (req, next) { var requestMapper = this.httpController.find(req); if (requestMapper == null) { return next.handle(req); } return new rxjs.Observable(function (observer) { // create an instance of test request var testReq = new testing.TestRequest(req, observer); // call request mapper requestMapper.call(null, testReq); // finalize return observer.next({ type: http.HttpEventType.Sent }); }); }; return ApiTestingInterceptor; }()); var ApiTestingModule = /** @class */ (function () { function ApiTestingModule(parentModule) { // } ApiTestingModule.forRoot = function () { return { ngModule: ApiTestingModule, providers: [ ApiTestingController, { provide: http.HTTP_INTERCEPTORS, useClass: ApiTestingInterceptor, multi: true, deps: [ApiTestingController] }, ] }; }; ApiTestingModule.decorators = [ { type: core.NgModule, args: [{ imports: [ common.CommonModule, http.HttpClientModule ] },] } ]; /** @nocollapse */ ApiTestingModule.ctorParameters = function () { return [ { type: ApiTestingModule, decorators: [{ type: core.Optional }, { type: core.SkipSelf }] } ]; }; return ApiTestingModule; }()); var TestingConfigurationService = /** @class */ (function () { function TestingConfigurationService() { this.config = { settings: { localization: { cultures: ['en', 'el'], default: 'el' } } }; this.currentLang = this.config.settings.localization.default; } Object.defineProperty(TestingConfigurationService.prototype, "settings", { get: function () { return this.config.settings; }, enumerable: true, configurable: true }); TestingConfigurationService.prototype.getCurrentLang = function () { return this.currentLang; }; TestingConfigurationService.prototype.setCurrentLang = function (lang) { this.currentLang = lang; }; return TestingConfigurationService; }()); // testing module /** * Generated bundle index. Do not edit. */ exports.ApiTestingController = ApiTestingController; exports.ApiTestingInterceptor = ApiTestingInterceptor; exports.ApiTestingModule = ApiTestingModule; exports.TestingConfigurationService = TestingConfigurationService; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=universis-common-testing.umd.js.map