You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
5.8 KiB

7 years ago
  1. /*
  2. * angular-socket-io v0.4.1
  3. * (c) 2014 Brian Ford http://briantford.com
  4. * License: MIT
  5. */
  6. 'use strict';
  7. describe('socketFactory', function () {
  8. beforeEach(module('btford.socket-io'));
  9. var socket,
  10. scope,
  11. $timeout,
  12. $browser,
  13. mockIoSocket,
  14. spy;
  15. beforeEach(inject(function (socketFactory, _$browser_, $rootScope, _$timeout_) {
  16. $browser = _$browser_;
  17. $timeout = _$timeout_;
  18. scope = $rootScope.$new();
  19. spy = jasmine.createSpy('emitSpy');
  20. mockIoSocket = io.connect();
  21. socket = socketFactory({
  22. ioSocket: mockIoSocket,
  23. scope: scope
  24. });
  25. }));
  26. describe('#on', function () {
  27. it('should apply asynchronously', function () {
  28. socket.on('event', spy);
  29. mockIoSocket.emit('event');
  30. expect(spy).not.toHaveBeenCalled();
  31. $timeout.flush();
  32. expect(spy).toHaveBeenCalled();
  33. });
  34. });
  35. describe('#disconnect', function () {
  36. it('should call the underlying socket.disconnect', function () {
  37. mockIoSocket.disconnect = spy;
  38. socket.disconnect();
  39. expect(spy).toHaveBeenCalled();
  40. });
  41. });
  42. describe('#connect', function () {
  43. it('should call the underlying socket.connect', function () {
  44. mockIoSocket.connect = spy;
  45. socket.connect();
  46. expect(spy).toHaveBeenCalled();
  47. });
  48. });
  49. describe('#once', function () {
  50. it('should apply asynchronously', function () {
  51. socket.once('event', spy);
  52. mockIoSocket.emit('event');
  53. expect(spy).not.toHaveBeenCalled();
  54. $timeout.flush();
  55. expect(spy).toHaveBeenCalled();
  56. });
  57. it('should only run once', function () {
  58. var counter = 0;
  59. socket.once('event', function () {
  60. counter += 1;
  61. });
  62. mockIoSocket.emit('event');
  63. mockIoSocket.emit('event');
  64. $timeout.flush();
  65. expect(counter).toBe(1);
  66. });
  67. });
  68. describe('#emit', function () {
  69. it('should call the delegate socket\'s emit', function () {
  70. spyOn(mockIoSocket, 'emit');
  71. socket.emit('event', {foo: 'bar'});
  72. expect(mockIoSocket.emit).toHaveBeenCalled();
  73. });
  74. it('should allow multiple data arguments', function () {
  75. spyOn(mockIoSocket, 'emit');
  76. socket.emit('event', 'x', 'y');
  77. expect(mockIoSocket.emit).toHaveBeenCalledWith('event', 'x', 'y');
  78. });
  79. it('should wrap the callback with multiple data arguments', function () {
  80. spyOn(mockIoSocket, 'emit');
  81. socket.emit('event', 'x', 'y', spy);
  82. expect(mockIoSocket.emit.mostRecentCall.args[3]).toNotBe(spy);
  83. mockIoSocket.emit.mostRecentCall.args[3]();
  84. expect(spy).not.toHaveBeenCalled();
  85. $timeout.flush();
  86. expect(spy).toHaveBeenCalled();
  87. });
  88. });
  89. describe('#removeListener', function () {
  90. it('should not call after removing an event', function () {
  91. socket.on('event', spy);
  92. socket.removeListener('event', spy);
  93. mockIoSocket.emit('event');
  94. expect($browser.deferredFns.length).toBe(0);
  95. });
  96. });
  97. describe('#removeAllListeners', function () {
  98. it('should not call after removing listeners for an event', function () {
  99. socket.on('event', spy);
  100. socket.removeAllListeners('event');
  101. mockIoSocket.emit('event');
  102. expect($browser.deferredFns.length).toBe(0);
  103. });
  104. it('should not call after removing all listeners', function () {
  105. socket.on('event', spy);
  106. socket.on('event2', spy);
  107. socket.removeAllListeners();
  108. mockIoSocket.emit('event');
  109. mockIoSocket.emit('event2');
  110. expect($browser.deferredFns.length).toBe(0);
  111. });
  112. });
  113. describe('#forward', function () {
  114. it('should forward events', function () {
  115. socket.forward('event');
  116. scope.$on('socket:event', spy);
  117. mockIoSocket.emit('event');
  118. $timeout.flush();
  119. expect(spy).toHaveBeenCalled();
  120. });
  121. it('should forward an array of events', function () {
  122. socket.forward(['e1', 'e2']);
  123. scope.$on('socket:e1', spy);
  124. scope.$on('socket:e2', spy);
  125. mockIoSocket.emit('e1');
  126. mockIoSocket.emit('e2');
  127. $timeout.flush();
  128. expect(spy.callCount).toBe(2);
  129. });
  130. it('should remove watchers when the scope is removed', function () {
  131. socket.forward('event');
  132. scope.$on('socket:event', spy);
  133. mockIoSocket.emit('event');
  134. $timeout.flush();
  135. expect(spy).toHaveBeenCalled();
  136. scope.$destroy();
  137. spy.reset();
  138. mockIoSocket.emit('event');
  139. expect(spy).not.toHaveBeenCalled();
  140. });
  141. it('should use the specified prefix', inject(function (socketFactory) {
  142. var socket = socketFactory({
  143. ioSocket: mockIoSocket,
  144. scope: scope,
  145. prefix: 'custom:'
  146. });
  147. socket.forward('event');
  148. scope.$on('custom:event', spy);
  149. mockIoSocket.emit('event');
  150. $timeout.flush();
  151. expect(spy).toHaveBeenCalled();
  152. }));
  153. it('should use an empty prefix if specified', inject(function (socketFactory) {
  154. var socket = socketFactory({
  155. ioSocket: mockIoSocket,
  156. scope: scope,
  157. prefix: ''
  158. });
  159. socket.forward('event');
  160. scope.$on('event', spy);
  161. mockIoSocket.emit('event');
  162. $timeout.flush();
  163. expect(spy).toHaveBeenCalled();
  164. }));
  165. it('should forward to the specified scope when one is provided', function () {
  166. var child = scope.$new();
  167. spyOn(child, '$broadcast');
  168. socket.forward('event', child);
  169. scope.$on('socket:event', spy);
  170. mockIoSocket.emit('event');
  171. $timeout.flush();
  172. expect(child.$broadcast).toHaveBeenCalled();
  173. });
  174. it('should pass all arguments to scope.$on', function () {
  175. socket.forward('event');
  176. scope.$on('socket:event', spy);
  177. mockIoSocket.emit('event', 1, 2, 3);
  178. $timeout.flush();
  179. expect(spy.calls[0].args.slice(1)).toEqual([1, 2, 3]);
  180. });
  181. });
  182. });