| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 |
1×
1×
8×
8×
8×
1×
8×
8×
8×
8×
8×
8×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| import angular from 'angular';
import 'angular-mocks';
import 'angular-ui-router';
import {PageViewAbstract} from './PageView';
import {RepoView} from './RepoView';
import {UserView} from './UserView';
import {GitHubService} from '../utils/github.service';
import {MOCK_REPOS, MOCK_CONTRIBUTORS, MOCK_USER, MOCK_USER_REPOS} from '../utils/github.service.mocks';
import {NO_REPO, NO_USER} from '../utils/error.constants';
describe('PageView component', () => {
let $scope;
let $rootScope;
let $state;
let $timeout;
let $stateParams;
let githubService;
beforeEach(() => {
angular.mock.module('ui.router');
angular.module('repoView', ['ui.router'])
.service('githubService', GitHubService)
.component('repoView', RepoView)
.component('userView', UserView);
angular.mock.module('repoView');
});
beforeEach(() => {
angular.mock.inject((_$rootScope_, _$timeout_, _$state_, _githubService_) => {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$timeout = _$timeout_;
$state = _$state_;
githubService = _githubService_;
});
});
it('should only be used as an abstract class', () => {
const err = new Error('Cannot construct PageViewAbstract instances directly');
expect(() => {
return new PageViewAbstract();
}).toThrow(err);
});
it('should first render a loading component', angular.mock.inject($compile => {
const element = $compile('<repo-view></repo-view>')($scope);
$scope.$digest();
expect(element.find('loading').length).toEqual(1);
}));
describe('Repo View', () => {
it('should redirect home without a repo query', angular.mock.inject($componentController => {
spyOn($state, 'go');
$stateParams = {repo: ''};
const component = $componentController('repoView', {$scope, $stateParams, githubService});
component.$onInit();
expect($state.go).toHaveBeenCalledWith('app');
}));
it('should fetch a repo resource', angular.mock.inject(($componentController, $httpBackend) => {
$stateParams = {repo: 'angular::angular'};
const component = $componentController('repoView', {$scope, $timeout, $state, $stateParams, githubService});
const API_HOST = 'https://api.github.com/';
const type = 'repos';
const value = 'angular/angular';
const firstUrl = `${API_HOST}${type}/${value}`;
const secondUrl = `${API_HOST}${type}/${value}/contributors`;
$httpBackend.whenGET(firstUrl).respond(MOCK_REPOS);
$httpBackend.whenGET(secondUrl).respond(MOCK_CONTRIBUTORS);
spyOn(component, 'initView').and.callThrough();
spyOn(githubService, 'getResource').and.callThrough();
component.$onInit();
$timeout.flush();
$httpBackend.flush();
expect(component.initView).toHaveBeenCalledWith(type, value);
expect(githubService.getResource).toHaveBeenCalledWith(type, value);
expect(component.repo.name).toEqual('angular');
}));
it('should assign error object if no repo found', angular.mock.inject(($componentController, $httpBackend) => {
$stateParams = {repo: 'foo::bar'};
const component = $componentController('repoView', {$scope, $timeout, $state, $stateParams, githubService});
const API_HOST = 'https://api.github.com/';
const type = 'repos';
const value = 'foo/bar';
const firstUrl = `${API_HOST}${type}/${value}`;
$httpBackend.whenGET(firstUrl).respond({status: 400});
spyOn(component, 'initView').and.callThrough();
spyOn(githubService, 'getResource').and.callThrough();
component.$onInit();
$timeout.flush();
$httpBackend.flush();
expect(component.initView).toHaveBeenCalledWith(type, value);
expect(githubService.getResource).toHaveBeenCalledWith(type, value);
expect(component.error.message).toEqual(NO_REPO);
}));
});
describe('User View', () => {
it('should redirect home without a user query', angular.mock.inject($componentController => {
spyOn($state, 'go');
$stateParams = {user: ''};
const component = $componentController('userView', {$scope, $stateParams, githubService});
component.$onInit();
expect($state.go).toHaveBeenCalledWith('app');
}));
it('should fetch a user resource', angular.mock.inject(($componentController, $httpBackend) => {
$stateParams = {user: 'angular'};
const component = $componentController('userView', {$scope, $timeout, $state, $stateParams, githubService});
const API_HOST = 'https://api.github.com/';
const type = 'users';
const value = 'angular';
const firstUrl = `${API_HOST}${type}/${value}`;
const secondUrl = `${API_HOST}${type}/${value}/repos`;
$httpBackend.whenGET(firstUrl).respond(MOCK_USER);
$httpBackend.whenGET(secondUrl).respond(MOCK_USER_REPOS);
spyOn(component, 'initView').and.callThrough();
spyOn(githubService, 'getResource').and.callThrough();
component.$onInit();
$timeout.flush();
$httpBackend.flush();
expect(component.initView).toHaveBeenCalledWith(type, value);
expect(githubService.getResource).toHaveBeenCalledWith(type, value);
expect(component.user.name).toEqual('Angular');
}));
it('should assign error object if no user found', angular.mock.inject(($componentController, $httpBackend) => {
$stateParams = {user: 'foo'};
const component = $componentController('userView', {$scope, $timeout, $state, $stateParams, githubService});
const API_HOST = 'https://api.github.com/';
const type = 'users';
const value = 'foo';
const firstUrl = `${API_HOST}${type}/${value}`;
$httpBackend.whenGET(firstUrl).respond({status: 400});
spyOn(component, 'initView').and.callThrough();
spyOn(githubService, 'getResource').and.callThrough();
component.$onInit();
$timeout.flush();
$httpBackend.flush();
expect(component.initView).toHaveBeenCalledWith(type, value);
expect(githubService.getResource).toHaveBeenCalledWith(type, value);
expect(component.error.message).toEqual(NO_USER);
}));
});
});
|