@universis/common
Version:
Universis - common directives and services
164 lines (156 loc) • 5.37 kB
JavaScript
import { Observable } from 'rxjs';
import { TestRequest } from '@angular/common/http/testing';
import { Injectable, NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpEventType, HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
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: 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 Observable(function (observer) {
// create an instance of test request
var testReq = new TestRequest(req, observer);
// call request mapper
requestMapper.call(null, testReq);
// finalize
return observer.next({ type: HttpEventType.Sent });
});
};
return ApiTestingInterceptor;
}());
var ApiTestingModule = /** @class */ (function () {
function ApiTestingModule(parentModule) {
//
}
ApiTestingModule.forRoot = function () {
return {
ngModule: ApiTestingModule,
providers: [
ApiTestingController,
{
provide: HTTP_INTERCEPTORS,
useClass: ApiTestingInterceptor,
multi: true,
deps: [ApiTestingController]
},
]
};
};
ApiTestingModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule,
HttpClientModule
]
},] }
];
/** @nocollapse */
ApiTestingModule.ctorParameters = function () { return [
{ type: ApiTestingModule, decorators: [{ type: Optional }, { type: 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.
*/
export { ApiTestingController, ApiTestingInterceptor, ApiTestingModule, TestingConfigurationService };
//# sourceMappingURL=universis-common-testing.js.map