-
Notifications
You must be signed in to change notification settings - Fork 0
/
internalRequestControllerTest.js
314 lines (231 loc) · 10.5 KB
/
internalRequestControllerTest.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
describe("controller: InternalRequestController", function () {
var $q, $scope, $templateCache, MockedInternalRequest, InternalRequestRepo, InternalRequestsService, RemoteProductManagerRepo, WsApi, controller;
var initializeVariables = function () {
inject(function (_$compile_, _$q_, _$templateCache_, _InternalRequestRepo_, _InternalRequestsService_, _RemoteProductManagerRepo_, _WsApi_) {
$compile = _$compile_;
$q = _$q_;
$templateCache = _$templateCache_;
MockedInternalRequest = new mockInternalRequest($q);
InternalRequestRepo = _InternalRequestRepo_;
InternalRequestsService = _InternalRequestsService_;
RemoteProductManagerRepo = _RemoteProductManagerRepo_;
WsApi = _WsApi_;
});
};
var initializeController = function (settings) {
inject(function (_$controller_, _$rootScope_, _InternalRequestsService_, _ModalService_, _RemoteProductsService_, _UserService_) {
$scope = _$rootScope_.$new();
sessionStorage.role = settings && settings.role ? settings.role : "ROLE_ADMIN";
sessionStorage.token = settings && settings.token ? settings.token : "faketoken";
controller = _$controller_("InternalRequestController", {
$scope: $scope,
InternalRequestRepo: InternalRequestRepo,
InternalRequestsService: _InternalRequestsService_,
ModalService: _ModalService_,
RemoteProductManagerRepo: RemoteProductManagerRepo,
RemoteProductsService: _RemoteProductsService_,
UserService: _UserService_
});
// ensure that the isReady() is called.
if (!$scope.$$phase) {
$scope.$digest();
}
});
};
beforeEach(function () {
module("core");
module("app");
module("templates");
module("mock.internalRequest", function ($provide) {
var InternalRequest = function () {
return MockedInternalRequest;
};
$provide.value("InternalRequest", InternalRequest);
});
module("mock.internalRequestRepo");
module("mock.internalRequestsService");
module("mock.remoteProductManagerRepo");
module("mock.remoteProductsService");
module("mock.user", function ($provide) {
var User = function () {
return MockedUser;
};
$provide.value("User", User);
});
module("mock.userService");
module("mock.wsApi");
installPromiseMatchers();
initializeVariables();
initializeController();
});
describe("Is the controller", function () {
var roles = [ "ROLE_ADMIN", "ROLE_MANAGER", "ROLE_USER", "ROLE_ANONYMOUS" ];
var controllerExists = function (setting) {
return function() {
initializeController(setting);
expect(controller).toBeDefined();
};
};
for (var i in roles) {
it("defined for " + roles[i], controllerExists({ role: roles[i] }));
}
});
describe("Is the scope method", function () {
var methods = [
"cancelDeleteInternalRequest",
"cancelEditInternalRequest",
"cancelPushFeatureRequest",
"confirmDeleteInternalRequest",
"createInternalRequest",
"deleteInternalRequest",
"editInternalRequest",
"getRemoteProductManagerRemoteProducts",
"pushFeatureRequest",
"pushInternalRequest",
"resetCreateInternalRequest",
"resetInternalRequestForms",
"updateInternalRequest"
];
var scopeMethodExists = function (method) {
return function() {
expect($scope[method]).toBeDefined();
expect(typeof $scope[method]).toEqual("function");
};
};
for (var i in methods) {
it(methods[i] + " defined", scopeMethodExists(methods[i]));
}
});
describe("Does the scope method", function () {
it("cancelDeleteInternalRequest clear internalRequestToDelete and close the modal", function () {
var internalRequest = new mockInternalRequest($q);
$scope.internalRequestToDelete = internalRequest;
spyOn($scope, "closeModal");
$scope.cancelDeleteInternalRequest();
expect($scope.closeModal).toHaveBeenCalled();
expect($scope.internalRequestToDelete).toEqual({});
});
it("cancelEditInternalRequest clear out internalRequestToEdit, close the modal, and call resetInternalRequestForms", function () {
var internalRequest = new mockInternalRequest($q);
$scope.internalRequestToEdit = internalRequest;
spyOn($scope.internalRequestToEdit, "refresh");
spyOn($scope, "resetInternalRequestForms");
$scope.cancelEditInternalRequest();
expect($scope.internalRequestToEdit.refresh).toHaveBeenCalled();
expect($scope.resetInternalRequestForms).toHaveBeenCalled();
});
it("cancelPushFeatureRequest clear out featureRequestToPush, close the modal, and call resetInternalRequestForms", function () {
var internalRequest = new mockInternalRequest($q);
$scope.featureRequestToPush = dataFeatureRequest1;
spyOn($scope, "resetInternalRequestForms");
$scope.cancelPushFeatureRequest();
expect($scope.featureRequestToPush).toEqual({});
expect($scope.resetInternalRequestForms).toHaveBeenCalled();
});
it("confirmDeleteInternalRequest set the internalRequestToDelete and open the modal", function () {
var internalRequest = new mockInternalRequest($q);
spyOn($scope, "openModal");
$scope.confirmDeleteInternalRequest(internalRequest);
expect($scope.openModal).toHaveBeenCalled();
expect($scope.internalRequestToDelete).toEqual(internalRequest);
});
it("createInternalRequest create a new Internal Requst", function () {
var internalRequest = new mockInternalRequest($q);
var id = dataInternalRequestRepo1.length + 1;
internalRequest.mock({
id: id,
name: "Mock Internal Request " + id
});
$scope.internalRequestToCreate = internalRequest;
$scope.createInternalRequest();
expect(InternalRequestRepo.findById(id)).toEqual(internalRequest);
});
it("deleteInternalRequest call the repo delete method and then call cancelDeleteInternalRequest when successful", function () {
var internalRequest = new mockInternalRequest($q);
$scope.internalRequestToDelete = internalRequest;
spyOn(InternalRequestRepo, "delete").and.callThrough();
spyOn($scope, "cancelDeleteInternalRequest");
$scope.deleteInternalRequest(internalRequest);
$scope.$digest();
expect(InternalRequestRepo.delete).toHaveBeenCalledWith(internalRequest);
expect($scope.cancelDeleteInternalRequest).toHaveBeenCalled();
});
it("editInternalRequest set the internalRequestToEdit and open the modal", function () {
var internalRequest = new mockInternalRequest($q);
spyOn($scope, "openModal");
$scope.editInternalRequest(internalRequest);
expect($scope.internalRequestToEdit).toEqual(internalRequest);
expect($scope.openModal).toHaveBeenCalled();
});
it("getRemoteProductManagerRemoteProducts return the requested Remote Products", function () {
var response;
var productManagerId = dataRemoteProductManager2.id;
response = $scope.getRemoteProductManagerRemoteProducts(productManagerId);
expect(response.id).toEqual(productManagerId);
});
it("pushFeatureRequest push a FeatureRequest", function () {
var featureRequest = dataFeatureRequest1;
$scope.featureRequestToPush = featureRequest;
spyOn(InternalRequestsService, "pushFeatureRequest").and.callThrough();
spyOn($scope, "cancelPushFeatureRequest");
$scope.pushFeatureRequest();
$scope.$digest();
expect(InternalRequestsService.pushFeatureRequest).toHaveBeenCalledWith(featureRequest);
expect($scope.cancelPushFeatureRequest).toHaveBeenCalled();
});
it("pushInternalRequest push an InternalRequest as a FeatureRequest", function () {
var internalRequest = new mockInternalRequest($q);
spyOn($scope, "openModal");
$scope.pushInternalRequest(internalRequest);
expect($scope.featureRequestToPush.title).toEqual(internalRequest.title);
expect($scope.featureRequestToPush.description).toEqual(internalRequest.description);
expect($scope.featureRequestToPush.productId).toBe(null);
expect($scope.featureRequestToPush.scopeId).toBe(null);
expect($scope.openModal).toHaveBeenCalled();
});
it("resetCreateInternalRequest call resetInternalRequestForms() and clear out the properties", function () {
var internalRequest = new mockInternalRequest($q);
var id = dataInternalRequestRepo1.length + 1;
internalRequest.mock({
id: id,
name: "Mock Internal Request " + id
});
$scope.internalRequestToCreate = internalRequest;
var modal = angular.element($templateCache.get("views/modals/addInternalRequestModal.html"));
modal = $compile(modal)($scope);
var form = $scope.internalRequestForms.create;
form.$setDirty();
expect(form.$dirty).toEqual(true);
spyOn($scope, "resetInternalRequestForms");
$scope.resetCreateInternalRequest();
expect($scope.internalRequestToCreate.title).toEqual("");
expect($scope.internalRequestToCreate.description).toEqual("");
expect($scope.internalRequestToCreate.createdOn).toBe(null);
expect($scope.resetInternalRequestForms).toHaveBeenCalled();
});
it("resetInternalRequestForms should close modals and reset InternalRequest forms", function () {
var modal = angular.element($templateCache.get("views/modals/editInternalRequestModal.html"));
modal = $compile(modal)($scope);
var form = $scope.internalRequestForms.edit;
form.$setDirty();
expect(form.$dirty).toEqual(true);
spyOn($scope, "closeModal");
$scope.resetInternalRequestForms();
expect(form.$pristine).toEqual(true);
expect(form.$dirty).toEqual(false);
expect($scope.closeModal).toHaveBeenCalled();
});
it("updateInternalRequest call dirty and save on the InternalRequest, and then call cancelEditInternalRequest", function () {
var internalRequest = new mockInternalRequest($q);
$scope.internalRequestToEdit = internalRequest;
spyOn($scope, "cancelEditInternalRequest");
spyOn(internalRequest, "dirty").and.callThrough();
spyOn(internalRequest, "save").and.callThrough();
$scope.updateInternalRequest();
$scope.$digest();
expect($scope.cancelEditInternalRequest).toHaveBeenCalled();
expect(internalRequest.dirty).toHaveBeenCalledWith(true);
expect(internalRequest.save).toHaveBeenCalled();
});
});
});