-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
iterator.js
43 lines (39 loc) · 979 Bytes
/
iterator.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
import test from 'ava';
let Emittery;
try {
Emittery = require('../iterator');
} catch (err) {
test('does not work due to syntax errors', t => {
t.is(err.name, 'SyntaxError');
});
}
if (Emittery) {
require('./_run')(Emittery);
/* eslint-disable ava/no-async-fn-without-await */
test('on() - async iterator (for await)', async t => {
t.plan(3);
const fixture = '🌈';
const emitter = new Emittery();
setInterval(() => {
emitter.emit('🦄', fixture);
}, 50);
let count = 0;
for await (const data of emitter.on('🦄')) {
count++;
if (count >= 3) {
break;
}
t.deepEqual(data, fixture);
}
});
/* eslint-enable ava/no-async-fn-without-await */
test('on() - async iterator', async t => {
const fixture = '🌈';
const emitter = new Emittery();
const iterator = emitter.on('🦄');
emitter.emit('🦄', fixture);
const {value, done} = await iterator.next();
t.deepEqual(done, false);
t.deepEqual(value, fixture);
});
}