-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathevent-list-test.js
226 lines (180 loc) · 8.44 KB
/
event-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
describe("Unit Component: eventList", function() {
"use strict";
// Angular injectables
var $scope, $q, $httpBackend, $injector, $rootScope, $state, $templateCache, $compile, $interval, $destroy, $uibModal, $componentController, $event, $filter, $stateParams;
// Module defined (non-Angular) injectables
var config, utils, eventStore;
// Local variables used for testing
var vm, clock, throttled, intervalSpy, timer, args, getEventListDeferred, eventList, element, count, i, to_date, from_date;
// Initialize modules
beforeEach(function() {
module("TendrlModule");
module("TestDataModule");
module("templates");
});
beforeEach(function() {
var templateHtml;
inject(function(_$q_, _$componentController_, _$rootScope_, _$state_, _$templateCache_, _$compile_, _$interval_, _$uibModal_, _$filter_, _$stateParams_) {
$q = _$q_;
$componentController = _$componentController_;
$rootScope = _$rootScope_;
$state = _$state_;
$templateCache = _$templateCache_;
$compile = _$compile_;
$interval = _$interval_;
$uibModal = _$uibModal_;
$filter = _$filter_;
$stateParams = _$stateParams_;
$scope = $rootScope.$new();
templateHtml = $templateCache.get("/modules/events/event-list/event-list.html");
element = $compile(templateHtml)($scope);
});
inject(function(_utils_, _config_, _eventStore_, _eventList_) {
utils = _utils_;
config = _config_;
eventStore = _eventStore_;
eventList = _eventList_;
});
});
beforeEach(function() {
$state.current.name = "cluster-events";
$stateParams.clusterId = "7b05c774-f1e9-4ded-9e0b-7ac989a4f60c";
getEventListDeferred = $q.defer();
sinon.stub($state, "go");
sinon.stub(eventStore, "getEventList").returns(getEventListDeferred.promise);
clock = sinon.useFakeTimers();
config.refreshIntervalTime = 120;
});
it("Should initialize all the properties", function() {
vm = $componentController('eventList', { $scope: $scope, $stateParams: $stateParams });
vm.clusterId = $stateParams.clusterId;
expect($rootScope.selectedClusterOption).to.be.equal(vm.clusterId);
expect(vm.eventList).to.be.an("array").that.is.empty;
expect(vm.isDataLoading).to.be.true;
expect(vm.searchDescText).to.be.equal("");
expect(vm.date).to.deep.equal(eventList.date);
expect(vm.toDateOptions.format).to.be.equal("dd M yyyy");
expect(vm.fromDateOptions.format).to.be.equal("dd M yyyy");
expect(vm.toDateOptions.format).to.be.equal("dd M yyyy");
expect(vm.toDateOptions.startDate).to.be.equal($filter("date")(eventList.date.fromDate, "dd MMM yyyy"));
expect(vm.popupFrom.opened).to.be.false;
expect(vm.popupTo.opened).to.be.false;
});
describe("Task List workflows", function() {
beforeEach(function() {
vm = $componentController('eventList', { $scope: $scope, $stateParams: $stateParams });
vm.clusterId = $stateParams.clusterId;
getEventListDeferred.resolve(eventList.eventList);
$rootScope.$digest();
});
it("Should get list of jobs", function() {
expect(angular.toJson(vm.eventList)).to.deep.equal(angular.toJson(eventList.eventList));
});
it("Should open from date popup", function() {
vm.openFromDate();
expect(vm.popupFrom.opened).to.be.true;
});
it("Should open To date popup", function() {
vm.openToDate();
expect(vm.popupTo.opened).to.be.true;
});
it("Should call the task 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 filter by created date: if to data and from date is valid and equal", function() {
vm.date.fromDate = "Thu Feb 01 2018 00:00:00 GMT+0530 (IST)";
vm.date.toDate = "Thu Feb 01 2018 00:00:00 GMT+0530 (IST)";
var bool = vm.filterByCreatedDate(eventList.eventList[1]);
expect(bool).to.be.false;
});
it("Should filter by created date: if to data and from date exist, but invalid", function() {
vm.date.fromDate = "Thu Feb 10 2018 00:00:00 GMT+0530 (IST)";
vm.date.toDate = "Thu Feb 01 2018 00:00:00 GMT+0530 (IST)";
var bool = vm.filterByCreatedDate(eventList.eventList[0]);
expect(bool).to.be.false;
expect(vm.date.toDate).to.be.equal("");
expect(vm.invalidToDate).to.be.true;
});
it("Should filter by created date: if to data and from date is valid", function() {
vm.date.fromDate = "Tue Jan 16 2018 00:00:00 GMT+0530 (IST)";
vm.date.toDate = "Thu Feb 01 2018 00:00:00 GMT+0530 (IST)";
var bool = vm.filterByCreatedDate(eventList.eventList[1]);
expect(bool).to.be.true;
expect(vm.invalidToDate).to.be.false;
});
it("Should filter by created date: if to data is valid", function() {
vm.date.toDate = "Thu Feb 10 2018 00:00:00 GMT+0530 (IST)";
var bool = vm.filterByCreatedDate(eventList.eventList[0]);
expect(bool).to.be.true;
});
it("Should filter by created date: if from data is valid", function() {
vm.date.fromDate = "Thu Feb 01 2018 00:00:00 GMT+0530 (IST)";
var bool = vm.filterByCreatedDate(eventList.eventList);
expect(bool).to.be.false;
});
it("Should filter by created date: if from data is valid", function() {
vm.date.fromDate = "Tue Jan 25 2018 00:00:00 GMT+0530 (IST)";
var bool = vm.filterByCreatedDate(eventList.eventList[1]);
expect(bool).to.be.false;
});
it("Should clear all Filters", function() {
vm.clearAllFilters();
expect(vm.date.fromDate).to.be.null;
expect(vm.date.toDate).to.be.null;
expect(vm.invalidToDate).to.be.false;
expect(vm.searchDescText).to.be.equal("");
});
it("Should show 'Clear All Filters' only if from/to date exist", function() {
vm.date.fromDate = "";
vm.date.toDate = "";
vm.searchDescText = "";
expect(Boolean(vm.showClearAction())).to.be.false;
vm.date.fromDate = "";
vm.date.toDate = "";
vm.searchDescText = "123";
expect(Boolean(vm.showClearAction())).to.be.true;
});
it("Should filter the text by search", function() {
var list = vm.searchByDesc(eventList.eventList);
expect(list).to.deep.equal(eventList.eventList);
});
it("Should filter the text by search", function() {
vm.searchDescText = "job";
var list = vm.searchByDesc(eventList.eventList[1]);
expect(angular.toJson(list)).to.deep.equal(angular.toJson(eventList.searchList[0]));
});
});
it("Should verify for Task API error", function() {
vm = $componentController("eventList", { $scope: $scope });
getEventListDeferred.reject("error");
$rootScope.$digest();
expect(vm.eventList).to.be.an("array").that.is.empty;
expect(vm.isDataLoading).to.be.false;
});
afterEach(function() {
// Tear down
$state.go.restore();
clock.restore();
});
});