Skip to content

Commit

Permalink
feat(http): expose get method for getting the service
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Apr 17, 2018
1 parent 399719a commit 400c082
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
46 changes: 37 additions & 9 deletions src/app/todos-data.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(TodosDataService);
describe("HttpClient testing", () => {
let http = createHTTPFactory<TodosDataService>(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);
})
);
});
36 changes: 29 additions & 7 deletions src/app/todos-data.service.ts
Original file line number Diff line number Diff line change
@@ -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");
})
);
}
}
16 changes: 13 additions & 3 deletions src/lib/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -22,6 +23,7 @@ export class SpectatorHTTP<T> {
httpClient: HttpClient;
controller: HttpTestingController;
dataService: T;
get: <S>(service: Type<S>) => S & SpyObject<S>;
expectOne: (url: string, method: HTTPMethod) => TestRequest;
}

Expand All @@ -44,10 +46,18 @@ export function createHTTPFactory<T>(dataService: Type<T>, providers = []) {
http.controller = TestBed.get(HttpTestingController);
http.dataService = TestBed.get(dataService);
http.httpClient = TestBed.get(HttpClient);
http.get = function<S>(provider: Type<S>): S & SpyObject<S> {
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;
};

Expand Down

0 comments on commit 400c082

Please sign in to comment.