-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathuser-list-test.js
206 lines (166 loc) · 7.32 KB
/
user-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
describe("Unit Component: UserList", function() {
"use strict";
// Angular injectables
var $scope, $q, $httpBackend, $injector, $rootScope, $state, $templateCache, $compile, $interval, $destroy, $componentController, $event, $uibModal;
// Module defined (non-Angular) injectables
var config, utils, userStore, userList, capitalizeFilter, Notifications;
// Local variables used for testing
var getUserListDeferred, vm, clock, throttled, intervalSpy, timer, args, 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/users/user-list/users.html");
element = $compile(templateHtml)($scope);
});
inject(function(_utils_, _config_, _userStore_, _userList_, _capitalizeFilter_, _Notifications_) {
utils = _utils_;
config = _config_;
userStore = _userStore_;
userList = _userList_;
capitalizeFilter = _capitalizeFilter_;
Notifications = _Notifications_;
});
});
beforeEach(function() {
$state.current.name = "users";
getUserListDeferred = $q.defer();
sinon.stub($state, "go");
sinon.stub(userStore, "getUserList").returns(getUserListDeferred.promise);
clock = sinon.useFakeTimers();
});
it("Should initialize all the properties", function() {
vm = $componentController("users", { $scope: $scope });
expect($rootScope.selectedClusterOption).to.be.null;
expect(vm.userList).to.be.an("array").that.is.empty;
expect(vm.filtersText).to.be.equal("");
expect(vm.filteredUserList).to.be.an("array").that.is.empty;
expect(vm.filters).to.be.an("array").that.is.empty;
expect(vm.isDataLoading).to.be.true;
expect(vm.filterConfig.fields).to.deep.equal(userList.fields);
expect(vm.filterConfig.onFilterChange).to.be.a("function");
expect(vm.filterConfig.appliedFilters).to.be.an("array").that.is.empty;
});
describe("User List workflows", function() {
beforeEach(function() {
vm = $componentController("users", { $scope: $scope });
getUserListDeferred.resolve(userList.users);
$rootScope.$digest();
});
it("Should get user list", function() {
expect(vm.userList).to.deep.equal(userList.users);
expect(vm.filteredUserList).to.deep.equal(userList.users);
expect(vm.isDataLoading).to.be.false;
});
it("Should go to add user view", function() {
vm.addNewUser();
expect($state.go.calledWith("add-user"));
});
it("Should go to edit user view", function() {
var user = userList.users[0];
vm.editUserDetail(user.username);
expect($state.go.calledWith("edit-user", { userId: user.username }));
});
it("Should toggle notification", function() {
var user = userList.users[0],
userDeferred = $q.defer();
sinon.stub(userStore, "editUser").returns(userDeferred.promise);
sinon.stub(Notifications, "message");
vm.toggleNotification(user);
userDeferred.resolve(userList.editUserResponse);
$rootScope.$digest();
// Verify result (behavior)
expect(Notifications.message.calledWith("success", "", "Email notification is now enabled for user1.")).to.be.true;
});
it("Should not toggle notification when edit API fails", function() {
var user = userList.users[0],
userDeferred = $q.defer();
sinon.stub(userStore, "editUser").returns(userDeferred.promise);
sinon.stub(Notifications, "message");
vm.toggleNotification(user);
userDeferred.reject("error");
$rootScope.$digest();
// Verify result (behavior)
expect(Notifications.message.calledWith("danger", "", "Failed to enable email notification for user1.")).to.be.true;
});
it("Should broadcast UpdatedUserList function", function() {
var users = userList.users;
$scope.$apply(function() {
$rootScope.$broadcast("UpdatedUserList", users);
});
expect(vm.userList).to.deep.equal(userList.users);
});
});
it("Should verify for API fail", function() {
vm = $componentController("users", { $scope: $scope });
getUserListDeferred.reject("error");
$rootScope.$digest();
expect(vm.userList).to.be.an("array").that.is.empty;
expect(vm.filteredUserList).to.be.an("array").that.is.empty;
expect(vm.isDataLoading).to.be.false;
});
it("Should filter the list with 'username' parameters", function() {
vm = $componentController("users", { $scope: $scope });
vm.filters = [{
id: "username",
title: "User ID",
placeholder: "Filter by User ID",
filterType: "text"
}];
vm.filters[0].value = "1";
getUserListDeferred.resolve(userList.users);
$rootScope.$digest();
vm.userList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filtersText).to.be.equal("User ID : 1\n");
expect(vm.filteredUserList).to.deep.equal(userList.filteredUsernameFormattedOutput);
});
it("Should filter the list with 'name' parameters", function() {
vm = $componentController("users", { $scope: $scope });
vm.filters = [{
id: "name",
title: "Name",
placeholder: "Filter by Name",
filterType: "text"
}];
vm.filters[0].value = "Steve";
getUserListDeferred.resolve(userList.users);
$rootScope.$digest();
vm.userList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filtersText).to.be.equal("Name : Steve\n");
expect(vm.filteredUserList).to.deep.equal(userList.filteredNameFormattedOutput);
});
it("Should filter the list with 'role' parameters", function() {
vm = $componentController("users", { $scope: $scope });
vm.filters = [{
id: "role",
title: "Role",
placeholder: "Filter by Role",
filterType: "text"
}];
vm.filters[0].value = "normal";
getUserListDeferred.resolve(userList.users);
$rootScope.$digest();
vm.userList.forEach(function(o) { delete o.$$hashKey });
expect(vm.filtersText).to.be.equal("Role : normal\n");
expect(vm.filteredUserList).to.deep.equal(userList.filteredRoleFormattedOutput);
});
afterEach(function() {
// Tear down
$state.go.restore();
clock.restore();
});
});