Skip to content

Commit a784056

Browse files
SwiipMehdy Dara
authored andcommitted
finish all tests in typescript
1 parent 2166fbc commit a784056

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { GithubContributor } from './githubContributor.service';
2+
3+
describe('service githubContributor', () => {
4+
beforeEach(angular.mock.module('<%- appName %>'));
5+
6+
it('should be registered', inject((githubContributor: GithubContributor) => {
7+
expect(githubContributor).not.toBeNull();
8+
}));
9+
10+
describe('getContributors function', () => {
11+
it('should return data', inject((githubContributor: GithubContributor, $httpBackend: ng.IHttpBackendService) => {
12+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond(200, [{pprt: 'value'}]);
13+
let data: any[];
14+
githubContributor.getContributors(1).then((fetchedData: any[]) => {
15+
data = fetchedData;
16+
});
17+
$httpBackend.flush();
18+
expect(data.length === 1).toBeTruthy();
19+
expect(data[0]).not.toBeNull();
20+
}));
21+
22+
it('should define a limit per page as default value', inject((githubContributor: GithubContributor, $httpBackend: ng.IHttpBackendService) => {
23+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=30').respond(200, new Array(30));
24+
var data: any[];
25+
githubContributor.getContributors().then((fetchedData: any[]) => {
26+
data = fetchedData;
27+
});
28+
$httpBackend.flush();
29+
expect(data.length === 30).toBeTruthy();
30+
}));
31+
32+
it('should log a error', inject((githubContributor: GithubContributor, $httpBackend: ng.IHttpBackendService, $log: ng.ILogService) => {
33+
$httpBackend.when('GET', githubContributor.apiHost + '/contributors?per_page=1').respond(500);
34+
githubContributor.getContributors(1);
35+
$httpBackend.flush();
36+
expect($log.error.logs).toEqual(jasmine.stringMatching('XHR Failed for'));
37+
}));
38+
});
39+
});

app/templates/src/app/components/githubContributor/_githubContributor.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export class GithubContributor {
1010
this.$http = $http;
1111
}
1212

13-
getContributors(limit: number = 30) {
13+
getContributors(limit: number = 30): ng.IPromise<any[]> {
1414
return this.$http.get(this.apiHost + '/contributors?per_page=' + limit)
15-
.then((response: any) => {
15+
.then((response: any): any => {
1616
return response.data;
1717
})
18-
.catch((error: any) => {
18+
.catch((error: any): any => {
1919
this.$log.error('XHR Failed for getContributors.\n', error.data);
2020
});
2121
}

0 commit comments

Comments
 (0)