arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/www/bower_components/angular-socket-io/socket.spec.js
www/bower_components/angular-socket-io/socket.spec.jsRaw
  1/*
  2 * angular-socket-io v0.4.1
  3 * (c) 2014 Brian Ford http://briantford.com
  4 * License: MIT
  5 */
  6
  7'use strict';
  8
  9
 10describe('socketFactory', function () {
 11
 12  beforeEach(module('btford.socket-io'));
 13
 14  var socket,
 15      scope,
 16      $timeout,
 17      $browser,
 18      mockIoSocket,
 19      spy;
 20
 21  beforeEach(inject(function (socketFactory, _$browser_, $rootScope, _$timeout_) {
 22    $browser = _$browser_;
 23    $timeout = _$timeout_;
 24    scope = $rootScope.$new();
 25    spy = jasmine.createSpy('emitSpy');
 26    mockIoSocket = io.connect();
 27    socket = socketFactory({
 28      ioSocket: mockIoSocket,
 29      scope: scope
 30    });
 31  }));
 32
 33
 34  describe('#on', function () {
 35
 36    it('should apply asynchronously', function () {
 37      socket.on('event', spy);
 38
 39      mockIoSocket.emit('event');
 40
 41      expect(spy).not.toHaveBeenCalled();
 42      $timeout.flush();
 43
 44      expect(spy).toHaveBeenCalled();
 45    });
 46
 47  });
 48
 49
 50  describe('#disconnect', function () {
 51
 52    it('should call the underlying socket.disconnect', function () {
 53      mockIoSocket.disconnect = spy;
 54      socket.disconnect();
 55      expect(spy).toHaveBeenCalled();
 56    });
 57
 58  });
 59
 60  describe('#connect', function () {
 61
 62    it('should call the underlying socket.connect', function () {
 63      mockIoSocket.connect = spy;
 64      socket.connect();
 65      expect(spy).toHaveBeenCalled();
 66    });
 67
 68  });
 69
 70
 71  describe('#once', function () {
 72
 73    it('should apply asynchronously', function () {
 74      socket.once('event', spy);
 75
 76      mockIoSocket.emit('event');
 77
 78      expect(spy).not.toHaveBeenCalled();
 79      $timeout.flush();
 80
 81      expect(spy).toHaveBeenCalled();
 82    });
 83
 84    it('should only run once', function () {
 85      var counter = 0;
 86      socket.once('event', function () {
 87        counter += 1;
 88      });
 89
 90      mockIoSocket.emit('event');
 91      mockIoSocket.emit('event');
 92      $timeout.flush();
 93
 94      expect(counter).toBe(1);
 95    });
 96
 97  });
 98
 99
100  describe('#emit', function () {
101
102    it('should call the delegate socket\'s emit', function () {
103      spyOn(mockIoSocket, 'emit');
104
105      socket.emit('event', {foo: 'bar'});
106
107      expect(mockIoSocket.emit).toHaveBeenCalled();
108    });
109
110    it('should allow multiple data arguments', function () {
111      spyOn(mockIoSocket, 'emit');
112      socket.emit('event', 'x', 'y');
113      expect(mockIoSocket.emit).toHaveBeenCalledWith('event', 'x', 'y');
114    });
115
116    it('should wrap the callback with multiple data arguments', function () {
117      spyOn(mockIoSocket, 'emit');
118      socket.emit('event', 'x', 'y', spy);
119      expect(mockIoSocket.emit.mostRecentCall.args[3]).toNotBe(spy);
120
121      mockIoSocket.emit.mostRecentCall.args[3]();
122      expect(spy).not.toHaveBeenCalled();
123      $timeout.flush();
124
125      expect(spy).toHaveBeenCalled();
126    });
127
128  });
129
130
131  describe('#removeListener', function () {
132
133    it('should not call after removing an event', function () {
134      socket.on('event', spy);
135      socket.removeListener('event', spy);
136
137      mockIoSocket.emit('event');
138
139      expect($browser.deferredFns.length).toBe(0);
140    });
141
142  });
143
144
145  describe('#removeAllListeners', function () {
146
147    it('should not call after removing listeners for an event', function () {
148      socket.on('event', spy);
149      socket.removeAllListeners('event');
150
151      mockIoSocket.emit('event');
152
153      expect($browser.deferredFns.length).toBe(0);
154    });
155
156    it('should not call after removing all listeners', function () {
157      socket.on('event', spy);
158      socket.on('event2', spy);
159      socket.removeAllListeners();
160
161      mockIoSocket.emit('event');
162      mockIoSocket.emit('event2');
163
164      expect($browser.deferredFns.length).toBe(0);
165    });
166
167  });
168
169
170  describe('#forward', function () {
171
172    it('should forward events', function () {
173      socket.forward('event');
174
175      scope.$on('socket:event', spy);
176      mockIoSocket.emit('event');
177      $timeout.flush();
178
179      expect(spy).toHaveBeenCalled();
180    });
181
182    it('should forward an array of events', function () {
183      socket.forward(['e1', 'e2']);
184
185      scope.$on('socket:e1', spy);
186      scope.$on('socket:e2', spy);
187
188      mockIoSocket.emit('e1');
189      mockIoSocket.emit('e2');
190      $timeout.flush();
191      expect(spy.callCount).toBe(2);
192    });
193
194    it('should remove watchers when the scope is removed', function () {
195
196      socket.forward('event');
197      scope.$on('socket:event', spy);
198      mockIoSocket.emit('event');
199      $timeout.flush();
200
201      expect(spy).toHaveBeenCalled();
202
203      scope.$destroy();
204      spy.reset();
205      mockIoSocket.emit('event');
206      expect(spy).not.toHaveBeenCalled();
207    });
208
209    it('should use the specified prefix', inject(function (socketFactory) {
210      var socket = socketFactory({
211        ioSocket: mockIoSocket,
212        scope: scope,
213        prefix: 'custom:'
214      });
215
216      socket.forward('event');
217
218      scope.$on('custom:event', spy);
219      mockIoSocket.emit('event');
220      $timeout.flush();
221
222      expect(spy).toHaveBeenCalled();
223    }));
224
225    it('should use an empty prefix if specified', inject(function (socketFactory) {
226      var socket = socketFactory({
227        ioSocket: mockIoSocket,
228        scope: scope,
229        prefix: ''
230      });
231
232      socket.forward('event');
233
234      scope.$on('event', spy);
235      mockIoSocket.emit('event');
236      $timeout.flush();
237
238      expect(spy).toHaveBeenCalled();
239    }));
240
241    it('should forward to the specified scope when one is provided', function () {
242      var child = scope.$new();
243      spyOn(child, '$broadcast');
244      socket.forward('event', child);
245
246      scope.$on('socket:event', spy);
247      mockIoSocket.emit('event');
248      $timeout.flush();
249
250      expect(child.$broadcast).toHaveBeenCalled();
251    });
252
253    it('should pass all arguments to scope.$on', function () {
254      socket.forward('event');
255      scope.$on('socket:event', spy);
256      mockIoSocket.emit('event', 1, 2, 3);
257      $timeout.flush();
258
259      expect(spy.calls[0].args.slice(1)).toEqual([1, 2, 3]);
260    });
261  });
262
263});