-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
138 lines (120 loc) · 3.48 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
'use strict';
/*global describe:false, it:false*/
const promisifyEvent = require('./index.js')();
const EventEmitter = require('events').EventEmitter;
const assert = require('assert');
function simpleEventEmitter() {
const ev = new EventEmitter();
setTimeout(() => {
ev.emit('end', 'value');
}, 10);
return ev;
}
function dataEventEmitter() {
const ev = new EventEmitter();
setTimeout(() => { ev.emit('data', 0); }, 0);
setTimeout(() => { ev.emit('data', 1); }, 10);
setTimeout(() => { ev.emit('data', 2); }, 20);
setTimeout(() => { ev.emit('data', 3); }, 30);
setTimeout(() => { ev.emit('data', 4); }, 40);
setTimeout(() => { ev.emit('end', 'done'); }, 50);
return ev;
}
function errorEmitter() {
const ev = new EventEmitter();
setTimeout(() => ev.emit('error', new Error()), 10);
setTimeout(() => ev.emit('end'), 20);
return ev;
}
function errorEmitterCustom() {
const ev = new EventEmitter();
setTimeout(() => { ev.emit('error2', {boop: true}); }, 10);
return ev;
}
describe('export', () => {
it('Correctly makes the function use the given promise implementation', () => {
const MyPromiseImp = class MyPromiseImp extends Promise {
};
const otherPromisifyEvent = require('./index.js')(MyPromiseImp);
assert(
otherPromisifyEvent(simpleEventEmitter(), 'end') instanceof MyPromiseImp,
'Wrong promise implemetation'
);
});
it('Defaults to the native promise implementation', () => {
assert(
promisifyEvent(simpleEventEmitter(), 'end') instanceof Promise,
'Wrong promise implementation'
);
});
});
describe('promisifyEvent', () => {
it('Functions with the second argument being a string', () =>
promisifyEvent(simpleEventEmitter(), 'end')
.then(value => assert.equal(value, 'value'))
);
it('Functions with the second argument being an object', () =>
promisifyEvent(simpleEventEmitter(), {name: 'end'})
);
it('Aggregates data when aggregate is set', () =>
promisifyEvent(dataEventEmitter(), {
name: 'end',
aggregate: 'data'
})
.then(result => {
assert.equal(result.value, 'done');
assert.deepEqual(result.aggregated, [0, 1, 2, 3, 4]);
})
);
it('Only resolves when the filter is set', () =>
promisifyEvent(dataEventEmitter(), {
name: 'data',
filter: data => data === 4
})
.then(result => {
assert.equal(result, 4);
})
);
it('Rejects when error is emitted', () =>
promisifyEvent(errorEmitter(), {name: 'end'})
.then(
() => assert(false, 'Should not resolve'),
error => assert(error instanceof Error, 'Should be instanceof error')
)
);
it('Rejects when the custom error is emitted', () =>
promisifyEvent(dataEventEmitter(), {
name: 'end',
errorName: 'data'
})
.then(
() => assert(false, 'Should not resolve'),
() => { /* do nothing */ }
)
);
it('Wraps a error string into an error object with the string being the message', () =>
promisifyEvent(dataEventEmitter(), {
errorName: 'end'
})
.then(
() => assert(false, 'Should not resolve'),
error => assert(error.message === 'done', 'Message should be emitted value')
)
);
it('Wraps a non error and non string error object into an error object and adds the event as a property', () =>
promisifyEvent(errorEmitterCustom(), {
name: 'end',
errorName: 'error2'
})
.then(
() => assert(false, 'Should not resolve'),
error => assert(error.event.boop, 'Object should be there')
)
);
it('Ignores the error event when ignoreErrors is set', () =>
promisifyEvent(errorEmitter(), {
name: 'end',
ignoreErrors: true
})
);
});