Skip to content

Commit 7313d3a

Browse files
committed
Add tests for duplicate listeners
1 parent 34ce9a4 commit 7313d3a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ describe('mitt#', () => {
6565
inst.on(eventType, foo);
6666
expect(events.get(eventType)).to.deep.equal([foo]);
6767
});
68+
69+
// Adding the same listener multiple times should register it multiple times.
70+
// See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener
71+
it('should add duplicate listeners', () => {
72+
const foo = () => {};
73+
inst.on('foo', foo);
74+
inst.on('foo', foo);
75+
expect(events.get('foo')).to.deep.equal([foo, foo]);
76+
});
6877
});
6978

7079
describe('off()', () => {
@@ -98,6 +107,16 @@ describe('mitt#', () => {
98107
expect(events.has('bar')).to.equal(false);
99108
expect(events.get('baz:bat!')).to.have.lengthOf(1);
100109
});
110+
111+
it('should remove only the first matching listener', () => {
112+
const foo = () => {};
113+
inst.on('foo', foo);
114+
inst.on('foo', foo);
115+
inst.off('foo', foo);
116+
expect(events.get('foo')).to.deep.equal([foo]);
117+
inst.off('foo', foo);
118+
expect(events.get('foo')).to.deep.equal([]);
119+
});
101120
});
102121

103122
describe('emit()', () => {

0 commit comments

Comments
 (0)