Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('Should fire nested events within loop', () => {
const spyA1 = sinon.spy();
const spyA2 = sinon.spy();
const spyB = sinon.spy();
publisher.on('eventA', spyA1);
publisher.on('eventA', () => {
publisher.fireEvent('eventB');
});
publisher.on('eventA', spyA2);
publisher.on('eventB', spyB);
publisher.fireEvent('eventA');
expect(spyB.called).to.be.true;
expect(spyA1.calledImmediatelyBefore(spyB)).to.be.true;
expect(spyA2.calledImmediatelyAfter(spyB)).to.be.true;
});
it('Should remove once listener after event', () => {
const spy = sinon.spy();
publisher.once('event', spy);
publisher.fireEvent('event');
publisher.fireEvent('event');
expect(spy.callCount).eq(1);
});
it('Should not call listeners removed during dispatching for nested events', () => {
const spy1 = sinon.spy();
const spy2 = sinon.spy();
publisher.on('event', spy1);
publisher.on('event', (event, depth) => {
publisher.off('event', spy2);
if (depth < 2) {
publisher.fireEvent('event', depth + 1);
}
});
publisher.on('event', spy2);
publisher.fireEvent('event', 1);
expect(spy1.calledTwice).to.be.true;
expect(spy2.calledOnce).to.be.true;
expect(spy2.calledWithExactly('event', 1)).to.be.true;
});
it('removeAllListeners should remove all once listeners', () => {
const spy1 = sinon.spy();
const spy2 = sinon.spy();
publisher.once('event', spy1);
publisher.once('event', spy2);
publisher.removeAllListeners();
publisher.fireEvent('event');
expect(spy1.called).to.be.false;
expect(spy2.called).to.be.false;
});
it('Should run callbacks after event loop', () => {
const spy1 = sinon.spy();
const spy2 = sinon.spy();
const afterSpy = sinon.spy();
publisher.on('event', spy1);
publisher.on('event', () => {
publisher.runAfterCurrentEvent(afterSpy);
});
publisher.on('event', spy2);
publisher.fireEvent('event');
expect(afterSpy.called).to.be.true;
expect(afterSpy.calledAfter(spy1)).to.be.true;
expect(afterSpy.calledAfter(spy2)).to.be.true;
});
it('Should not call listeners added during dispatching', () => {
const spy = sinon.spy();
publisher.on('event', () => {
publisher.on('event', spy);
});
publisher.fireEvent('event');
expect(spy.called).to.be.false;
});
it('Same listener should not be removed from other events', () => {
const spy = sinon.spy();
publisher.once('event1', spy);
publisher.once('event2', spy);
publisher.off('event1', spy);
publisher.fireEvent('event2');
expect(spy.calledOnce).to.be.true;
});
});
it('Should not call listeners removed during dispatching for nested events', () => {
const spy1 = sinon.spy();
const spy2 = sinon.spy();
publisher.on('event', spy1);
publisher.on('event', (event, depth) => {
publisher.off('event', spy2);
if (depth < 2) {
publisher.fireEvent('event', depth + 1);
}
});
publisher.on('event', spy2);
publisher.fireEvent('event', 1);
expect(spy1.calledTwice).to.be.true;
expect(spy2.calledOnce).to.be.true;
expect(spy2.calledWithExactly('event', 1)).to.be.true;
});
it('Should call listeners with arguments', () => {
const spy = sinon.spy();
publisher.on('event', spy);
publisher.fireEvent('event', 'foo');
expect(spy.calledWithExactly('event', 'foo')).to.be.true;
});
it('Should call listeners in order of subscribing', () => {
const spy1 = sinon.spy();
const spy2 = sinon.spy();
publisher.on('event', spy1);
publisher.on('event', spy2);
publisher.fireEvent('event');
expect(spy1.calledBefore(spy2)).to.be.true;
});