events_test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const EventEmitter = require('../../lib/events').EventEmitter;
  19. const assert = require('assert');
  20. const sinon = require('sinon');
  21. describe('EventEmitter', function() {
  22. describe('#emit()', function() {
  23. it('can emit events when nothing is registered', function() {
  24. let emitter = new EventEmitter;
  25. emitter.emit('foo');
  26. // Ok if no errors are thrown.
  27. });
  28. it('can pass args to listeners on emit', function() {
  29. let emitter = new EventEmitter;
  30. let now = Date.now();
  31. let messages = [];
  32. emitter.on('foo', (arg) => messages.push(arg));
  33. emitter.emit('foo', now);
  34. assert.deepEqual([now], messages);
  35. emitter.emit('foo', now + 15);
  36. assert.deepEqual([now, now + 15], messages);
  37. });
  38. });
  39. describe('#addListener()', function() {
  40. it('can add multiple listeners for one event', function() {
  41. let emitter = new EventEmitter;
  42. let count = 0;
  43. emitter.addListener('foo', () => count++);
  44. emitter.addListener('foo', () => count++);
  45. emitter.addListener('foo', () => count++);
  46. emitter.emit('foo');
  47. assert.equal(3, count);
  48. });
  49. it('only registers each listener function once', function() {
  50. let emitter = new EventEmitter;
  51. let count = 0;
  52. let onFoo = () => count++;
  53. emitter.addListener('foo', onFoo);
  54. emitter.addListener('foo', onFoo);
  55. emitter.addListener('foo', onFoo);
  56. emitter.emit('foo');
  57. assert.equal(1, count);
  58. emitter.emit('foo');
  59. assert.equal(2, count);
  60. });
  61. it('allows users to specify a custom scope', function() {
  62. let obj = {
  63. count: 0,
  64. inc: function() {
  65. this.count++;
  66. }
  67. };
  68. let emitter = new EventEmitter;
  69. emitter.addListener('foo', obj.inc, obj);
  70. emitter.emit('foo');
  71. assert.equal(1, obj.count);
  72. emitter.emit('foo');
  73. assert.equal(2, obj.count);
  74. });
  75. });
  76. describe('#once()', function() {
  77. it('only calls registered callback once', function() {
  78. let emitter = new EventEmitter;
  79. let count = 0;
  80. emitter.once('foo', () => count++);
  81. emitter.once('foo', () => count++);
  82. emitter.once('foo', () => count++);
  83. emitter.emit('foo');
  84. assert.equal(3, count);
  85. emitter.emit('foo');
  86. assert.equal(3, count);
  87. emitter.emit('foo');
  88. assert.equal(3, count);
  89. });
  90. });
  91. describe('#removeListeners()', function() {
  92. it('only removes the given listener function', function() {
  93. let emitter = new EventEmitter;
  94. let count = 0;
  95. emitter.addListener('foo', () => count++);
  96. emitter.addListener('foo', () => count++);
  97. let toRemove = () => count++;
  98. emitter.addListener('foo', toRemove);
  99. emitter.emit('foo');
  100. assert.equal(3, count);
  101. emitter.removeListener('foo', toRemove);
  102. emitter.emit('foo');
  103. assert.equal(5, count);
  104. });
  105. });
  106. describe('#removeAllListeners()', function() {
  107. it('only removes listeners for type if specified', function() {
  108. let emitter = new EventEmitter;
  109. let count = 0;
  110. emitter.addListener('foo', () => count++);
  111. emitter.addListener('foo', () => count++);
  112. emitter.addListener('foo', () => count++);
  113. emitter.addListener('bar', () => count++);
  114. emitter.emit('foo');
  115. assert.equal(3, count);
  116. emitter.removeAllListeners('foo');
  117. emitter.emit('foo');
  118. assert.equal(3, count);
  119. emitter.emit('bar');
  120. assert.equal(4, count);
  121. });
  122. it('removes absolutely all listeners if no type specified', function() {
  123. let emitter = new EventEmitter;
  124. let count = 0;
  125. emitter.addListener('foo', () => count++);
  126. emitter.addListener('bar', () => count++);
  127. emitter.addListener('baz', () => count++);
  128. emitter.addListener('baz', () => count++);
  129. emitter.emit('foo');
  130. assert.equal(1, count);
  131. emitter.emit('baz');
  132. assert.equal(3, count);
  133. emitter.removeAllListeners();
  134. emitter.emit('foo');
  135. assert.equal(3, count);
  136. emitter.emit('bar');
  137. assert.equal(3, count);
  138. emitter.emit('baz');
  139. assert.equal(3, count);
  140. });
  141. });
  142. });