-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): expose get method for getting the service
- Loading branch information
1 parent
399719a
commit 400c082
Showing
3 changed files
with
79 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters