UNPKG

angular-http-interceptor

Version:
260 lines (259 loc) 10.2 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { Component, NgModule } from "@angular/core"; import { TestBed, inject, fakeAsync, tick } from "@angular/core/testing"; import { By } from '@angular/platform-browser'; import { InterceptorModule, INTERCEPTORS } from "./interceptor.module"; import { Response, Http, HttpModule, XHRBackend, ResponseOptions } from "@angular/http"; import { MockBackend } from "@angular/http/testing"; import { Interceptor } from "./interfaces"; import { Observable } from "rxjs/Observable"; import "rxjs/add/observable/empty"; import { CustomHttp } from "."; var CustomInterceptorImpl = /** @class */ (function () { function CustomInterceptorImpl() { } CustomInterceptorImpl.prototype.before = function (request) { this.beforeRequest = request; return Observable.empty(); }; CustomInterceptorImpl.prototype.after = function (response) { this.afterResponse = response; }; CustomInterceptorImpl.prototype.error = function (error) { this.errorCall = error; }; return CustomInterceptorImpl; }()); var CustomInterceptor = /** @class */ (function (_super) { __extends(CustomInterceptor, _super); function CustomInterceptor() { return _super !== null && _super.apply(this, arguments) || this; } return CustomInterceptor; }(CustomInterceptorImpl)); var CustomInterceptor2 = /** @class */ (function (_super) { __extends(CustomInterceptor2, _super); function CustomInterceptor2() { return _super !== null && _super.apply(this, arguments) || this; } return CustomInterceptor2; }(CustomInterceptorImpl)); var CustomInterceptor3 = /** @class */ (function (_super) { __extends(CustomInterceptor3, _super); function CustomInterceptor3() { return _super !== null && _super.apply(this, arguments) || this; } return CustomInterceptor3; }(CustomInterceptorImpl)); var interceptor3 = new CustomInterceptor3(); var fixture; var comp; describe('interceptor-module', function () { var interceptor1; var interceptor2; beforeEach(function () { interceptor1 = new CustomInterceptor(); interceptor2 = new CustomInterceptor2(); spyOn(interceptor1, 'before').and.callThrough(); spyOn(interceptor2, 'before').and.callThrough(); spyOn(interceptor3, 'before').and.callThrough(); TestBed.configureTestingModule({ imports: [ HttpModule, InterceptorModule, OtherInterceptorModule ], providers: [ { provide: XHRBackend, useClass: MockBackend }, { provide: Interceptor, useValue: interceptor1, multi: true }, { provide: Interceptor, useValue: interceptor2, multi: true } ], declarations: [AppComponent] }); // // create component and test fixture fixture = TestBed.createComponent(AppComponent); // // get test component from the fixture comp = fixture.componentInstance; }); it('should inject CustomHttp in hello world component', function () { fixture.detectChanges(); var debugElement = fixture.debugElement.query(By.css("h1")); var element = debugElement.nativeElement; expect(element.textContent).toContain('Hello'); expect(comp.http).not.toBeNull(); }); it('should call all interceptors', fakeAsync(inject([CustomHttp, XHRBackend], function (http, backend) { var body = JSON.stringify({ success: true }); backend.connections.subscribe(function (connection) { var options = new ResponseOptions({ body: body }); connection.mockRespond(new Response(options)); }); http.get("fake").subscribe(); tick(100); expect(interceptor1.before).toHaveBeenCalled(); expect(interceptor2.before).toHaveBeenCalled(); expect(interceptor3.before).toHaveBeenCalled(); expect(http.interceptors.length).toEqual(3); }))); }); describe('interceptor-module-withInterceptors', function () { var interceptor1; var interceptor2; beforeEach(function () { interceptor1 = new CustomInterceptor(); interceptor2 = new CustomInterceptor2(); spyOn(interceptor1, 'before').and.callThrough(); spyOn(interceptor2, 'before').and.callThrough(); spyOn(interceptor3, 'before').and.callThrough(); TestBed.configureTestingModule({ imports: [ HttpModule, OtherInterceptorModule, InterceptorModule.withInterceptors([{ provide: INTERCEPTORS, useValue: interceptor1, multi: true }]) ], providers: [ { provide: XHRBackend, useClass: MockBackend }, { provide: Interceptor, useValue: interceptor2, multi: true } ], declarations: [AppComponent] }); // // create component and test fixture fixture = TestBed.createComponent(AppComponent); // // get test component from the fixture comp = fixture.componentInstance; }); it('should inject CustomHttp in hello world component', function () { fixture.detectChanges(); var debugElement = fixture.debugElement.query(By.css("h1")); var element = debugElement.nativeElement; expect(element.textContent).toContain('Hello'); expect(comp.http).not.toBeNull(); }); it('should inject interceptors', fakeAsync(inject([CustomHttp, XHRBackend], function (http, backend) { var body = JSON.stringify({ success: true }); backend.connections.subscribe(function (connection) { var options = new ResponseOptions({ body: body }); connection.mockRespond(new Response(options)); }); http.get("fake").subscribe(); tick(100); expect(interceptor1.before).toHaveBeenCalled(); expect(interceptor2.before).not.toHaveBeenCalled(); expect(interceptor3.before).not.toHaveBeenCalled(); expect(http.interceptors.length).toEqual(1); }))); }); describe('interceptor-module-accept-multi-false', function () { var interceptor1; var interceptor2; beforeEach(function () { interceptor1 = new CustomInterceptor(); interceptor2 = new CustomInterceptor2(); spyOn(interceptor1, 'before').and.callThrough(); spyOn(interceptor2, 'before').and.callThrough(); TestBed.configureTestingModule({ imports: [ HttpModule, InterceptorModule.withInterceptors([{ provide: INTERCEPTORS, useValue: interceptor1 }, { provide: Interceptor, useValue: interceptor2 }]) ], providers: [ { provide: XHRBackend, useClass: MockBackend } ], declarations: [AppComponent] }); // // create component and test fixture fixture = TestBed.createComponent(AppComponent); // // get test component from the fixture comp = fixture.componentInstance; }); it('should inject interceptors', fakeAsync(inject([CustomHttp, XHRBackend], function (http, backend) { var body = JSON.stringify({ success: true }); backend.connections.subscribe(function (connection) { var options = new ResponseOptions({ body: body }); connection.mockRespond(new Response(options)); }); http.get("fake").subscribe(); tick(100); expect(interceptor1.before).toHaveBeenCalled(); expect(interceptor2.before).not.toHaveBeenCalled(); expect(http.interceptors.length).toEqual(1); }))); }); var AppComponent = /** @class */ (function () { function AppComponent(http) { this.http = http; } AppComponent.decorators = [ { type: Component, args: [{ selector: 'test-app-component', template: '<h1>Hello</h1>' },] }, ]; /** @nocollapse */ AppComponent.ctorParameters = function () { return [ { type: Http, }, ]; }; return AppComponent; }()); var OtherInterceptorModule = /** @class */ (function () { function OtherInterceptorModule() { } OtherInterceptorModule.decorators = [ { type: NgModule, args: [{ imports: [ InterceptorModule ], providers: [ { provide: Interceptor, useValue: interceptor3, multi: true } ] },] }, ]; return OtherInterceptorModule; }());