EventEmitterTests.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import { expect } from 'chai'
  14. import sinon from 'sinon'
  15. import EventEmitter from '../../../frontend/js/utils/EventEmitter'
  16. export default describe('EventEmitter', function() {
  17. beforeEach(function() {
  18. return (this.eventEmitter = new EventEmitter())
  19. })
  20. it('calls listeners', function() {
  21. const cb1 = sinon.stub()
  22. const cb2 = sinon.stub()
  23. this.eventEmitter.on('foo', cb1)
  24. this.eventEmitter.on('bar', cb2)
  25. this.eventEmitter.trigger('foo')
  26. expect(cb1).to.have.been.called
  27. return expect(cb2).to.not.have.been.called
  28. })
  29. it('calls multiple listeners', function() {
  30. const cb1 = sinon.stub()
  31. const cb2 = sinon.stub()
  32. this.eventEmitter.on('foo', cb1)
  33. this.eventEmitter.on('foo', cb2)
  34. this.eventEmitter.trigger('foo')
  35. expect(cb1).to.have.been.called
  36. return expect(cb2).to.have.been.called
  37. })
  38. it('calls listeners with namespace', function() {
  39. const cb1 = sinon.stub()
  40. const cb2 = sinon.stub()
  41. this.eventEmitter.on('foo', cb1)
  42. this.eventEmitter.on('foo.bar', cb2)
  43. this.eventEmitter.trigger('foo')
  44. expect(cb1).to.have.been.called
  45. return expect(cb2).to.have.been.called
  46. })
  47. it('removes listeners', function() {
  48. const cb = sinon.stub()
  49. this.eventEmitter.on('foo', cb)
  50. this.eventEmitter.off('foo')
  51. this.eventEmitter.trigger('foo')
  52. return expect(cb).to.not.have.been.called
  53. })
  54. it('removes namespaced listeners', function() {
  55. const cb = sinon.stub()
  56. this.eventEmitter.on('foo.bar', cb)
  57. this.eventEmitter.off('foo.bar')
  58. this.eventEmitter.trigger('foo')
  59. return expect(cb).to.not.have.been.called
  60. })
  61. it('does not remove unnamespaced listeners if off called with namespace', function() {
  62. const cb1 = sinon.stub()
  63. const cb2 = sinon.stub()
  64. this.eventEmitter.on('foo', cb1)
  65. this.eventEmitter.on('foo.bar', cb2)
  66. this.eventEmitter.off('foo.bar')
  67. this.eventEmitter.trigger('foo')
  68. expect(cb1).to.have.been.called
  69. return expect(cb2).to.not.have.been.called
  70. })
  71. })