Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@protobufjs/aspromise/tests/index.js
1129 views
1
var tape = require("tape");
2
3
var asPromise = require("..");
4
5
tape.test("aspromise", function(test) {
6
7
test.test(this.name + " - resolve", function(test) {
8
9
function fn(arg1, arg2, callback) {
10
test.equal(this, ctx, "function should be called with this = ctx");
11
test.equal(arg1, 1, "function should be called with arg1 = 1");
12
test.equal(arg2, 2, "function should be called with arg2 = 2");
13
callback(null, arg2);
14
}
15
16
var ctx = {};
17
18
var promise = asPromise(fn, ctx, 1, 2);
19
promise.then(function(arg2) {
20
test.equal(arg2, 2, "promise should be resolved with arg2 = 2");
21
test.end();
22
}).catch(function(err) {
23
test.fail("promise should not be rejected (" + err + ")");
24
});
25
});
26
27
test.test(this.name + " - reject", function(test) {
28
29
function fn(arg1, arg2, callback) {
30
test.equal(this, ctx, "function should be called with this = ctx");
31
test.equal(arg1, 1, "function should be called with arg1 = 1");
32
test.equal(arg2, 2, "function should be called with arg2 = 2");
33
callback(arg1);
34
}
35
36
var ctx = {};
37
38
var promise = asPromise(fn, ctx, 1, 2);
39
promise.then(function() {
40
test.fail("promise should not be resolved");
41
}).catch(function(err) {
42
test.equal(err, 1, "promise should be rejected with err = 1");
43
test.end();
44
});
45
});
46
47
test.test(this.name + " - resolve twice", function(test) {
48
49
function fn(arg1, arg2, callback) {
50
test.equal(this, ctx, "function should be called with this = ctx");
51
test.equal(arg1, 1, "function should be called with arg1 = 1");
52
test.equal(arg2, 2, "function should be called with arg2 = 2");
53
callback(null, arg2);
54
callback(null, arg1);
55
}
56
57
var ctx = {};
58
var count = 0;
59
60
var promise = asPromise(fn, ctx, 1, 2);
61
promise.then(function(arg2) {
62
test.equal(arg2, 2, "promise should be resolved with arg2 = 2");
63
if (++count > 1)
64
test.fail("promise should not be resolved twice");
65
test.end();
66
}).catch(function(err) {
67
test.fail("promise should not be rejected (" + err + ")");
68
});
69
});
70
71
test.test(this.name + " - reject twice", function(test) {
72
73
function fn(arg1, arg2, callback) {
74
test.equal(this, ctx, "function should be called with this = ctx");
75
test.equal(arg1, 1, "function should be called with arg1 = 1");
76
test.equal(arg2, 2, "function should be called with arg2 = 2");
77
callback(arg1);
78
callback(arg2);
79
}
80
81
var ctx = {};
82
var count = 0;
83
84
var promise = asPromise(fn, ctx, 1, 2);
85
promise.then(function() {
86
test.fail("promise should not be resolved");
87
}).catch(function(err) {
88
test.equal(err, 1, "promise should be rejected with err = 1");
89
if (++count > 1)
90
test.fail("promise should not be rejected twice");
91
test.end();
92
});
93
});
94
95
test.test(this.name + " - reject error", function(test) {
96
97
function fn(callback) {
98
test.ok(arguments.length === 1 && typeof callback === "function", "function should be called with just a callback");
99
throw 3;
100
}
101
102
var promise = asPromise(fn, null);
103
promise.then(function() {
104
test.fail("promise should not be resolved");
105
}).catch(function(err) {
106
test.equal(err, 3, "promise should be rejected with err = 3");
107
test.end();
108
});
109
});
110
111
test.test(this.name + " - reject and error", function(test) {
112
113
function fn(callback) {
114
callback(3);
115
throw 4;
116
}
117
118
var count = 0;
119
120
var promise = asPromise(fn, null);
121
promise.then(function() {
122
test.fail("promise should not be resolved");
123
}).catch(function(err) {
124
test.equal(err, 3, "promise should be rejected with err = 3");
125
if (++count > 1)
126
test.fail("promise should not be rejected twice");
127
test.end();
128
});
129
});
130
});
131
132