Skip to content

Commit 47d238a

Browse files
committed
refactor(main): use HttpClient instead of Http
1 parent 11decfe commit 47d238a

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

Diff for: templates/app/client/app/app.module.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
RequestOptions,
1313
RequestOptionsArgs,<% } %>
1414
} from '@angular/http';
15+
import { HttpClientModule } from '@angular/common/http';
1516
import {
1617
removeNgStyles,
1718
createNewHosts,
@@ -67,6 +68,7 @@ const appRoutes: Routes = [{ path: '',
6768
imports: [
6869
BrowserModule,
6970
HttpModule,
71+
HttpClientModule,
7072
<%_ if (filters.uirouter) { -%>
7173
UIRouterModule.forRoot(),<% } %>
7274
<%_ if (filters.ngroute) { -%>

Diff for: templates/app/client/app/main/main.component.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, OnInit<% if(filters.ws) { %>, OnDestroy<% } %> } from '@angular/core';
2-
import { Http } from '@angular/http';
3-
import { Observable } from 'rxjs/Observable';
2+
import { HttpClient } from '@angular/common/http';
43
import 'rxjs/add/operator/map';<% if(filters.ws) { %>
54
import { SocketService } from '../../components/socket/socket.service';<% } %>
65

@@ -10,28 +9,24 @@ import { SocketService } from '../../components/socket/socket.service';<% } %>
109
styles: [require('./main.<%=styleExt%>')],
1110
})
1211
export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<% } %> {
13-
Http;
1412
<%_ if(filters.ws) { -%>
1513
SocketService;<% } %>
1614
awesomeThings = [];
1715
<%_ if(filters.models) { -%>
1816
newThing = '';<% } %>
1917

20-
static parameters = [Http<% if(filters.ws) { %>, SocketService<% } %>];
21-
constructor(<%= private() %>http: Http<% if(filters.ws) { %>, <%= private() %>socketService: SocketService<% } %>) {
22-
this.Http = http;
18+
static parameters = [HttpClient<% if(filters.ws) { %>, SocketService<% } %>];
19+
constructor(<%= private() %>http: HttpClient<% if(filters.ws) { %>, <%= private() %>socketService: SocketService<% } %>) {
20+
this.http = http;
2321
<%_ if(filters.ws) { -%>
2422
this.SocketService = socketService;<% } %>
2523
}
2624

2725
ngOnInit() {
28-
return this.Http.get('/api/things')
29-
.map(res => res.json())
30-
// .catch(err => Observable.throw(err.json().error || 'Server error'))
26+
return this.http.get('/api/things')
3127
.subscribe(things => {
3228
this.awesomeThings = things;
33-
<%_ if(filters.ws) { -%>
34-
this.SocketService.syncUpdates('thing', this.awesomeThings);<% } %>
29+
this.SocketService.syncUpdates('thing', this.awesomeThings);
3530
});
3631
}<% if (filters.models) { %>
3732
<%_ if(filters.ws) { %>
@@ -45,19 +40,15 @@ export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<%
4540
let text = this.newThing;
4641
this.newThing = '';
4742

48-
return this.Http.post('/api/things', { name: text })
49-
.map(res => res.json())
50-
.catch(err => Observable.throw(err.json().error || 'Server error'))
43+
return this.http.post('/api/things', { name: text })
5144
.subscribe(thing => {
5245
console.log('Added Thing:', thing);
5346
});
5447
}
5548
}
5649

5750
deleteThing(thing) {
58-
return this.Http.delete(`/api/things/${thing._id}`)
59-
.map(res => res.json())
60-
.catch(err => Observable.throw(err.json().error || 'Server error'))
51+
return this.http.delete(`/api/things/${thing._id}`)
6152
.subscribe(() => {
6253
console.log('Deleted Thing');
6354
});

Diff for: templates/app/client/app/main/main.component.spec.js

+23-35
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ import {
55
inject,
66
TestBed,
77
} from '@angular/core/testing';
8-
import {
9-
BaseRequestOptions,
10-
ConnectionBackend,
11-
Http,
12-
HttpModule,
13-
Response,
14-
ResponseOptions,
15-
} from '@angular/http';
16-
import { MockBackend } from '@angular/http/testing';
17-
<%_ if(filters.mocha && filters.expect) { -%>
8+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';<% if(filters.mocha && filters.expect) { %>
189
import { expect } from 'chai';<% } %><% if(filters.uibootstrap) { %>
1910
import { TooltipModule } from 'ngx-bootstrap';<% } %>
2011
import { FormsModule } from '@angular/forms';<% if(filters.ws) { %>
@@ -25,51 +16,48 @@ import { MainComponent } from './main.component';
2516
describe('Component: MainComponent', function() {
2617
let comp: MainComponent;
2718
let fixture: ComponentFixture<MainComponent>;
19+
let httpTestingController: HttpTestingController;
20+
const mockThings = ['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express'];
2821

2922
beforeEach(async(() => {
3023
TestBed.configureTestingModule({
3124
imports: [
3225
FormsModule,<% if(filters.uibootstrap) { %>
3326
TooltipModule.forRoot(),<% } %>
34-
HttpModule,
27+
HttpClientTestingModule,
3528
],
36-
declarations: [ MainComponent ], // declare the test component
29+
declarations: [ MainComponent ], // declare the test component<% if(filters.ws) { %>
3730
providers: [
38-
BaseRequestOptions,
39-
MockBackend,
40-
{
41-
provide: Http,
42-
useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) => {
43-
return new Http(backend, defaultOptions);
44-
},
45-
deps: [MockBackend, BaseRequestOptions]
46-
},<% if(filters.ws) { %>
47-
{ provide: SocketService, useClass: SocketServiceStub },<% } %>
48-
],
31+
{ provide: SocketService, useClass: SocketServiceStub },
32+
],<% } %>
4933
}).compileComponents();
50-
}));
5134

52-
beforeEach(async(inject([MockBackend], (mockBackend) => {
53-
mockBackend.connections.subscribe(conn => {
54-
conn.mockRespond(new Response(new ResponseOptions({
55-
body: JSON.stringify(['HTML5 Boilerplate', 'AngularJS', 'Karma', 'Express'])
56-
})));
57-
});
58-
})));
35+
httpTestingController = TestBed.get(HttpTestingController);
36+
}));
5937

6038
beforeEach(async(() => {
6139
fixture = TestBed.createComponent(MainComponent);
6240
// MainComponent test instance
6341
comp = fixture.componentInstance;
6442

6543
/**
66-
* Trigger initial data binding.
44+
* Trigger initial data binding and run lifecycle hooks
6745
*/
6846
fixture.detectChanges();
6947
}));
7048

71-
it('should attach a list of things to the controller', () => {<% if(filters.jasmine) { %>
72-
expect(comp.awesomeThings.length).toEqual(4);<% } else if(filters.mocha) { %>
73-
<%= expect() %>comp.awesomeThings.length<%= to() %>.equal(4);<% } %>
49+
it('should attach a list of things to the controller', () => {
50+
// `GET /api/things` should be made once
51+
const req = httpTestingController.expectOne('/api/things');
52+
expect(req.request.method).to.equal('GET');
53+
54+
// Respond with mock data
55+
req.flush(mockThings);
56+
57+
// assert that there are no outstanding requests
58+
httpTestingController.verify();
59+
60+
<% if(filters.jasmine) { %>expect(comp.awesomeThings).toEqual(mockThings);<% } else if(filters.mocha) { %>
61+
<%= expect() %>comp.awesomeThings<%= to() %>.equal(mockThings);<% } %>
7462
});
7563
});

0 commit comments

Comments
 (0)