Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created December 17, 2015 11:18

Revisions

  1. robwormald created this gist Dec 17, 2015.
    30 changes: 30 additions & 0 deletions github_api.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    import {Injectable} from 'angular2/core';
    import {Http} from 'angular2/http';
    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/operator/map';

    const GITHUB_API_URL = 'https://api.github.com';

    export class Repository {
    name: string;
    full_name: string;
    stargazers_count: number;
    watchers_count: number;
    constructor({name, full_name, stargazers_count, watchers_count}) {
    this.name = name;
    this.full_name = full_name;
    this.stargazers_count = stargazers_count;
    this.watchers_count = watchers_count;
    }
    }

    @Injectable()
    export class GithubAPI {
    constructor(private http:Http) {}

    getRepo(org:string, name:string): Observable<Repository> {
    return this.http.get(`${GITHUB_API_URL}/repos/${org}/${name}`)
    .map(res => res.json())
    .map(repoData => new Repository(repoData));
    }
    }
    58 changes: 58 additions & 0 deletions github_api_spec.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    import {GithubAPI, Repository} from './github_api';
    import {Injector, provide} from 'angular2/core';
    import {MockBackend, MockConnection} from 'angular2/http/testing';
    import {HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions} from 'angular2/http';

    const ANGULAR_REPO_URL = 'https://api.github.com/repos/angular/angular';
    const MOCK_API_RESPONSE = `
    {
    "id": 24195339,
    "name": "angular",
    "full_name": "angular/angular",
    "stargazers_count": 2000,
    "watchers_count" : 1000
    }`;

    export function main() {
    describe('GithubAPI Service', () => {
    let injector:Injector;
    let backend:MockBackend;
    let githubAPI:GithubAPI;

    beforeEach(() => {
    injector = Injector.resolveAndCreate([
    HTTP_PROVIDERS,
    provide(XHRBackend, {useClass: MockBackend}),
    GithubAPI
    ]);
    backend = injector.get(XHRBackend);
    githubAPI = injector.get(GithubAPI);
    });

    it('should instantiate the mock backend', () => {
    expect(githubAPI).toBeDefined();
    });

    it('should make a request to the Github API', (done) => {
    backend.connections.subscribe((c:MockConnection) => {
    expect(c.request.url).toEqual(ANGULAR_REPO_URL);
    done();
    });
    githubAPI.getRepo('angular', 'angular').subscribe();
    });

    it('should make a return an instance of a Repository', (done) => {
    backend.connections.subscribe((c:MockConnection) => {
    c.mockRespond(new Response(new ResponseOptions({body: MOCK_API_RESPONSE})));
    });
    githubAPI.getRepo('angular', 'angular').subscribe((repo:Repository) => {
    expect(repo instanceof Repository).toBe(true);
    expect(repo.full_name).toEqual('angular/angular');
    expect(repo.name).toEqual('angular');
    expect(repo.watchers_count).toEqual(1000);
    expect(repo.stargazers_count).toEqual(2000);
    done();
    });
    });
    });
    }