From 400c0827758ec03732f225d053d12a5352a748f1 Mon Sep 17 00:00:00 2001 From: Netanel Basal Date: Tue, 17 Apr 2018 08:27:43 +0300 Subject: [PATCH] feat(http): expose get method for getting the service --- src/app/todos-data.service.spec.ts | 46 ++++++++++++++++++++++++------ src/app/todos-data.service.ts | 36 ++++++++++++++++++----- src/lib/src/http.ts | 16 +++++++++-- 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/src/app/todos-data.service.spec.ts b/src/app/todos-data.service.spec.ts index d89e2eb9..e0a04a74 100644 --- a/src/app/todos-data.service.spec.ts +++ b/src/app/todos-data.service.spec.ts @@ -1,23 +1,51 @@ -import { TodosDataService } from './todos-data.service'; -import { createHTTPFactory, HTTPMethod } from '../lib/src/http'; +import { TodosDataService, UserService } from "./todos-data.service"; +import { createHTTPFactory, HTTPMethod } from "../lib/src/http"; +import { mockProvider } from "../lib/src"; +import { defer } from "rxjs/observable/defer"; +import { fakeAsync, tick } from "@angular/core/testing"; -describe('HttpClient testing', () => { - let http = createHTTPFactory(TodosDataService); +describe("HttpClient testing", () => { + let http = createHTTPFactory(TodosDataService, [ + mockProvider(UserService) + ]); - it('can test HttpClient.get', () => { + it("can test HttpClient.get", () => { let { dataService, controller, expectOne } = http(); dataService.get().subscribe(); - expectOne('url', HTTPMethod.GET); + expectOne("url", HTTPMethod.GET); }); - it('can test HttpClient.post', () => { + it("can test HttpClient.post", () => { let { dataService, controller, expectOne } = http(); dataService.post(1).subscribe(); - const req = expectOne('url', HTTPMethod.POST); - expect(req.request.body['id']).toEqual(1); + const req = expectOne("url", HTTPMethod.POST); + expect(req.request.body["id"]).toEqual(1); + }); + + it("should test two requests", () => { + let { dataService, controller, expectOne } = http(); + dataService.twoRequests().subscribe(); + const req = expectOne("one", HTTPMethod.POST); + req.flush({}); + expectOne("two", HTTPMethod.GET); }); + + it( + "should work with external service", + fakeAsync(() => { + let { dataService, controller, expectOne, get } = http(); + get(UserService).getUser.andCallFake(() => { + return defer(() => Promise.resolve({})); + }); + + const req = dataService.requestWithExternalService().subscribe(); + tick(); + + expectOne("two", HTTPMethod.GET); + }) + ); }); diff --git a/src/app/todos-data.service.ts b/src/app/todos-data.service.ts index 3ec3ab42..57b627e3 100644 --- a/src/app/todos-data.service.ts +++ b/src/app/todos-data.service.ts @@ -1,17 +1,39 @@ -import { HttpClient } from '@angular/common/http'; -import { Injectable } from '@angular/core'; +import { HttpClient } from "@angular/common/http"; +import { Injectable } from "@angular/core"; +import { concatMap } from "rxjs/operators"; +import { defer } from "rxjs/observable/defer"; + +export class UserService { + getUser() { + return defer(() => Promise.resolve({})); + } +} @Injectable() export class TodosDataService { - constructor( private http: HttpClient ) { - - } + constructor(private http: HttpClient, private userService: UserService) {} get() { - return this.http.get('url'); + return this.http.get("url"); } post(id: number) { - return this.http.post('url', {id}); + return this.http.post("url", { id }); + } + + twoRequests() { + return this.http.post("one", {}).pipe( + concatMap(() => { + return this.http.get("two"); + }) + ); + } + + requestWithExternalService() { + return this.userService.getUser().pipe( + concatMap(() => { + return this.http.get("two"); + }) + ); } } diff --git a/src/lib/src/http.ts b/src/lib/src/http.ts index 2aead083..151ec1b5 100644 --- a/src/lib/src/http.ts +++ b/src/lib/src/http.ts @@ -9,7 +9,8 @@ import { async, TestBed } from '@angular/core/testing'; import { HttpClientTestingModule, HttpTestingController, TestRequest } from '@angular/common/http/testing'; import { HttpClient } from '@angular/common/http'; -import { Type } from '@angular/core'; +import { Provider, Type } from '@angular/core'; +import { SpyObject } from './mock'; export const enum HTTPMethod { GET = 'GET', @@ -22,6 +23,7 @@ export class SpectatorHTTP { httpClient: HttpClient; controller: HttpTestingController; dataService: T; + get: (service: Type) => S & SpyObject; expectOne: (url: string, method: HTTPMethod) => TestRequest; } @@ -44,10 +46,18 @@ export function createHTTPFactory(dataService: Type, providers = []) { http.controller = TestBed.get(HttpTestingController); http.dataService = TestBed.get(dataService); http.httpClient = TestBed.get(HttpClient); + http.get = function(provider: Type): S & SpyObject { + return TestBed.get(provider); + }; http.expectOne = (url: string, method: HTTPMethod) => { - const req = http.controller.expectOne(url); - expect(req.request.method).toEqual(method); + const req = http.controller.expectOne({ + url, + method + }); + // assert that there are no outstanding requests. + http.controller.verify(); + return req; };