EventEmitterTests.js 2.3 KB

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