2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-07-09 09:56:33 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
import EventEmitter from '../../../frontend/js/utils/EventEmitter'
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
export default describe('EventEmitter', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
return (this.eventEmitter = new EventEmitter())
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('calls listeners', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const cb1 = sinon.stub()
|
|
|
|
const cb2 = sinon.stub()
|
|
|
|
this.eventEmitter.on('foo', cb1)
|
|
|
|
this.eventEmitter.on('bar', cb2)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.eventEmitter.trigger('foo')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
expect(cb1).to.have.been.called
|
|
|
|
return expect(cb2).to.not.have.been.called
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('calls multiple listeners', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const cb1 = sinon.stub()
|
|
|
|
const cb2 = sinon.stub()
|
|
|
|
this.eventEmitter.on('foo', cb1)
|
|
|
|
this.eventEmitter.on('foo', cb2)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.eventEmitter.trigger('foo')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
expect(cb1).to.have.been.called
|
|
|
|
return expect(cb2).to.have.been.called
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('calls listeners with namespace', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const cb1 = sinon.stub()
|
|
|
|
const cb2 = sinon.stub()
|
|
|
|
this.eventEmitter.on('foo', cb1)
|
|
|
|
this.eventEmitter.on('foo.bar', cb2)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.eventEmitter.trigger('foo')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
expect(cb1).to.have.been.called
|
|
|
|
return expect(cb2).to.have.been.called
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('removes listeners', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const cb = sinon.stub()
|
|
|
|
this.eventEmitter.on('foo', cb)
|
|
|
|
this.eventEmitter.off('foo')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.eventEmitter.trigger('foo')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return expect(cb).to.not.have.been.called
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('removes namespaced listeners', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const cb = sinon.stub()
|
|
|
|
this.eventEmitter.on('foo.bar', cb)
|
|
|
|
this.eventEmitter.off('foo.bar')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.eventEmitter.trigger('foo')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return expect(cb).to.not.have.been.called
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('does not remove unnamespaced listeners if off called with namespace', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
const cb1 = sinon.stub()
|
|
|
|
const cb2 = sinon.stub()
|
|
|
|
this.eventEmitter.on('foo', cb1)
|
|
|
|
this.eventEmitter.on('foo.bar', cb2)
|
|
|
|
this.eventEmitter.off('foo.bar')
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.eventEmitter.trigger('foo')
|
|
|
|
|
|
|
|
expect(cb1).to.have.been.called
|
|
|
|
return expect(cb2).to.not.have.been.called
|
|
|
|
})
|
|
|
|
})
|