|
| 1 | +/****************************************** |
| 2 | +Unit tests for the GithubService service. We need to test... |
| 3 | +
|
| 4 | + * public getUser method |
| 5 | + * public getRepoListPages method |
| 6 | + * http service usage |
| 7 | + * url service usage |
| 8 | +
|
| 9 | +/*******************************************/ |
| 10 | + |
| 11 | +// --------------- Service mocks --------------- |
| 12 | + |
| 13 | +import * as sinon from 'sinon'; |
| 14 | + |
| 15 | +const fakeHttpService = { |
| 16 | + get: sinon.stub() |
| 17 | +}; |
| 18 | + |
| 19 | +const fakeUrlService = { |
| 20 | + urlToUser: sinon.stub(), |
| 21 | + urlToUserRepoListPage: sinon.stub() |
| 22 | +}; |
| 23 | + |
| 24 | +// --------------- Test config --------------- |
| 25 | + |
| 26 | +import { HttpClient } from '@angular/common/http'; |
| 27 | +import { UrlService } from '../services/urlservice'; |
| 28 | +import { GithubService } from '../services/githubservice'; |
| 29 | + |
| 30 | +const testModuleConfig = { |
| 31 | + providers: [GithubService, {provide: HttpClient, useValue: fakeHttpService}, {provide: UrlService, useValue: fakeUrlService}] |
| 32 | +} |
| 33 | + |
| 34 | +// --------------- Test suite --------------- |
| 35 | + |
| 36 | +import { TestBed, getTestBed } from '@angular/core/testing'; |
| 37 | +import { expect } from 'chai'; |
| 38 | + |
| 39 | +let service: GithubService; |
| 40 | + |
| 41 | +describe('GithubService', () => { |
| 42 | + before(() => TestBed.configureTestingModule(testModuleConfig)); |
| 43 | + after(() => getTestBed().resetTestingModule()); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + sinon.resetHistory(); |
| 47 | + sinon.resetBehavior(); |
| 48 | + service = TestBed.get(GithubService); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should instantiate ok', () => { |
| 52 | + expect(service).to.exist; |
| 53 | + }); |
| 54 | + |
| 55 | + describe('the getUser method', () => { |
| 56 | + const fakeUserId = Math.random().toString(); |
| 57 | + const fakeUserUrl = Math.random().toString(); |
| 58 | + const fakeGetObservable = Symbol(); |
| 59 | + beforeEach(() => { |
| 60 | + fakeUrlService.urlToUser.returns(fakeUserUrl); |
| 61 | + fakeHttpService.get.returns(fakeGetObservable); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should call url service with id to get url to user', () => { |
| 65 | + service.getUser(fakeUserId); |
| 66 | + expect(fakeUrlService.urlToUser.called).to.be.true; |
| 67 | + expect(fakeUrlService.urlToUser.lastCall.args[0]).to.equal(fakeUserId); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should pass the user url to .get method of the http service', () => { |
| 71 | + service.getUser(fakeUserId); |
| 72 | + expect(fakeHttpService.get.called).to.be.true; |
| 73 | + expect(fakeHttpService.get.lastCall.args[0]).to.equal(fakeUserUrl); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return the return value from the get method', () => { |
| 77 | + expect(service.getUser(fakeUserId)).to.equal(fakeGetObservable); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments