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
|
import mitt from '../src';
import chai, { expect } from 'chai';
import { spy } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
it('should default export be a function', () => {
expect(mitt).to.be.a('function');
});
describe('mitt#', () => {
let events, inst;
beforeEach( () => {
events = Object.create(null);
inst = mitt(events);
});
describe('on()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('on')
.that.is.a('function');
});
it('should register handler for new type', () => {
let foo = () => {};
inst.on('foo', foo);
expect(events).to.have.property('foo').that.deep.equals([foo]);
});
it('should register handlers for any type strings', () => {
let foo = () => {};
inst.on('constructor', foo);
expect(events).to.have.property('constructor').that.deep.equals([foo]);
});
it('should append handler for existing type', () => {
let foo = () => {};
let bar = () => {};
inst.on('foo', foo);
inst.on('foo', bar);
expect(events).to.have.property('foo').that.deep.equals([foo, bar]);
});
it('should NOT normalize case', () => {
let foo = () => {};
inst.on('FOO', foo);
inst.on('Bar', foo);
inst.on('baz:baT!', foo);
expect(events).to.have.property('FOO').that.deep.equals([foo]);
expect(events).to.not.have.property('foo');
expect(events).to.have.property('Bar').that.deep.equals([foo]);
expect(events).to.not.have.property('bar');
expect(events).to.have.property('baz:baT!').that.deep.equals([foo]);
});
});
describe('off()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('off')
.that.is.a('function');
});
it('should remove handler for type', () => {
let foo = () => {};
inst.on('foo', foo);
inst.off('foo', foo);
expect(events).to.have.property('foo').that.is.empty;
});
it('should NOT normalize case', () => {
let 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).to.have.property('FOO').that.is.empty;
expect(events).to.not.have.property('foo');
expect(events).to.have.property('Bar').that.is.empty;
expect(events).to.not.have.property('bar');
expect(events).to.have.property('baz:bat!').with.length(1);
});
});
describe('emit()', () => {
it('should be a function', () => {
expect(inst)
.to.have.property('emit')
.that.is.a('function');
});
it('should invoke handler for type', () => {
let event = { a: 'b' };
inst.on('foo', (one, two) => {
expect(one).to.deep.equal(event);
expect(two).to.be.an('undefined');
});
inst.emit('foo', event);
});
it('should NOT ignore case', () => {
let onFoo = spy(),
onFOO = spy();
events.Foo = [onFoo];
events.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', () => {
let star = spy(),
ea = { a: 'a' },
eb = { b: 'b' };
events['*'] = [star];
inst.emit('foo', ea);
expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);
star.reset();
inst.emit('bar', eb);
expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);
});
});
});
|