-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
218 lines (215 loc) · 7.78 KB
/
test.js
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
213
214
215
216
217
218
// Grab assert and the evee module
import assert from 'assert'
import Evee, { evee } from './dist/esm/index.js'
import EveeMin, { evee as eveeMin } from './dist/esm/index.min.js'
// Run tests with evee
run_tests(() => new Evee(), "evee (node, new instance)");
run_tests(() => {evee.clear(); return evee}, "evee (node, shared instance)");
// Run tests with minified evee
run_tests(() => new EveeMin(), "evee (node, minified, new instance)");
run_tests(() => {eveeMin.clear(); return eveeMin}, "evee (node, minified, shared instance)");
function run_tests(makeInstance, name) {
describe(name, () => {
describe('#on(name, action)', () => {
it('should dispatch properly', () => {
const evee = makeInstance();
var res = false;
evee.on('a', _ => res = true);
evee.signal('a');
assert.equal(true, res);
});
it('should increment the id when subscribing', () => {
const evee = makeInstance();
assert.equal(0, evee.on('a', _ => undefined).id);
assert.equal(1, evee.on('b', _ => undefined).id);
assert.equal(2, evee.on('c', _ => undefined).id);
});
it('should throw when the name is not a string', () => {
const evee = makeInstance();
assert.throws(() => evee.on(undefined, () => undefined));
});
it('should throw when the action is not a function', () => {
const evee = makeInstance();
assert.throws(() => evee.on('a', undefined));
});
it('should throw when called without arguments', () => {
const evee = makeInstance();
assert.throws(() => evee.on());
});
});
describe('#once(name, action)', () => {
it('should only dispatch once', () => {
const evee = makeInstance();
var res = false;
evee.once('a', _ => res = !res);
evee.signal('a', 'a');
assert.equal(true, res);
});
it('should increment the id when subscribing', () => {
const evee = makeInstance();
assert.equal(0, evee.once('a', _ => undefined).id);
assert.equal(1, evee.once('b', _ => undefined).id);
assert.equal(2, evee.once('c', _ => undefined).id);
});
it('should throw when the name is not a string', () => {
const evee = makeInstance();
assert.throws(() => evee.once(undefined, () => undefined));
});
it('should throw when the action is not a function', () => {
const evee = makeInstance();
assert.throws(() => evee.once('a', undefined));
});
});
describe('#drop(event)', () => {
it('should return true when subscriptions were removed', () => {
const evee = makeInstance();
var ev = evee.on('a', _ => undefined);
assert.equal(true, evee.drop(ev));
});
it('should throw when the argument is not an event object', () => {
const evee = makeInstance();
assert.throws(() => evee.drop(0));
});
it('should throw when called without arguments', () => {
const evee = makeInstance();
assert.throws(() => evee.drop());
});
it('should remove the right event', () => {
const evee = makeInstance();
var resa = false;
var resb = false;
var eva = evee.on('a', _ => resa = true);
var evb = evee.on('b', _ => resb = true);
evee.drop(evb);
evee.emit(['a', 'b']);
assert.equal(true, resa);
assert.equal(false, resb);
});
});
describe('#emit(name, data)', () => {
it('should transmit the correct event data when dispatching', () => {
const evee = makeInstance();
var res = 0;
evee.on('a', e => res += e.data);
evee.on('b', e => res += e.data);
evee.emit('a', 11);
evee.emit('b', 11);
assert.equal(22, res);
});
it('should transmit the correct sender when dispatching', () => {
const evee = makeInstance();
var res = false;
var sender = evee.on('a', e => res = e.sender === sender);
evee.emit('a');
assert.equal(true, res);
});
it('should not dispatch when the event name matches no receivers', () => {
const evee = makeInstance();
var res = false;
evee.on('a', _ => res = true);
evee.emit('b');
assert.equal(false, res);
});
it('should dispatch when the event name matches a receiver', () => {
const evee = makeInstance();
var res = false;
evee.on('a', _ => res = true);
evee.emit('a');
assert.equal(true, res);
});
it('should dispatch when the event name matches multiple receivers', () => {
const evee = makeInstance();
var res = 0;
evee.on('a', _ => ++res);
evee.on('a', _ => ++res);
evee.emit('a');
assert.equal(2, res);
});
it('should dispatch events with data', () => {
const evee = makeInstance();
var res = false;
evee.on('a', e => res = e.data);
evee.emit('a', true);
assert.equal(true, res);
});
it('should dispatch events without data', () => {
const evee = makeInstance();
var res = false;
evee.on('a', e => res = e.data === undefined ? true : false);
evee.emit('a');
assert.equal(true, res);
});
it('should dispatch multiple events with the same data', () => {
const evee = makeInstance();
var resa = false;
var resb = false;
evee.on('a', e => resa = e.data);
evee.on('b', e => resb = e.data);
evee.emit(['a', 'b'], true);
assert.equal(true, resa);
assert.equal(true, resb);
});
it('should dispatch multiple events with no data', () => {
const evee = makeInstance();
var resa = true;
var resb = false;
evee.on('a', e => resa = e.data === undefined ? resa : !resa);
evee.on('b', e => resb = e.data === undefined ? !resb : resb);
evee.emit(['a', 'b']);
assert.equal(true, resa);
assert.equal(true, resb);
});
it('should dispatch multiple events with different data', () => {
const evee = makeInstance();
var resa = false;
var resb = true;
evee.on('a', e => resa = e.data);
evee.on('b', e => resb = e.data);
evee.emit([{name: 'a', data: true}, {name: 'b', data: false}]);
assert.equal(true, resa);
assert.equal(false, resb);
});
it('should throw when called without arguments', () => {
const evee = makeInstance();
assert.throws(() => evee.emit());
});
});
describe('#signal(...names)', () => {
it('should correctly dispatch multiple events a single time', () => {
const evee = makeInstance();
var resa = false;
var resb = false;
evee.on('a', _ => resa = true);
evee.on('b', _ => resb = true);
evee.signal('a', 'b');
assert.equal(true, resa);
assert.equal(true, resb);
});
it('should correctly dispatch the same event multiple multiple', () => {
const evee = makeInstance();
var res = false;
evee.on('a', _ => res = !res);
evee.signal('a', 'a', 'a');
assert.equal(true, res);
});
it('should throw when called without arguments', () => {
const evee = makeInstance();
assert.throws(() => evee.signal());
});
});
describe('#clear()', () => {
it('should clear the receiver list', () => {
const evee = makeInstance();
var res = false;
evee.on('a', _ => res = true);
evee.clear();
evee.signal('a');
assert.equal(false, res);
});
it('should not throw when no subscriptions are active', () => {
const evee = makeInstance();
assert.doesNotThrow(() => evee.clear());
});
});
});
}