Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 2754c55

Browse files
committed
wip make github first spec actually work
1 parent 857b972 commit 2754c55

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

karma.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module.exports = function (config) {
2424
{ pattern: 'node_modules/angular2/bundles/router.dev.js', included: true, watched: true },
2525
{ pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true },
2626

27-
2827
{ pattern: 'karma-test-shim.js', included: true, watched: true },
2928

3029
// paths loaded via module imports

src/client/app/github/github.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1+
declare var jasmine:any;
2+
declare var expect:any;
13
import {
24
it,
35
iit,
46
describe,
57
ddescribe,
6-
expect,
8+
// expect,
79
inject,
810
injectAsync,
911
TestComponentBuilder,
1012
beforeEachProviders
1113
} from 'angular2/testing';
1214
import {provide} from 'angular2/core';
1315
import {Github} from './github';
16+
import {HTTP_PROVIDERS, XHRBackend, Response, BaseResponseOptions} from 'angular2/http';
17+
import {MockBackend, MockConnection} from 'angular2/http/testing';
1418

1519

1620
describe('Github Service', () => {
1721

18-
beforeEachProviders(() => [Github]);
22+
beforeEachProviders(() => [Github, HTTP_PROVIDERS, provide(XHRBackend, {useClass: MockBackend})]);
1923

2024

21-
it('should ...', inject([Github], (service: Github) => {
25+
it('should make a request to the Github API', inject([Github, XHRBackend], (service: Github, backend: MockBackend) => {
26+
console.log('backend', backend);
27+
var nextSpy = jasmine.createSpy('next')
28+
backend.connections.subscribe(nextSpy);
2229

30+
var fetchObservable = service.fetch('/repo', 'foo', 'bar=baz');
31+
expect(nextSpy).not.toHaveBeenCalled();
32+
fetchObservable.subscribe();
33+
expect(nextSpy).toHaveBeenCalled();
2334
}));
24-
2535
});

src/client/app/github/github.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {Http} from 'angular2/http';
33
import {Observable} from 'rxjs/Observable';
44
import {ScalarObservable} from 'rxjs/observable/ScalarObservable';
55
import {ErrorObservable} from 'rxjs/observable/ErrorObservable';
6+
import {_catch} from 'rxjs/operator/catch';
7+
import {map} from 'rxjs/operator/map';
8+
import {_do} from 'rxjs/operator/do';
69

710
import {User, Repo} from './types';
811
const GITHUB_API = 'https://api.github.com';
@@ -13,16 +16,15 @@ export class Github {
1316
constructor(private _http:Http) {}
1417

1518
fetch(path:string, accessToken, params?: string): Observable<Repo> {
16-
17-
return getCache(path)
18-
.catch(() => this._httpRequest(path, accessToken, params));
19+
return _catch.call(getCache(path),
20+
() => this._httpRequest(path, accessToken, params));
1921
}
2022

2123
_httpRequest (path:string, accessToken:string, params?:string) {
2224
var url = `${GITHUB_API}${path}?${params ? params + '&' : ''}access_token=${accessToken}`
23-
return this._http.get(url)
24-
.do(res => setCache(path, res.text()))
25-
.map(res => res.json());
25+
var reqObservable = this._http.get(url);
26+
var setCacheSideEffect = _do.call(reqObservable, res => setCache(path, res.text()));
27+
return map.call(setCacheSideEffect, res => res.json());
2628
}
2729
}
2830

0 commit comments

Comments
 (0)