Skip to content

Commit

Permalink
Add getHeroes method (thymikee#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkerM committed Aug 26, 2018
1 parent 32f929f commit f6178f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions example/src/app/service/hero.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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<any>) => {
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 = {}

Expand Down
8 changes: 8 additions & 0 deletions example/src/app/service/hero.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export class HeroService {

constructor(private http: HttpClient) {}

/** GET heroes from the server */
getHeroes(): Observable<Hero[]> {
return this.http.get<Hero>(heroesUrl)
.pipe(
catchError(err => this.handleError(err, 'getHero')),
)
}

/** GET hero by id. Will 404 if id not found */
getHero(id: number): Observable<Hero> {
const params = new HttpParams().set('id', id.toString())
Expand Down

0 comments on commit f6178f2

Please sign in to comment.