-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcluster-list-test.js
377 lines (303 loc) · 14.3 KB
/
cluster-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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
describe("Unit Component: clusterList", function() {
"use strict";
// Angular injectables
var $scope, $q, $httpBackend, $injector, $rootScope, $state, $templateCache, $compile, $interval, $destroy, $uibModal, $componentController, $event;
// Module defined (non-Angular) injectables
var config, utils, clusterList, Notifications, clusterStore;
// Local variables used for testing
var getClusterListDeferred, 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_;
$uibModal = _$uibModal_;
$scope = $rootScope.$new();
templateHtml = $templateCache.get("/modules/cluster/cluster-list/cluster-list.html");
element = $compile(templateHtml)($scope);
});
inject(function(_utils_, _config_, _clusterStore_, _Notifications_, _clusterList_) {
utils = _utils_;
config = _config_;
clusterList = _clusterList_;
clusterStore = _clusterStore_;
Notifications = _Notifications_;
});
});
beforeEach(function() {
$rootScope.clusterData = clusterList;
$state.current.name = "clusters";
getClusterListDeferred = $q.defer();
sinon.stub($state, "go");
sinon.stub(clusterStore, "getClusterList").returns(getClusterListDeferred.promise);
sinon.stub(utils, "redirectToGrafana")
clock = sinon.useFakeTimers();
config.refreshIntervalTime = 120;
});
it("Should initialize all the properties", function() {
vm = $componentController("clusterList", { $scope: $scope });
expect(vm.isDataLoading).to.be.true;
expect(vm.clusterNotPresent).to.be.false;
expect(vm.flag).to.be.false;
expect(vm.enProfilingBtnClicked).to.be.false;
expect(vm.disProfilingBtnClicked).to.be.false;
expect($rootScope.selectedClusterOption).to.be.equal("allClusters");
expect(vm.filtersText).to.be.equal("");
expect(vm.filters).to.be.an("array").that.is.empty;
expect(vm.clusterList).to.be.an("array").that.is.empty;
expect(vm.filteredClusterList).to.be.an("array").that.is.empty;
expect(vm.sortConfig.fields).to.deep.equal(clusterList.fields);
expect(vm.sortConfig.onSortChange).to.be.a("function");
expect(vm.filterConfig.fields).to.deep.equal(clusterList.filterFields);
expect(vm.filterConfig.onFilterChange).to.be.a("function");
expect(vm.filterConfig.appliedFilters).to.be.an("array").that.is.empty;
expect(clusterStore.selectedTab).to.be.equal(1);
});
describe("Cluster List workflows", function() {
beforeEach(function() {
vm = $componentController("clusterList", { $scope: $scope });
getClusterListDeferred.resolve(clusterList.clusters);
$rootScope.$digest();
});
it("Should get list of clusters", function() {
expect(vm.clusterList).to.deep.equal(clusterList.clusters);
});
it("Should take the user to dashboard on clicking Dashboard button", function() {
// Exercise SUT
var cluster = clusterList.clusters[0];
vm.redirectToGrafana(cluster);
// Verify result (behavior)
expect(utils.redirectToGrafana.calledWith("glance", { clusterId: cluster.clusterId })).to.be.true;
});
it("Should enable/disable profiling on clicking Enable/Disable profiling link", function() {
// Exercise SUT
var cluster = clusterList.clusters[0],
profilingDeferred = $q.defer(),
event = new Event("click");
sinon.stub(clusterStore, "doProfilingAction").returns(profilingDeferred.promise);
sinon.stub(Notifications, "message");
sinon.stub(event, "stopPropagation");
vm.doProfilingAction(event, cluster, "Enable", cluster.clusterId);
profilingDeferred.resolve(clusterList.profilingResponse);
$rootScope.$digest();
// Verify result (behavior)
expect(Notifications.message.calledWith("success", "", "Enable volume profiling job initiated successfully.")).to.be.true;
expect(cluster.disableAction).to.be.true;
expect(event.stopPropagation.calledOnce).to.be.true;
});
it("Should give error while enable/disable profiling on clicking Enable/Disable profiling link", function() {
// Exercise SUT
var cluster = clusterList.clusters[0],
profilingDeferred = $q.defer(),
event = new Event("click");
sinon.stub(clusterStore, "doProfilingAction").returns(profilingDeferred.promise);
sinon.stub(Notifications, "message");
sinon.stub(event, "stopPropagation");
vm.doProfilingAction(event, cluster, "Enable", cluster.clusterId);
profilingDeferred.reject("error");
$rootScope.$digest();
// Verify result (behavior)
expect(Notifications.message.calledWith("danger", "", "Failed to enable volume profile.")).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 cluster list API continuosly after a certain interval", function() {
intervalSpy = sinon.spy($interval);
throttled = throttle(intervalSpy);
throttled();
clock.tick(1000 * config.refreshIntervalTime - 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.refreshIntervalTime);
function throttle(callback) {
return function() {
clearTimeout(timer);
args = [].slice.call(arguments);
timer = setTimeout(function() {
callback.apply(this, args);
}, 1000 * config.refreshIntervalTime);
};
}
});
it("Should cancel the timer", function() {
sinon.stub($interval, "cancel");
$scope.$destroy();
expect($interval.cancel.calledOnce).to.be.true;
});
it("Should go to Import Flow View", function() {
var cluster = clusterList.clusters[0];
vm.goToImportFlow(cluster);
expect($state.go.calledWith("import-cluster", { clusterId: cluster.integrationId }));
});
it("Should go to Cluster Host View", function() {
var cluster = clusterList.clusters[0];
vm.goToClusterHost(cluster);
expect($state.go.calledWith("cluster-hosts", { clusterId: cluster.clusterId }));
});
it("Should go to Task Detail View when cluster is expanding", function() {
var cluster = clusterList.clusters[0];
cluster.jobType = "ExpandClusterWithDetectedPeers";
vm.goToTaskDetail(cluster);
expect($state.go.calledWith("task-detail", { clusterId: cluster.integrationId, taskId: cluster.currentTaskId }));
});
it("Should go to Task Detail View when cluster is changing profiling", function() {
var cluster = clusterList.clusters[0];
cluster.jobType = "EnableDisableVolumeProfiling";
vm.goToTaskDetail(cluster);
expect($state.go.calledWith("task-detail", { clusterId: cluster.integrationId, taskId: cluster.currentTaskId }));
});
it("Should go to Global Task Detail View", function() {
var cluster = clusterList.clusters[0];
vm.goToTaskDetail(cluster);
expect($state.go.calledWith("global-task-detail", { clusterId: cluster.integrationId, taskId: cluster.currentTaskId }));
});
it("Should show import button", function() {
var cluster = clusterList.clusters[0];
$rootScope.userRole = "normal";
expect(vm.showImportBtn(cluster)).to.be.true;
});
it("Should disable import button", function() {
var cluster = clusterList.clusters[1];
cluster.currentStatus = "in_progress";
$rootScope.userRole = "normal";
expect(vm.disableImportBtn(cluster)).to.be.true;
});
it("Should show dashboard button", function() {
var cluster = clusterList.clusters[1];
expect(vm.showDashboardBtn(cluster)).to.be.true;
});
it("Should show Kebab Menu", function() {
var cluster = clusterList.clusters[1];
$rootScope.userRole = "normal";
expect(vm.showKebabMenu(cluster)).to.be.true;
});
it("Should hide Expand button", function() {
var cluster = clusterList.clusters[0];
$rootScope.userRole = "limited";
expect(vm.hideExpandBtn(cluster)).to.be.true;
});
it("Should show tooltip", function() {
var cluster = clusterList.clusters[1];
cluster.currentStatus = "finished";
expect(vm.getTemplate(cluster)).to.be.equal("The cluster is successfully imported for viewing monitoring data and metrics.");
});
it("Should get an icon class when cluster is unmanaged", function() {
var cluster = clusterList.clusters[0];
var cls = vm.getClass(cluster);
expect(cls).to.be.equal("fa ffont fa-question");
});
it("Should get an icon class when cluster is unhealthy", function() {
var cluster = clusterList.clusters[0];
cluster.status = "HEALTH_ERR";
var cls = vm.getClass(cluster);
expect(cls).to.be.equal("pficon pficon-warning-triangle-o");
});
it("Should get an icon class when cluster is ok", function() {
var cluster = clusterList.clusters[0];
cluster.status = "HEALTH_OK";
var cls = vm.getClass(cluster);
expect(cls).to.be.equal("pficon pficon-ok");
});
it("Should get an icon class when cluster is expanding", function() {
var cluster = clusterList.clusters[0];
cluster.state = "expanding";
cluster.currentStatus = "in_progress";
cluster.jobType = "ExpandClusterWithDetectedPeers";
var cls = vm.getClass(cluster);
expect(cls).to.be.equal("pficon pficon-in-progress");
});
});
it("Should check for error for cluster list API", function() {
vm = $componentController("clusterList", { $scope: $scope });
getClusterListDeferred.reject("error");
$rootScope.$digest();
expect(vm.clusterList).to.be.an("array").that.is.empty;
expect(vm.filteredClusterList).to.be.an("array").that.is.empty;
expect(vm.isDataLoading).to.be.false;
});
it("Should sort the list with changed parameters of Status", function() {
vm = $componentController("clusterList", { $scope: $scope });
vm.sortConfig.currentField = {
id: "status",
title: "Status",
sortType: "alpha"
};
vm.sortConfig.isAscending = false;
getClusterListDeferred.resolve(clusterList.clusters);
$rootScope.$digest();
expect(vm.filteredClusterList).to.deep.equal(clusterList.sortedformattedOutputStatus);
});
it("Should sort the list with changed parameters of Name", function() {
vm = $componentController("clusterList", { $scope: $scope });
vm.sortConfig.currentField = {
id: "name",
title: "Name",
sortType: "alpha"
};
vm.sortConfig.isAscending = false;
getClusterListDeferred.resolve(clusterList.clusters);
$rootScope.$digest();
expect(vm.filteredClusterList).to.deep.equal(clusterList.sortedformattedOutputName);
});
it("Should sort the list with changed parameters of sdsVersion", function() {
vm = $componentController("clusterList", { $scope: $scope });
vm.sortConfig.currentField = {
id: "sdsVersion",
title: "Cluster Version",
sortType: "alpha"
};
vm.sortConfig.isAscending = false;
getClusterListDeferred.resolve(clusterList.clusters);
$rootScope.$digest();
expect(vm.filteredClusterList).to.deep.equal(clusterList.sortedformattedOutputSds);
});
it("Should sort the list with changed parameters of managed", function() {
vm = $componentController("clusterList", { $scope: $scope });
vm.sortConfig.currentField = {
id: "managed",
title: "Managed",
sortType: "alpha"
};
vm.sortConfig.isAscending = false;
getClusterListDeferred.resolve(clusterList.clusters);
$rootScope.$digest();
expect(vm.filteredClusterList).to.deep.equal(clusterList.sortedformattedOutputManaged);
});
it("Should filter the list with 'name' parameters", function() {
vm = $componentController("clusterList", { $scope: $scope });
vm.filters = [{
id: "name",
title: "Name",
placeholder: "Filter by Name",
filterType: "text"
}];
vm.filters[0].value = "f755";
getClusterListDeferred.resolve(clusterList.clusters);
$rootScope.$digest();
expect(vm.filtersText).to.be.equal("Name : f755\n");
expect(vm.filteredClusterList).to.deep.equal(clusterList.filteredNameFormattedOutput);
});
afterEach(function() {
// Tear down
$state.go.restore();
clock.restore();
});
});