Permalink
Browse files

Upgrade to new HttpClient

  • Loading branch information...
textbook committed Sep 11, 2017
1 parent cb987aa commit 046cfb059dd1b7e141141ef018a8ee029232221c
Showing with 52 additions and 59 deletions.
  1. +1 −1 package.json
  2. +2 −2 src/app/app.module.ts
  3. +34 −49 src/app/person.service.spec.ts
  4. +8 −7 src/app/person.service.ts
  5. +1 −0 src/lib/index.ts
  6. +6 −0 src/lib/transport.ts
View
@@ -49,7 +49,7 @@
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"angular-in-memory-web-api": "^0.3.2",
"angular-in-memory-web-api": "^0.4.0",
"angular2-highcharts": "^0.5.5",
"bulma": "^0.5.0",
"core-js": "^2.4.1",
View
@@ -1,5 +1,5 @@
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
@@ -24,7 +24,7 @@ import { InMemoryPersonService } from './in-memory-person.service';
],
imports: [
BrowserModule,
HttpModule,
HttpClientModule,
InMemoryWebApiModule.forRoot(InMemoryPersonService),
SharedModule,
],
@@ -1,84 +1,72 @@
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import {
BaseRequestOptions, ConnectionBackend, Http, RequestMethod,
RequestOptions, ResponseOptions, Response,
} from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { PersonService } from './person.service';
import { Person } from '@lib/models';
describe('PersonService', () => {
let service: PersonService;
let backend: MockBackend;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ConnectionBackend, useClass: MockBackend },
{ provide: RequestOptions, useClass: BaseRequestOptions },
Http,
PersonService,
]
imports: [HttpClientTestingModule],
providers: [PersonService],
});
service = TestBed.get(PersonService);
backend = TestBed.get(ConnectionBackend);
httpMock = TestBed.get(HttpTestingController);
});
it('should get the people from the API', done => {
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.url).toMatch(/\/people$/);
expect(connection.request.method).toBe(RequestMethod.Get, 'expected GET request');
done();
});
afterEach(() => {
httpMock.verify();
});
it('should get the people from the API', () => {
service.fetch();
const req = httpMock.expectOne({ method: 'GET' });
expect(req.request.url).toMatch(/\/people$/);
});
it('should expose the people as an observable', done => {
let people = [{ name: 'Alice', salary: 12345, cohort: 'A' }];
backend.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
status: 200,
body: { data: people },
})));
});
const people = [{ name: 'Alice', salary: 12345, cohort: 'A' }];
service.fetch();
const req = httpMock.expectOne({ method: 'GET' });
expect(req.request.url).toMatch(/\/people$/);
req.flush({ data: people });
service.people$.subscribe(received => {
expect(received).toEqual([new Person(people[0].name, people[0].salary, people[0].cohort)]);
done();
});
});
it('should post a new person to the API', done => {
it('should post a new person to the API', () => {
let name = 'Lynn';
let salary = 123;
let cohort = 'Q';
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.url).toMatch(/\/people$/);
expect(connection.request.method).toBe(RequestMethod.Post, 'expected POST request');
expect(connection.request.json()).toEqual({ name, salary, cohort });
done();
});
service
.addPerson(new Person(name, salary, cohort))
.subscribe(() => {});
service.addPerson(new Person(name, salary, cohort));
const req = httpMock.expectOne({ method: 'POST' });
expect(req.request.url).toMatch(/\/people$/);
expect(req.request.body).toEqual({ name, salary, cohort });
});
it('should delete a single person from the API', done => {
it('should delete a single person from the API', () => {
let name = 'Davina';
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.url).toMatch(/\/people\/4$/);
expect(connection.request.method).toBe(RequestMethod.Delete, 'expected DELETE request');
done();
});
service
.deletePerson(new Person(name, 12453, 'A', 4))
.subscribe(() => {});
service.deletePerson(new Person(name, 12453, 'A', 4));
const req = httpMock.expectOne({ method: 'DELETE' });
expect(req.request.url).toMatch(/\/people\/4$/);
});
it('should expose an observable of sorted cohorts', done => {
@@ -89,15 +77,12 @@ describe('PersonService', () => {
new Person('Bob', 275, 'A')
];
backend.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
status: 200,
body: { data: people },
})));
});
service.fetch();
httpMock
.expectOne({ method: 'GET' })
.flush({ data: people });
service.cohorts$.subscribe(cohorts => {
expect(Object.keys(cohorts)).toEqual(['A', 'B']);
expect(cohorts.A).toEqual([275, 300]);
View
@@ -1,9 +1,10 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable, ReplaySubject } from 'rxjs';
import { Person, CohortMap } from '@lib/models';
import { CohortMap, Person } from '@lib/models';
import { RawPerson } from '@lib/transport';
@Injectable()
export class PersonService {
@@ -13,7 +14,7 @@ export class PersonService {
private personSubject = new ReplaySubject<Person[]>(1);
private personRoute = '/app/people';
constructor(private http: Http) {
constructor(private http: HttpClient) {
this.people$ = this.personSubject.asObservable();
this.cohorts$ = this.people$.map(people => this.createCohorts(people));
}
@@ -31,13 +32,13 @@ export class PersonService {
fetch() {
this.http
.get(this.personRoute)
.subscribe(response => {
this.personSubject.next(this.deserialise(response.json().data));
.get<{ data: RawPerson[] }>(this.personRoute)
.subscribe(json => {
this.personSubject.next(this.deserialise(json.data));
});
}
private deserialise(people: any[]): Person[] {
private deserialise(people: RawPerson[]): Person[] {
return people.map(person => new Person(person.name, person.salary, person.cohort, person.id));
}
View
@@ -1,2 +1,3 @@
export * from './models';
export * from './statistics';
export * from './transport';
View
@@ -0,0 +1,6 @@
export class RawPerson {
id: number;
name: string;
salary: number;
cohort: string;
}

0 comments on commit 046cfb0

Please sign in to comment.