-
Notifications
You must be signed in to change notification settings - Fork 424
/
index_test.ts
212 lines (177 loc) 路 5.22 KB
/
index_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import mitt, { Emitter, EventHandlerMap } from '..';
import chai, { expect } from 'chai';
import { spy } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
describe('mitt', () => {
it('should default export be a function', () => {
expect(mitt).to.be.a('function');
});
it('should accept an optional event handler map', () => {
expect(() => mitt(new Map())).not.to.throw;
const map = new Map();
const a = spy();
const b = spy();
map.set('foo', [a, b]);
const events = mitt<{ foo: undefined }>(map);
events.emit('foo');
expect(a).to.have.been.calledOnce;
expect(b).to.have.been.calledOnce;
});
});
describe('mitt#', () => {
const eventType = Symbol('eventType');
type Events = {
foo: unknown;
constructor: unknown;
FOO: unknown;
bar: unknown;
Bar: unknown;
'baz:bat!': unknown;
'baz:baT!': unknown;
Foo: unknown;
[eventType]: unknown;
};
let events: EventHandlerMap<Events>, inst: Emitter<Events>;
beforeEach(() => {
events = new Map();
inst = mitt(events);
});
describe('properties', () => {
it('should expose the event handler map', () => {
expect(inst)
.to.have.property('all')
.that.is.a('map');
});
});
describe('on()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('on')
.that.is.a('function');
});
it('should register handler for new type', () => {
const foo = () => {};
inst.on('foo', foo);
expect(events.get('foo')).to.deep.equal([foo]);
});
it('should register handlers for any type strings', () => {
const foo = () => {};
inst.on('constructor', foo);
expect(events.get('constructor')).to.deep.equal([foo]);
});
it('should append handler for existing type', () => {
const foo = () => {};
const bar = () => {};
inst.on('foo', foo);
inst.on('foo', bar);
expect(events.get('foo')).to.deep.equal([foo, bar]);
});
it('should NOT normalize case', () => {
const foo = () => {};
inst.on('FOO', foo);
inst.on('Bar', foo);
inst.on('baz:baT!', foo);
expect(events.get('FOO')).to.deep.equal([foo]);
expect(events.has('foo')).to.equal(false);
expect(events.get('Bar')).to.deep.equal([foo]);
expect(events.has('bar')).to.equal(false);
expect(events.get('baz:baT!')).to.deep.equal([foo]);
});
it('can take symbols for event types', () => {
const foo = () => {};
inst.on(eventType, foo);
expect(events.get(eventType)).to.deep.equal([foo]);
});
// Adding the same listener multiple times should register it multiple times.
// See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener
it('should add duplicate listeners', () => {
const foo = () => {};
inst.on('foo', foo);
inst.on('foo', foo);
expect(events.get('foo')).to.deep.equal([foo, foo]);
});
});
describe('off()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('off')
.that.is.a('function');
});
it('should remove handler for type', () => {
const foo = () => {};
inst.on('foo', foo);
inst.off('foo', foo);
expect(events.get('foo')).to.be.empty;
});
it('should NOT normalize case', () => {
const foo = () => {};
inst.on('FOO', foo);
inst.on('Bar', foo);
inst.on('baz:bat!', foo);
inst.off('FOO', foo);
inst.off('Bar', foo);
inst.off('baz:baT!', foo);
expect(events.get('FOO')).to.be.empty;
expect(events.has('foo')).to.equal(false);
expect(events.get('Bar')).to.be.empty;
expect(events.has('bar')).to.equal(false);
expect(events.get('baz:bat!')).to.have.lengthOf(1);
});
it('should remove only the first matching listener', () => {
const foo = () => {};
inst.on('foo', foo);
inst.on('foo', foo);
inst.off('foo', foo);
expect(events.get('foo')).to.deep.equal([foo]);
inst.off('foo', foo);
expect(events.get('foo')).to.deep.equal([]);
});
it('off("type") should remove all handlers of the given type', () => {
inst.on('foo', () => {});
inst.on('foo', () => {});
inst.on('bar', () => {});
inst.off('foo');
expect(events.get('foo')).to.deep.equal([]);
expect(events.get('bar')).to.have.length(1);
inst.off('bar');
expect(events.get('bar')).to.deep.equal([]);
});
});
describe('emit()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('emit')
.that.is.a('function');
});
it('should invoke handler for type', () => {
const event = { a: 'b' };
inst.on('foo', (one, two?: unknown) => {
expect(one).to.deep.equal(event);
expect(two).to.be.an('undefined');
});
inst.emit('foo', event);
});
it('should NOT ignore case', () => {
const onFoo = spy(),
onFOO = spy();
events.set('Foo', [onFoo]);
events.set('FOO', [onFOO]);
inst.emit('Foo', 'Foo arg');
inst.emit('FOO', 'FOO arg');
expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg');
expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');
});
it('should invoke * handlers', () => {
const star = spy(),
ea = { a: 'a' },
eb = { b: 'b' };
events.set('*', [star]);
inst.emit('foo', ea);
expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);
star.resetHistory();
inst.emit('bar', eb);
expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);
});
});
});