|
| 1 | +declare var jasmine:any; |
| 2 | +declare var expect:any; |
| 3 | +import { |
| 4 | + it, |
| 5 | + iit, |
| 6 | + describe, |
| 7 | + ddescribe, |
| 8 | + // expect, |
| 9 | + inject, |
| 10 | + injectAsync, |
| 11 | + TestComponentBuilder, |
| 12 | + beforeEach, |
| 13 | + beforeEachProviders |
| 14 | +} from 'angular2/testing'; |
| 15 | +import {provide} from 'angular2/core'; |
| 16 | +import {AngularFire, FIREBASE_PROVIDERS, defaultFirebase} from 'angularfire2'; |
| 17 | +import {Github} from './github'; |
| 18 | +import {HTTP_PROVIDERS, XHRBackend, Response, BaseResponseOptions} from 'angular2/http'; |
| 19 | +import {MockBackend, MockConnection} from 'angular2/http/testing'; |
| 20 | +import {LOCAL_STORAGE} from '../config'; |
| 21 | +import {ScalarObservable} from 'rxjs/observable/ScalarObservable'; |
| 22 | + |
| 23 | +describe('Github Service', () => { |
| 24 | + |
| 25 | + beforeEachProviders(() => [ |
| 26 | + Github, |
| 27 | + FIREBASE_PROVIDERS, |
| 28 | + defaultFirebase('https://issue-zero.firebaseio.com'), |
| 29 | + HTTP_PROVIDERS, |
| 30 | + provide(XHRBackend, { |
| 31 | + useClass: MockBackend |
| 32 | + }), |
| 33 | + provide(LOCAL_STORAGE, { |
| 34 | + useClass: MockLocalStorage |
| 35 | + })]); |
| 36 | + |
| 37 | + beforeEach(inject([AngularFire], (angularFire) => { |
| 38 | + angularFire.auth = new ScalarObservable({github: { |
| 39 | + accessToken: 'fooAccessToken' |
| 40 | + }}) |
| 41 | + })); |
| 42 | + |
| 43 | + |
| 44 | + it('should make a request to the Github API', inject([Github, XHRBackend], (service: Github, backend: MockBackend) => { |
| 45 | + var nextSpy = jasmine.createSpy('next') |
| 46 | + backend.connections.subscribe(nextSpy); |
| 47 | + |
| 48 | + var fetchObservable = service.fetch('/repo', 'bar=baz'); |
| 49 | + expect(nextSpy).not.toHaveBeenCalled(); |
| 50 | + fetchObservable.subscribe(); |
| 51 | + expect(nextSpy).toHaveBeenCalled(); |
| 52 | + })); |
| 53 | + |
| 54 | + |
| 55 | + it('should not make a request to the Github API if value exists in cache', inject( |
| 56 | + [Github, XHRBackend, LOCAL_STORAGE], |
| 57 | + (service: Github, backend: MockBackend, localStorage) => { |
| 58 | + var connectionCreated = jasmine.createSpy('connectionCreated'); |
| 59 | + var valueReceived = jasmine.createSpy('valueReceived'); |
| 60 | + backend.connections.subscribe(connectionCreated); |
| 61 | + |
| 62 | + localStorage.setItem('izCache/repo', '{"issues": ["1"]}'); |
| 63 | + |
| 64 | + var fetchObservable = service.fetch('/repo', 'bar=baz'); |
| 65 | + expect(connectionCreated).not.toHaveBeenCalled(); |
| 66 | + fetchObservable.subscribe(valueReceived); |
| 67 | + expect(connectionCreated).not.toHaveBeenCalled(); |
| 68 | + expect(valueReceived.calls.argsFor(0)[0]).toEqual({issues: ['1']}) |
| 69 | + })); |
| 70 | + |
| 71 | + it('should set http response json to cache', inject( |
| 72 | + [Github, XHRBackend, LOCAL_STORAGE], |
| 73 | + (service: Github, backend: MockBackend, localStorage) => { |
| 74 | + var setItemSpy = spyOn(localStorage, 'setItem'); |
| 75 | + backend.connections.subscribe((c:MockConnection) => { |
| 76 | + c.mockRespond(new Response(new BaseResponseOptions().merge({body: `{"issues": ["1","2","3"]}`}))); |
| 77 | + }); |
| 78 | + |
| 79 | + var fetchObservable = service.fetch('/repo', 'bar=baz'); |
| 80 | + fetchObservable.subscribe(); |
| 81 | + expect(setItemSpy).toHaveBeenCalledWith('izCache/repo', '{"issues": ["1","2","3"]}'); |
| 82 | + })); |
| 83 | +}); |
| 84 | + |
| 85 | +class MockLocalStorage { |
| 86 | + private _cache = {}; |
| 87 | + getItem (key:string): string { |
| 88 | + return key in this._cache ? this._cache[key] : null; |
| 89 | + } |
| 90 | + |
| 91 | + setItem (key:string, value:string): void { |
| 92 | + this._cache[key] = value; |
| 93 | + } |
| 94 | +} |
0 commit comments