-
Notifications
You must be signed in to change notification settings - Fork 8
/
controllers.test.js
346 lines (271 loc) · 13 KB
/
controllers.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
describe( 'controller test', function() {
var scope;
beforeEach( angular.mock.module( 'myApp' ) );
//mock the controller for the same reason and include $rootScope and $controller
beforeEach(angular.mock.inject(function($rootScope, $controller){
//create an empty scope
scope = $rootScope.$new();
socketMock = new sockMock($rootScope);
//declare the controller and inject our empty scope
$controller('AppCtrl', {$scope: scope, socket: socketMock, serverState:{roomName:'testRoom'}});
}));
it('should have variable name = "testRoom"', function(){
expect(scope.name).toBe('testRoom');
});
it('should accept changeCurrentFile file="/myfiles/test.js"', function(){
socketMock.receive('changeCurrentFile', '/myfiles/test.js', 'text/javascript');
expect(scope.currentFile).toBe('/myfiles/test.js');
expect(scope.editorOptions.mode).toBe('text/javascript');
});
it('should accept roomReadOnly', function(){
socketMock.receive('roomReadOnly', true);
expect(scope.readOnly).toBe(true);
socketMock.receive('roomReadOnly', false);
expect(scope.readOnly).toBe(false);
});
it('should accept roomAuthMap', function(){
var testMap = {
moderator:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': true, 'changeCurrentFile':true, 'changeRole':true},
editor:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': true, 'changeCurrentFile':true, 'changeRole':false},
default:{'editData':false, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': false, 'changeCurrentFile':false, 'changeRole':false}
};
socketMock.receive('roomAuthMap', testMap);
expect(scope.authMap).toBe(testMap);
});
it('should accept fileAdded and add file to files list', function(){
socketMock.receive('fileAdded', '/my/test/file.js');
expect(scope.files.length).toBe(1);
expect(scope.files[0]).toBe('/my/test/file.js');
});
it('should accept fileDeleted and remove file from files list', function(){
socketMock.receive('fileAdded', '/my/test/file.js');
socketMock.receive('fileAdded', '/my/other/test/stuff.css');
socketMock.receive('fileDeleted', '/my/test/file.js');
expect(scope.files.length).toBe(1);
expect(scope.files[0]).toBe('/my/other/test/stuff.css');
});
it('should accept refreshData', function(){
socketMock.receive('changeCurrentFile', 'a.txt');
socketMock.receive('refreshData', 'This is the new file data.');
expect(scope.bodyStore['a.txt'].body).toBe('This is the new file data.');
expect(scope.bodyStore['a.txt'].isDirty).toBe(false);
});
it('should accept changeData', function(){
socketMock.receive('changeCurrentFile', 'a.txt');
var testOp = {origin:'+input'};
socketMock.receive('changeData', testOp);
expect(scope.newChange).toBe(testOp);
});
it('should accept resetHostData and reset all host data', function(){
socketMock.receive('resetHostData');
expect(scope.currentFile).toBe('no file');
expect(scope.files.length).toBe(0);
expect(Object.keys(scope.bodyStore).length).toBe(0);
expect(scope.warning).toBe('');
});
it('should accept newChatMessage and add it to the messages list', function(){
var flag;
runs(function() {
flag = false;
socketMock.receive('newChatMessage', "Hi my name is bob", 'bob');
socketMock.receive('newChatMessage', "pleased to meet you, I'm Cindy", 'Cindy Crawford');
setTimeout(function() {
socketMock.receive('newChatMessage', "I'm like.. a Model!", 'Cindy Crawford');
flag = true;
}, 100);
});
waitsFor(function() {
return flag;
}, "it should wait to post the last message", 200);
runs(function() {
expect(scope.messages.length).toBe(3);
expect(scope.messages[2].message).toBe("I'm like.. a Model!");
expect(scope.messages[1].date.getTime()).toBeLessThan(scope.messages[2].date.getTime());
});
});
it('should accept newUser and add it to the users list', function(){
var testUserInfo = {
"provider":'github',
"displayName":'bob',
"username":'bob the builder',
"profileUrl":'',
"emails":'bob@thebuilder.com',
"avatar_url": '',
"gravatar_id": ''
};
var newUser = {userId:'bob', isYou:false, userInfo:testUserInfo, role:'default'}
socketMock.receive('newUser', newUser);
expect(scope.users.length).toBe(1);
expect(scope.users[0]).toBe(newUser);
});
it('should accept newUser that is you and set current user and read only', function(){
socketMock.receive('roomReadOnly', false);
var testMap = {
moderator:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': true, 'changeCurrentFile':true, 'changeRole':true},
editor:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': true, 'changeCurrentFile':true, 'changeRole':false},
default:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': false, 'changeCurrentFile':false, 'changeRole':false}
};
socketMock.receive('roomAuthMap', testMap);
var testUserInfo = {
"provider":'github',
"displayName":'bob',
"username":'bob the builder',
"profileUrl":'',
"emails":'bob@thebuilder.com',
"avatar_url": '',
"gravatar_id": ''
};
var newUser = {userId:'bob', isYou:true, userInfo:testUserInfo, role:'default'}
socketMock.receive('newUser', newUser);
expect(scope.currentUser).toBe(newUser);
expect(scope.readOnly).toBe(false);
expect(scope.editorOptions.readOnly).toBe(false);
});
it('should accept exitingUser and remove user from user list', function(){
socketMock.receive('newUser', {userId:'bob', isYou:true, userInfo:null, role:'default'});
socketMock.receive('newUser', {userId:'tara', isYou:true, userInfo:null, role:'default'});
socketMock.receive('newUser', {userId:'will', isYou:true, userInfo:null, role:'default'});
socketMock.receive('exitingUser', 'tara');
expect(scope.users.length).toBe(2);
expect(scope.users[1].userId).toBe('will');
});
it('should accept userIdChanged and change the users name', function(){
socketMock.receive('newUser', {userId:'bob', isYou:true, userInfo:null, role:'default'});
socketMock.receive('userIdChanged', 'bob', 'tara');
expect(scope.users[0].userId).toBe('tara');
});
it('should accept userRoleChanged and change the users name', function(){
socketMock.receive('roomReadOnly', false);
var testMap = {
moderator:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': true, 'changeCurrentFile':true, 'changeRole':true},
editor:{'editData':true, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': true, 'changeCurrentFile':true, 'changeRole':false},
default:{'editData':false, 'newChatMessage':true, 'changeUserId':true, 'saveCurrentFile': false, 'changeCurrentFile':false, 'changeRole':false}
};
socketMock.receive('roomAuthMap', testMap);
socketMock.receive('newUser', {userId:'bob', isYou:true, userInfo:null, role:'default'});
socketMock.receive('userRoleChanged', 'bob', 'editor');
expect(scope.users[0].role).toBe('editor');
expect(scope.editorOptions.readOnly).toBe(false);
});
it('should accept requestedRole and change the users requested role', function(){
socketMock.receive('newUser', {userId:'bob', isYou:true, userInfo:null, role:'default'});
socketMock.receive('requestedRole', 'bob', 'editor');
expect(scope.users[0].requestedRole).toBe('editor');
});
it('should emit changeCurrentFile when requestChangeCurrentFile is called', function(){
scope.requestChangeCurrentFile('/test.js');
//emits['eventName'][#emit][#arg]
expect(socketMock.emits['changeCurrentFile'][0][0]).toBe('/test.js');
});
it('should emit saveCurrentFile when requestSaveCurrentFile is called', function(){
socketMock.receive('changeCurrentFile', 'a.txt');
socketMock.receive('refreshData', 'This is the new file data.');
scope.requestSaveCurrentFile();
expect(socketMock.emits['saveCurrentFile'].length).toBe(1);
});
it('should emit newChatMessage and clear newMessage when newChatMessage is called', function(){
scope.newMessage = 'Hi my name is Earl.';
scope.newChatMessage();
expect(socketMock.emits['newChatMessage'][0][0]).toBe('Hi my name is Earl.');
expect(scope.newMessage).toBe('');
});
it('should emit changeUserId and clear newUserId when changeUserId is called', function(){
scope.newUserId = 'Earl';
scope.changeUserId();
expect(socketMock.emits['changeUserId'][0][0]).toBe('Earl');
expect(scope.newUserId).toBe('');
});
it('should emit grantChangeRole when grantChangeRole is called', function(){
scope.grantChangeRole('Earl', 'editor');
expect(socketMock.emits['grantChangeRole'].length).toBe(1);
expect(socketMock.emits['grantChangeRole'][0][0].userId).toBe('Earl');
expect(socketMock.emits['grantChangeRole'][0][0].newRole).toBe('editor');
});
it('should emit grantChangeRole if the user has a requested role and grantRequestedRole is called', function(){
var newUser = {userId:'bob', isYou:false, userInfo:null, role:'default'}
socketMock.receive('newUser', newUser);
socketMock.receive('requestedRole', 'bob', 'editor');
scope.grantRequestedRole('bob');
expect(socketMock.emits['grantChangeRole'].length).toBe(1);
expect(socketMock.emits['grantChangeRole'][0][0].userId).toBe('bob');
expect(socketMock.emits['grantChangeRole'][0][0].newRole).toBe('editor');
});
it('should emit requestChangeRole when requestChangeRole is called', function(){
var newUser = {userId:'bob', isYou:false, userInfo:null, role:'default'}
socketMock.receive('newUser', newUser);
scope.requestChangeRole('bob', 'editor', 'xxx123');
expect(socketMock.emits['requestChangeRole'].length).toBe(1);
expect(socketMock.emits['requestChangeRole'][0][0].userId).toBe('bob');
expect(socketMock.emits['requestChangeRole'][0][0].newRole).toBe('editor');
expect(socketMock.emits['requestChangeRole'][0][0].pass).toBe('xxx123');
});
it('should emit changeData and refreshData when onEditorChange is called and scope.readOnly is false', function(){
scope.readOnly = false;
socketMock.receive('changeCurrentFile', 'a.txt');
socketMock.receive('refreshData', 'this is the text');
var testOp = {op:'op', origin:'+input'};
scope.onEditorChange(0, testOp);
expect(socketMock.emits['changeData'][0][0]).toBe(testOp);
expect(socketMock.emits['refreshData'][0][0]).toBe("this is the text");
expect(socketMock.emits['refreshData'][0][1]).toBe(false);
});
it('should persist multiple open files', function(){
scope.readOnly = false;
socketMock.receive('changeCurrentFile', 'a.txt');
socketMock.receive('refreshData', 'this is a.');
socketMock.receive('changeCurrentFile', 'b.txt');
socketMock.receive('refreshData', 'this is b.');
expect(scope.bodyStore['a.txt'].body).toBe('this is a.');
expect(scope.bodyStore['a.txt'].isDirty).toBe(false);
expect(scope.bodyStore['b.txt'].body).toBe('this is b.');
expect(scope.bodyStore['b.txt'].isDirty).toBe(false);
});
it('should accept a close file and remove from local memory', function(){
scope.readOnly = false;
socketMock.receive('changeCurrentFile', 'a.txt');
socketMock.receive('refreshData', 'this is a.');
socketMock.receive('changeCurrentFile', 'b.txt');
socketMock.receive('refreshData', 'this is b.');
socketMock.receive('closeFile', 'a.txt');
expect(scope.bodyStore.hasOwnProperty('a.txt')).toBe(false);
expect(Object.keys(scope.bodyStore).length).toBe(1);
});
it('should accept a sync open file', function(){
var testOpenFile = {fileName:'a.txt', body:'this is a.', isDirty: false};
socketMock.receive('syncOpenFile', testOpenFile);
expect(scope.bodyStore['a.txt'].body).toBe('this is a.');
expect(scope.bodyStore['a.txt'].isDirty).toBe(false);
});
});
/*
Simple mock for socket.io
see: https://github.com/btford/angular-socket-io-seed/issues/4
thanks to https://github.com/southdesign for the idea
*/
var sockMock = function($rootScope){
this.events = {};
this.emits = {};
// intercept 'on' calls and capture the callbacks
this.on = function(eventName, callback){
if(!this.events[eventName]) this.events[eventName] = [];
this.events[eventName].push(callback);
};
// intercept 'emit' calls from the client and record them to assert against in the test
this.emit = function(eventName){
var args = Array.prototype.slice.call(arguments, 1);
if(!this.emits[eventName])
this.emits[eventName] = [];
this.emits[eventName].push(args);
};
//simulate an inbound message to the socket from the server (only called from the test)
this.receive = function(eventName){
var args = Array.prototype.slice.call(arguments, 1);
if(this.events[eventName]){
angular.forEach(this.events[eventName], function(callback){
$rootScope.$apply(function() {
callback.apply(this, args);
});
});
};
};
};