UNPKG

@universis/common

Version:

Universis - common directives and services

165 lines (157 loc) 5.75 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule, Optional, SkipSelf } from '@angular/core'; import { HttpEventType, HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { Observable } from 'rxjs'; import { TestRequest } from '@angular/common/http/testing'; import { CommonModule } from '@angular/common'; class ApiTestingController { constructor() { // initialize map this.matches = new Map(); } /** * Prepares a request mapper based on the given request match * @param match */ match(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 */ map(mapper) { if (this._match != null) { // search if match.url already exists const keys = Array.from(this.matches.keys()); // find key by method and url const key = keys.find(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 match * @param req */ find(req) { // get keys const keys = Array.from(this.matches.keys()); // find key by method and url const key = keys.find(value => { return value.method === req.method && value.url === req.url; }); // and return request mapper if any if (key) { return this.matches.get(key); } return; } } ApiTestingController.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingController, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); ApiTestingController.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingController }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingController, decorators: [{ type: Injectable }], ctorParameters: function () { return []; } }); class ApiTestingInterceptor { constructor(httpController) { this.httpController = httpController; // } intercept(req, next) { const requestMapper = this.httpController.find(req); if (requestMapper == null) { return next.handle(req); } return new Observable((observer) => { // create an instance of test request const testReq = new TestRequest(req, observer); // call request mapper requestMapper.call(null, testReq); // finalize return observer.next({ type: HttpEventType.Sent }); }); } } class ApiTestingModule { constructor(parentModule) { // } static forRoot() { return { ngModule: ApiTestingModule, providers: [ ApiTestingController, { provide: HTTP_INTERCEPTORS, useClass: ApiTestingInterceptor, multi: true, deps: [ApiTestingController] }, ] }; } } ApiTestingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingModule, deps: [{ token: ApiTestingModule, optional: true, skipSelf: true }], target: i0.ɵɵFactoryTarget.NgModule }); ApiTestingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingModule, imports: [CommonModule, HttpClientModule] }); ApiTestingModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingModule, imports: [CommonModule, HttpClientModule] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: ApiTestingModule, decorators: [{ type: NgModule, args: [{ imports: [ CommonModule, HttpClientModule ] }] }], ctorParameters: function () { return [{ type: ApiTestingModule, decorators: [{ type: Optional }, { type: SkipSelf }] }]; } }); class TestingConfigurationService { constructor() { this.config = { settings: { localization: { cultures: ['en', 'el'], default: 'el' } } }; this.currentLang = this.config.settings.localization.default; } get settings() { return this.config.settings; } getCurrentLang() { return this.currentLang; } setCurrentLang(lang) { this.currentLang = lang; } } // testing module /** * Generated bundle index. Do not edit. */ export { ApiTestingController, ApiTestingInterceptor, ApiTestingModule, TestingConfigurationService }; //# sourceMappingURL=universis-common-testing.mjs.map