forked from notenoughneon/typed-promisify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
97 lines (82 loc) · 2.37 KB
/
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
import * as assert from 'assert';
import {promisify, map} from './index';
describe('promisify', function() {
function f(cb) {
cb(null, 'f');
}
function fe(cb) {
cb('error', null);
}
function fa(a, cb) {
cb(null, 'f' + a);
}
function fae(a, cb) {
cb('error', null);
}
let o = {
v: 'value',
f(cb) {
cb(null, this.v);
}
};
it('function with no args', function() {
return promisify(f)()
.then(res => {
assert.equal(res, 'f');
});
});
it('function with no args throws exception', function() {
return promisify(fe)()
.then(res => {
throw new Error('expected an exception');
})
.catch(err => {
assert.equal(err, 'error');
});
});
it('function with one arg', function() {
return promisify(fa)('a')
.then(res => {
assert.equal(res, 'fa');
});
});
it('function with one arg throws exception', function() {
return promisify(fae)('a')
.then(res => {
throw new Error('expected an exception');
})
.catch(err => {
assert.equal(err, 'error');
});
});
it('function with this', function () {
return promisify(o.f, o)()
.then(res => {
assert.equal(res, 'value');
});
});
});
describe('map', function() {
var elts = [1, 2, 3];
var f: (n) => Promise<number> = n => new Promise((res,rej) => res(n * n));
var expected = [1, 4, 9];
it('array of values', function() {
return map(elts, f)
.then(res => assert.deepEqual(res, expected));
});
it('array of promises', function() {
var eltps = elts.map(elt => new Promise((res, rej) => res(elt)));
return map(eltps, f)
.then(res => assert.deepEqual(res, expected));
});
it('promise of array of values', function() {
var pelts = new Promise((res,rej) => res(elts));
return map(pelts, f)
.then(res => assert.deepEqual(res, expected));
});
it('promise of array of promises', function() {
var peltps = new Promise((res,rej) => res(elts.map(elt => new Promise((res, rej) => res(elt)))));
return map(peltps, f)
.then(res => assert.deepEqual(res, expected));
});
});