-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathvolume-list-test.js
352 lines (272 loc) · 12.8 KB
/
volume-list-test.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
describe("Unit Component: volumeList", function() {
"use strict";
// Angular injectables
var $scope, $q, $httpBackend, $injector, $rootScope, $state, $templateCache, $compile, $interval, $destroy, $componentController, $event;
// Module defined (non-Angular) injectables
var config, utils, volumeStore, volumeList, capitalizeFilter, clusterStore;
// Local variables used for testing
var getVolumeListDeferred, getClusterDetailDeferred, vm, clock, throttled, intervalSpy, timer, args, dashboardStub, element;
// Initialize modules
beforeEach(function() {
module("TendrlModule");
module("TestDataModule");
module("templates");
});
beforeEach(function() {
var templateHtml;
inject(function(_$q_, _$componentController_, _$rootScope_, _$state_, _$templateCache_, _$compile_, _$interval_, _$uibModal_) {
$q = _$q_;
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$state = _$state_;
$templateCache = _$templateCache_;
$compile = _$compile_;
$interval = _$interval_;
$scope = $rootScope.$new();
templateHtml = $templateCache.get("/modules/volumes/volume-list/volume-list.html");
element = $compile(templateHtml)($scope);
});
inject(function(_utils_, _config_, _volumeStore_, _volumeList_, _capitalizeFilter_, _clusterStore_) {
utils = _utils_;
config = _config_;
volumeStore = _volumeStore_;
volumeList = _volumeList_;
capitalizeFilter = _capitalizeFilter_;
clusterStore = _clusterStore_;
});
});
beforeEach(function() {
$state.current.name = "cluster-volumes";
getVolumeListDeferred = $q.defer();
getClusterDetailDeferred = $q.defer();
sinon.stub($state, "go");
sinon.stub(volumeStore, "getVolumeList").returns(getVolumeListDeferred.promise);
sinon.stub(clusterStore, "getClusterDetails").returns(getClusterDetailDeferred.promise)
sinon.stub(utils, "redirectToGrafana")
clock = sinon.useFakeTimers();
config.volumeRefreshInterval = 10;
});
it("Should initialize all the properties", function() {
vm = $componentController("volumeList", { $scope: $scope });
expect(vm.isDataLoading).to.be.true;
expect(vm.flag).to.be.false;
expect(vm.volumeList).to.be.an("array").that.is.empty;
expect(vm.filteredVolumeList).to.be.an("array").that.is.empty;
expect(vm.filtersText).to.be.equal("");
expect(vm.filters).to.be.an("array").that.is.empty;
expect(vm.sortConfig.fields).to.deep.equal(volumeList.sortFields);
expect(vm.sortConfig.onSortChange).to.be.a("function");
expect(vm.filterConfig.fields).to.deep.equal(volumeList.filterFields);
expect(vm.filterConfig.onFilterChange).to.be.a("function");
expect(vm.filterConfig.appliedFilters).to.be.an("array").that.is.empty;
});
describe("Volume List workflows", function() {
beforeEach(function() {
vm = $componentController("volumeList", { $scope: $scope });
vm.clusterId = "d139e643-62c3-4520-b79a-ce7cb8babb1f";
getVolumeListDeferred.resolve(volumeList.volumes);
$rootScope.$digest();
});
it("Should get list of volumes", function() {
expect(vm.volumeList).to.deep.equal(volumeList.volumes);
});
it("Should take the user to dashboard on clicking Dashboard button", function() {
// Exercise SUT
var volume = volumeList.volumes[0];
vm.redirectToGrafana(volume);
// Verify result (behavior)
expect(utils.redirectToGrafana.calledWith("volumes", { clusterId: vm.clusterName, volumeName: volume.name })).to.be.true;
});
it("Should enable/disable profiling on clicking Enable/Disable profiling action button", function() {
// Exercise SUT
var volume = volumeList.volumes[0],
profilingDeferred = $q.defer(),
event = new Event("click");
sinon.stub(volumeStore, "toggleProfiling").returns(profilingDeferred.promise);
sinon.stub(event, "stopPropagation");
sinon.stub($interval, "cancel");
vm.toggleProfiling(volume, "disable", event);
profilingDeferred.resolve(volumeList.profilingResponse);
$rootScope.$digest();
// Verify result (behavior)
expect(volume.disableAction).to.be.true;
expect($interval.cancel.calledOnce).to.be.true;
expect(event.stopPropagation.calledOnce).to.be.true;
});
it("Should set the flag by addTooltip", function() {
// Exercise SUT
sinon.stub(utils, "tooltip").returns(true);
vm.addTooltip();
//Verify result (behavior)
expect(vm.flag).to.be.true;
});
it("Should call the volume list API continuosly after a certain interval", function() {
intervalSpy = sinon.spy($interval);
throttled = throttle(intervalSpy);
throttled();
clock.tick(1000 * config.volumeRefreshInterval - 1);
expect(intervalSpy.notCalled).to.be.true;
clock.tick(1);
expect(intervalSpy.called).to.be.true;
expect(new Date().getTime()).to.be.equal(1000 * config.volumeRefreshInterval);
function throttle(callback) {
return function() {
clearTimeout(timer);
args = [].slice.call(arguments);
timer = setTimeout(function() {
callback.apply(this, args);
}, 1000 * config.volumeRefreshInterval);
};
}
});
it("Should cancel the timer", function() {
sinon.stub($interval, "cancel");
$scope.$destroy();
expect($interval.cancel.calledOnce).to.be.true;
});
it("Should take the user to task detail page", function() {
var volume = volumeList.volumes[1];
vm.goToTaskDetail(volume);
expect($state.go.calledWith("task-detail", { clusterId: vm.clusterId, taskId: volume.currentTask.job_id }));
});
it("Should take user to volume detail page", function() {
var volume = volumeList.volumes[0];
vm.goToVolumeDetail(volume);
expect($state.go.calledWith("volume-detail", { clusterId: vm.clusterId, taskId: volume.currentTask.job_id }));
});
it("Should show enable button on proper conditions", function() {
var volume = volumeList.volumes[1];
expect(vm.showEnableBtn(volume)).to.be.true;
volume = volumeList.volumes[0];
expect(vm.showEnableBtn(volume)).to.be.false;
volume = volumeList.volumes[1];
$rootScope.userRole = "limited";
expect(vm.showEnableBtn(volume)).to.be.false;
});
it("Should show disable button on proper conditions", function() {
var volume = volumeList.volumes[1];
expect(vm.showDisableBtn(volume)).to.be.false;
volume = volumeList.volumes[0];
expect(vm.showDisableBtn(volume)).to.be.true;
volume = volumeList.volumes[0];
$rootScope.userRole = "limited";
expect(vm.showDisableBtn(volume)).to.be.false;
});
it("Should check for volume icon if state is up", function() {
expect(vm.getVolumeIcon("up")).to.be.equal("pficon pficon-ok");
});
it("Should check for volume icon if state is down", function() {
expect(vm.getVolumeIcon("down")).to.be.equal("fa ffont fa-arrow-circle-o-down");
});
it("Should check for volume icon if state is partial", function() {
expect(vm.getVolumeIcon("partial")).to.be.equal("pficon pficon-degraded icon-red");
});
it("Should check for volume icon if state is Degraded", function() {
expect(vm.getVolumeIcon("degraded")).to.be.equal("pficon pficon-degraded icon-orange");
});
it("Should check for volume icon if state is unknown", function() {
expect(vm.getVolumeIcon("unknown")).to.be.equal("fa ffont fa-question");
});
it("Should check for volume tooltip if state is up", function() {
expect(vm.getVolState("up")).to.be.equal("Up");
});
it("Should check for volume tooltip if state is down", function() {
expect(vm.getVolState("down")).to.be.equal("Down");
});
it("Should check for volume tooltip if state is partial", function() {
expect(vm.getVolState("partial")).to.be.equal("Partial");
});
it("Should check for volume tooltip if state is Degraded", function() {
expect(vm.getVolState("degraded")).to.be.equal("Degraded");
});
it("Should check for volume tooltip if state is unknown", function() {
expect(vm.getVolState("unknown")).to.be.equal("Unknown");
});
});
it("Should verify for volume API error", function() {
vm = $componentController("volumeList", { $scope: $scope });
getVolumeListDeferred.reject("error");
$rootScope.$digest();
expect(vm.volumeList).to.be.an("array").that.is.empty;
expect(vm.filteredVolumeList).to.be.an("array").that.is.empty;
expect(vm.isDataLoading).to.be.false;
});
it("Should sort the list with 'name' parameters", function() {
vm = $componentController("volumeList", { $scope: $scope });
vm.sortConfig.currentField = {
id: "name",
title: "Name",
sortType: "alpha"
};
vm.sortConfig.isAscending = false;
getVolumeListDeferred.resolve(volumeList.volumes);
$rootScope.$digest();
vm.volumeList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filteredVolumeList).to.deep.equal(volumeList.sortedformattedOutputName);
});
it("Should sort the list with 'status' parameters", function() {
vm = $componentController("volumeList", { $scope: $scope });
vm.sortConfig.currentField = {
id: "status",
title: "Status",
sortType: "alpha"
};
vm.sortConfig.isAscending = false;
getVolumeListDeferred.resolve(volumeList.volumes);
$rootScope.$digest();
vm.volumeList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filteredVolumeList).to.deep.equal(volumeList.sortedformattedOutputStatus);
});
it("Should filter the list with 'name' parameters", function() {
vm = $componentController("volumeList", { $scope: $scope });
vm.filters = [{
id: "name",
title: "Name",
placeholder: "Filter by Name",
filterType: "text"
}];
vm.filters[0].value = "vol2";
getVolumeListDeferred.resolve(volumeList.volumes);
$rootScope.$digest();
expect(vm.filtersText).to.be.equal("Name : vol2\n");
vm.volumeList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filteredVolumeList).to.deep.equal(volumeList.filteredNameFormattedOutput);
});
it("Should filter the list with 'status' parameters", function() {
vm = $componentController("volumeList", { $scope: $scope });
vm.filters = [{
id: "state",
title: "State",
placeholder: "Filter by State",
filterType: "select",
filterValues: ["Up", "Down", "Partial", "Degraded", "Unknown"]
}];
vm.filters[0].value = "up";
getVolumeListDeferred.resolve(volumeList.volumes);
$rootScope.$digest();
expect(vm.filtersText).to.be.equal("State : up\n");
vm.volumeList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filteredVolumeList).to.deep.equal(volumeList.filteredStatusFormattedOutput);
});
it("Should filter the list with 'type' parameters", function() {
vm = $componentController("volumeList", { $scope: $scope });
vm.filters = [{
id: "type",
title: "Type",
placeholder: "Filter by Type",
filterType: "select",
filterValues: ["Distribute", "Replicated", "Dispersed", "Distributed-Dispersed"]
}];
vm.filters[0].value = "Distribute";
getVolumeListDeferred.resolve(volumeList.volumes);
$rootScope.$digest();
expect(vm.filtersText).to.be.equal("Type : Distribute\n");
vm.volumeList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filteredVolumeList).to.deep.equal(volumeList.filteredTypeFormattedOutput);
});
afterEach(function() {
// Tear down
$state.go.restore();
clock.restore();
});
});