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.

327 lines
8.4 KiB

8 years ago
  1. /* istanbul ignore next */
  2. describe('Ultron', function () {
  3. 'use strict';
  4. var EventEmitter = require('eventemitter3')
  5. , EE = require('events').EventEmitter
  6. , assume = require('assume')
  7. , Ultron = require('./')
  8. , ultron
  9. , ee;
  10. beforeEach(function () {
  11. ee = new EventEmitter();
  12. ultron = new Ultron(ee);
  13. });
  14. afterEach(function () {
  15. ultron.destroy();
  16. ee.removeAllListeners();
  17. });
  18. it('is exposed as a function', function () {
  19. assume(Ultron).is.a('function');
  20. });
  21. it('can be initialized without the new keyword', function () {
  22. assume(Ultron(ee)).is.instanceOf(Ultron);
  23. });
  24. it('assigns a unique id to every instance', function () {
  25. for (var i = 0; i < 100; i++) {
  26. assume(ultron.id).does.not.equal((new Ultron()).id);
  27. }
  28. });
  29. it('allows removal through the event emitter', function () {
  30. function foo() {}
  31. function bar() {}
  32. ultron.on('foo', foo);
  33. ultron.once('foo', bar);
  34. assume(foo.__ultron).equals(ultron.id);
  35. assume(bar.__ultron).equals(ultron.id);
  36. assume(ee.listeners('foo').length).equals(2);
  37. ee.removeListener('foo', foo);
  38. assume(ee.listeners('foo').length).equals(1);
  39. ee.removeListener('foo', bar);
  40. assume(ee.listeners('foo').length).equals(0);
  41. });
  42. describe('#on', function () {
  43. it('assigns a listener', function () {
  44. assume(ee.listeners('foo').length).equals(0);
  45. function foo() {}
  46. ultron.on('foo', foo);
  47. assume(ee.listeners('foo').length).equals(1);
  48. assume(ee.listeners('foo')[0]).equals(foo);
  49. });
  50. it('tags the assigned function', function () {
  51. assume(ee.listeners('foo').length).equals(0);
  52. ultron.on('foo', function () {});
  53. assume(ee.listeners('foo')[0].__ultron).equals(ultron.id);
  54. });
  55. it('also passes in the context', function (next) {
  56. var context = 1313;
  57. ultron.on('foo', function (a, b, c) {
  58. assume(a).equals('a');
  59. assume(b).equals('b');
  60. assume(c).equals('c');
  61. assume(this).equals(context);
  62. next();
  63. }, context);
  64. ee.emit('foo', 'a', 'b', 'c');
  65. });
  66. it('works with regular eventemitters as well', function (next) {
  67. var ee = new EE()
  68. , ultron = new Ultron(ee);
  69. ultron.on('foo', function (a, b, c) {
  70. assume(a).equals('a');
  71. assume(b).equals('b');
  72. assume(c).equals('c');
  73. next();
  74. });
  75. ee.emit('foo', 'a', 'b', 'c');
  76. });
  77. });
  78. describe('#once', function () {
  79. it('assigns a listener', function () {
  80. assume(ee.listeners('foo').length).equals(0);
  81. function foo() {}
  82. ultron.once('foo', foo);
  83. assume(ee.listeners('foo').length).equals(1);
  84. assume(ee.listeners('foo')[0]).equals(foo);
  85. });
  86. it('tags the assigned function', function () {
  87. assume(ee.listeners('foo').length).equals(0);
  88. ultron.once('foo', function () {});
  89. assume(ee.listeners('foo')[0].__ultron).equals(ultron.id);
  90. });
  91. it('also passes in the context', function (next) {
  92. var context = 1313;
  93. ultron.once('foo', function (a, b, c) {
  94. assume(a).equals('a');
  95. assume(b).equals('b');
  96. assume(c).equals('c');
  97. assume(this).equals(context);
  98. next();
  99. }, context);
  100. ee.emit('foo', 'a', 'b', 'c');
  101. ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute
  102. });
  103. it('works with regular eventemitters as well', function (next) {
  104. var ee = new EE()
  105. , ultron = new Ultron(ee);
  106. ultron.once('foo', function (a, b, c) {
  107. assume(a).equals('a');
  108. assume(b).equals('b');
  109. assume(c).equals('c');
  110. next();
  111. });
  112. ee.emit('foo', 'a', 'b', 'c');
  113. ee.emit('foo', 'a', 'b', 'c'); // Ensure that we don't double execute
  114. });
  115. });
  116. describe('#remove', function () {
  117. it('removes only our assigned `on` listeners', function () {
  118. function foo() {}
  119. function bar() {}
  120. ee.on('foo', foo);
  121. ultron.on('foo', bar);
  122. assume(ee.listeners('foo').length).equals(2);
  123. ultron.remove('foo');
  124. assume(ee.listeners('foo').length).equals(1);
  125. assume(ee.listeners('foo')[0]).equals(foo);
  126. });
  127. it('removes our private __ultron references', function () {
  128. function once() {}
  129. function on() {}
  130. assume('__ultron' in once).is.false();
  131. assume('__ultron' in on).is.false();
  132. ultron.on('foo', on);
  133. ultron.once('bar', once);
  134. assume('__ultron' in once).is.true();
  135. assume('__ultron' in on).is.true();
  136. ultron.remove('foo, bar');
  137. assume('__ultron' in once).is.false();
  138. assume('__ultron' in on).is.false();
  139. ultron.destroy();
  140. ee = new EE();
  141. ultron = new Ultron(ee);
  142. assume('__ultron' in once).is.false();
  143. assume('__ultron' in on).is.false();
  144. ultron.on('foo', on);
  145. ultron.once('bar', once);
  146. assume('__ultron' in once).is.true();
  147. assume('__ultron' in on).is.true();
  148. ultron.remove('foo, bar');
  149. assume('__ultron' in once).is.false();
  150. assume('__ultron' in on).is.false();
  151. });
  152. it('removes only our assigned `once` listeners', function () {
  153. function foo() {}
  154. function bar() {}
  155. ee.once('foo', foo);
  156. ultron.once('foo', bar);
  157. assume(ee.listeners('foo').length).equals(2);
  158. ultron.remove('foo');
  159. assume(ee.listeners('foo').length).equals(1);
  160. assume(ee.listeners('foo')[0]).equals(foo);
  161. });
  162. it('removes only our assigned `once` listeners from regular EE', function () {
  163. var ee = new EE()
  164. , ultron = new Ultron(ee);
  165. function foo() {}
  166. function bar() {}
  167. ee.once('foo', foo);
  168. ultron.once('foo', bar);
  169. assume(ee.listeners('foo').length).equals(2);
  170. ultron.remove('foo');
  171. assume(ee.listeners('foo').length).equals(1);
  172. assume(ee.listeners('foo')[0].listener).equals(foo);
  173. });
  174. it('removes all assigned events if called without args', function () {
  175. function foo() {}
  176. function bar() {}
  177. ultron.on('foo', foo);
  178. ultron.on('bar', bar);
  179. assume(ee.listeners('foo').length).equals(1);
  180. assume(ee.listeners('bar').length).equals(1);
  181. ultron.remove();
  182. assume(ee.listeners('foo').length).equals(0);
  183. assume(ee.listeners('bar').length).equals(0);
  184. });
  185. it('removes multiple listeners based on args', function () {
  186. function foo() {}
  187. function bar() {}
  188. function baz() {}
  189. ultron.on('foo', foo);
  190. ultron.on('bar', bar);
  191. ultron.on('baz', baz);
  192. assume(ee.listeners('foo').length).equals(1);
  193. assume(ee.listeners('bar').length).equals(1);
  194. assume(ee.listeners('baz').length).equals(1);
  195. ultron.remove('foo', 'bar');
  196. assume(ee.listeners('foo').length).equals(0);
  197. assume(ee.listeners('bar').length).equals(0);
  198. assume(ee.listeners('baz').length).equals(1);
  199. });
  200. it('removes multiple listeners if first arg is seperated string', function () {
  201. function foo() {}
  202. function bar() {}
  203. function baz() {}
  204. ultron.on('foo', foo);
  205. ultron.on('bar', bar);
  206. ultron.on('baz', baz);
  207. assume(ee.listeners('foo').length).equals(1);
  208. assume(ee.listeners('bar').length).equals(1);
  209. assume(ee.listeners('baz').length).equals(1);
  210. ultron.remove('foo, bar');
  211. assume(ee.listeners('foo').length).equals(0);
  212. assume(ee.listeners('bar').length).equals(0);
  213. assume(ee.listeners('baz').length).equals(1);
  214. });
  215. });
  216. describe('#destroy', function () {
  217. it('removes all listeners', function () {
  218. function foo() {}
  219. function bar() {}
  220. function baz() {}
  221. ultron.on('foo', foo);
  222. ultron.on('bar', bar);
  223. ultron.on('baz', baz);
  224. assume(ee.listeners('foo').length).equals(1);
  225. assume(ee.listeners('bar').length).equals(1);
  226. assume(ee.listeners('baz').length).equals(1);
  227. ultron.destroy();
  228. assume(ee.listeners('foo').length).equals(0);
  229. assume(ee.listeners('bar').length).equals(0);
  230. assume(ee.listeners('baz').length).equals(0);
  231. });
  232. it('removes the .ee reference', function () {
  233. assume(ultron.ee).equals(ee);
  234. ultron.destroy();
  235. assume(ultron.ee).equals(null);
  236. });
  237. it('returns booleans for state indication', function () {
  238. assume(ultron.destroy()).is.true();
  239. assume(ultron.destroy()).is.false();
  240. assume(ultron.destroy()).is.false();
  241. assume(ultron.destroy()).is.false();
  242. });
  243. });
  244. });