Skip to content

Commit d1496d0

Browse files
committed
url service tests
1 parent cb72fb3 commit d1496d0

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/specs/service.url.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/******************************************
2+
Unit tests for the Url service. We need to test...
3+
4+
* public urlToRepoList method
5+
* public urlToUser method
6+
* auth service usage
7+
8+
/*******************************************/
9+
10+
// --------------- Service mocks ---------------
11+
12+
import * as sinon from 'sinon';
13+
14+
const fakeAuthService = {
15+
authState: {
16+
token: Math.random.toString()
17+
}
18+
};
19+
20+
// --------------- Test config ---------------
21+
22+
import { UrlService } from '../services/urlservice';
23+
import { AuthService } from '../services/authservice';
24+
25+
const testModuleConfig = {
26+
providers: [
27+
UrlService,
28+
{provide: AuthService, useValue: fakeAuthService},
29+
]
30+
}
31+
32+
// --------------- Test suite ---------------
33+
34+
import { TestBed, getTestBed } from '@angular/core/testing';
35+
import { expect } from 'chai';
36+
37+
let service: UrlService;
38+
39+
describe('UrlService', () => {
40+
before(() => TestBed.configureTestingModule(testModuleConfig));
41+
after(() => getTestBed().resetTestingModule());
42+
43+
const baseUrl = 'https://api.github.com/';
44+
45+
beforeEach(() => {
46+
service = TestBed.get(UrlService);
47+
});
48+
49+
it('should instantiate ok', () => {
50+
expect(service).to.exist;
51+
});
52+
53+
describe('the urlToUser method', () => {
54+
const fakeUserId = Math.random().toString();
55+
let result;
56+
57+
beforeEach(() => {
58+
result = service.urlToUser(fakeUserId);
59+
});
60+
61+
it('should give correct base URL', () => {
62+
expect(result.split('?')[0]).to.equal(`${baseUrl}users/${fakeUserId}`);
63+
});
64+
65+
it('should include access token as query param', () => {
66+
expect(result.split('?')[1]).to.contain(`access_token=${fakeAuthService.authState.token}`);
67+
});
68+
});
69+
70+
describe('the urlToRepoList method', () => {
71+
const fakeUserId = Math.random().toString();
72+
const fakePage = Math.ceil(Math.random()*100);
73+
let result;
74+
75+
beforeEach(() => {
76+
result = service.urlToRepoList(fakeUserId, fakePage);
77+
});
78+
79+
it('should give correct base URL', () => {
80+
expect(result.split('?')[0]).to.equal(`${baseUrl}users/${fakeUserId}/repos`);
81+
});
82+
83+
it('should include access token as query param', () => {
84+
expect(result.split('?')[1]).to.contain(`access_token=${fakeAuthService.authState.token}`);
85+
});
86+
87+
it('should include page as query param', () => {
88+
expect(result.split('?')[1]).to.contain(`page=${fakePage}`);
89+
});
90+
});
91+
});

0 commit comments

Comments
 (0)