Skip to content

Commit 65ef995

Browse files
committed
test(client:main): fix main tests
1 parent 9be904d commit 65ef995

File tree

5 files changed

+85
-24
lines changed

5 files changed

+85
-24
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<%
2424
}
2525

2626
ngOnInit() {
27-
this.Http.get('/api/things')
28-
.map(res => {
29-
return res.json();
30-
})
31-
.catch(err => Observable.throw(err.json().error || 'Server error'))
27+
return this.Http.get('/api/things')
28+
.map(res => res.json())
29+
// .catch(err => Observable.throw(err.json().error || 'Server error'))
3230
.subscribe(things => {
3331
this.awesomeThings = things;
3432
<%_ if(filters.ws) { -%>

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

+70-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,74 @@
11
'use strict';
2-
3-
// import main from './main.component';
4-
// import {MainController} from './main.component';
2+
import {
3+
async,
4+
ComponentFixture,
5+
inject,
6+
TestBed,
7+
} 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.expect) { -%>
18+
import { expect } from 'chai';<% } %><% if(filters.uibootstrap) { %>
19+
import { TooltipModule } from 'ngx-bootstrap';<% } %>
20+
import { FormsModule } from '@angular/forms';
21+
import { SocketService } from '../../components/socket/socket.service';
22+
import { SocketServiceStub } from '../../components/socket/socket.mock';
23+
import { MainComponent } from './main.component';
524

625
describe('Component: MainComponent', function() {
7-
it('should attach a list of things to the controller', function() {});
26+
let comp: MainComponent;
27+
let fixture: ComponentFixture<MainComponent>;
28+
29+
beforeEach(async(() => {
30+
TestBed.configureTestingModule({
31+
imports: [
32+
FormsModule,<% if(filters.uibootstrap) { %>
33+
TooltipModule.forRoot(),<% } %>
34+
HttpModule,
35+
],
36+
declarations: [ MainComponent ], // declare the test component
37+
providers: [
38+
BaseRequestOptions,
39+
MockBackend,
40+
{
41+
provide: Http,
42+
useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) => {
43+
return new Http(backend, defaultOptions);
44+
},<%_ if(filters.ws) { %>
45+
deps: [MockBackend, BaseRequestOptions]<% } %>
46+
},
47+
{ provide: SocketService, useClass: SocketServiceStub },
48+
],
49+
}).compileComponents();
50+
}));
51+
<%_ if(filters.ws) { %>
52+
beforeEach('mock backend', 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+
})));<% } %>
59+
60+
beforeEach('ngOnInit', async(() => {
61+
fixture = TestBed.createComponent(MainComponent);
62+
// MainComponent test instance
63+
comp = fixture.componentInstance;
64+
65+
/**
66+
* Trigger initial data binding.
67+
*/
68+
fixture.detectChanges();
69+
}));
70+
71+
it('should attach a list of things to the controller', () => {
72+
expect(comp.awesomeThings.length).to.equal(4);
73+
});
874
});
+4-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
'use strict';
2-
import { noop } from 'lodash-es';
32

4-
export class SocketServiceMock {
5-
socket = {
6-
connect: noop,
7-
on: noop,
8-
emit: noop,
9-
receive: noop
10-
};
11-
syncUpdates = noop;
12-
unsyncUpdates = noop;
3+
export class SocketServiceStub {
4+
constructor() {}
5+
syncUpdates() {}
6+
unsyncUpdates() {}
137
}

Diff for: templates/app/spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import 'zone.js/dist/fake-async-test';
1313

1414
var testsContext = require.context('./client', true, /\.(spec|test)\.<%= scriptExt %>$/);
1515
// testsContext.keys().forEach(testsContext);
16-
// testsContext('./app/main/main.component.spec.<%= scriptExt %>');
16+
testsContext('./app/main/main.component.spec.<%= scriptExt %>');
1717
testsContext('./components/util.spec.<%= scriptExt %>');
18-
<%_ if(filters.oauth) { -%>
19-
testsContext('./components/oauth-buttons/oauth-buttons.component.spec.<%= scriptExt %>');
20-
<%_ } -%>
18+
<% if(filters.oauth) { -%>
19+
testsContext('./components/oauth-buttons/oauth-buttons.component.spec.<%= scriptExt %>');<% } %>
2120

2221
import { TestBed, getTestBed } from '@angular/core/testing';
2322
import {

Diff for: templates/app/tsconfig(ts).json

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
"compilerOptions": {
33
"allowJs": true,
44
"allowSyntheticDefaultImports": true,
5+
"baseDir": ".",
56
"experimentalDecorators": true,
67
"sourceMap": true,
78
"rootDir": "./",
89
"module": "es6",
9-
"outDir": ".tmp",
10+
"outDir": ".tmp",<% if(filters.ws) %>
11+
"paths": {
12+
"primus": ["client/components/socket/primus.js"]
13+
},<% } %>
1014
"removeComments": false,
1115
"target": "es5",
1216
"skipLibCheck": true,

0 commit comments

Comments
 (0)