From f6178f2213c380ff5de4d1c3252fe12d86b2f49c Mon Sep 17 00:00:00 2001 From: Parker Mauney <5124113+ParkerM@users.noreply.github.com> Date: Sun, 26 Aug 2018 16:28:23 -0500 Subject: [PATCH] Add getHeroes method (#110) --- example/src/app/service/hero.service.spec.ts | 25 ++++++++++++++++++++ example/src/app/service/hero.service.ts | 8 +++++++ 2 files changed, 33 insertions(+) diff --git a/example/src/app/service/hero.service.spec.ts b/example/src/app/service/hero.service.spec.ts index 1c6841a0f9..638596671d 100644 --- a/example/src/app/service/hero.service.spec.ts +++ b/example/src/app/service/hero.service.spec.ts @@ -13,6 +13,17 @@ describe('Service: HeroService', () => { name: 'Test hero', } + const expectedDataAll = [ + { + id: 1, + name: 'Test hero 1' + }, + { + id: 2, + name: 'Test hero 2' + } + ] + beforeEach(() => { TestBed.configureTestingModule({ imports: [ @@ -40,6 +51,20 @@ describe('Service: HeroService', () => { expect(service).toBeDefined() }) + it('should call the GET heroes api and return all results', () => { + let actualDataAll = {} + + service.getHeroes().subscribe(data => actualDataAll = data) + + backend.expectOne((req: HttpRequest) => { + return req.url === `${heroesUrl}` + && req.method === 'GET' + }, `GET all hero data from ${heroesUrl}`) + .flush(expectedDataAll) + + expect(actualDataAll).toEqual(expectedDataAll) + }) + it('should call the GET hero api and return the result', () => { let actualData = {} diff --git a/example/src/app/service/hero.service.ts b/example/src/app/service/hero.service.ts index ecc2d290f3..48709078eb 100644 --- a/example/src/app/service/hero.service.ts +++ b/example/src/app/service/hero.service.ts @@ -13,6 +13,14 @@ export class HeroService { constructor(private http: HttpClient) {} + /** GET heroes from the server */ + getHeroes(): Observable { + return this.http.get(heroesUrl) + .pipe( + catchError(err => this.handleError(err, 'getHero')), + ) + } + /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable { const params = new HttpParams().set('id', id.toString())