forked from kriskowal/q
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.js
117 lines (95 loc) · 2.6 KB
/
bind.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
"use strict";
var Q = require("../q");
exports['test transforms return into fulfill'] = function (ASSERT, done) {
var returnVal = {};
var bound = Q.bind(function () {
return returnVal;
});
var result = bound();
result
.then(function (val) {
ASSERT.strictEqual(val, returnVal, "fulfilled with correct value");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};
exports['test transforms throw into reject'] = function (ASSERT, done) {
var throwMe = new Error("boo!");
var bound = Q.bind(function () {
throw throwMe;
});
var result = bound();
result
.then(function (val) {
ASSERT.ok(false, val);
})
.fail(function (reason) {
ASSERT.strictEqual(reason, throwMe, "rejected with correct reason");
})
.fin(done);
};
exports['test passes through arguments'] = function (ASSERT, done) {
var x = {};
var y = {};
var bound = Q.bind(function (a, b) {
ASSERT.strictEqual(a, x, "first argument correct");
ASSERT.strictEqual(b, y, "second argument correct");
});
bound(x, y)
.then(function () {
ASSERT.ok(true, "fulfilled");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};
exports['test combining bound and free arguments'] = function (ASSERT, done) {
var x = {};
var y = {};
var bound = Q.bind(function (a, b) {
ASSERT.strictEqual(a, x, "first argument correct");
ASSERT.strictEqual(b, y, "second argument correct");
}, null, x);
bound(y)
.then(function () {
ASSERT.ok(true, "fulfilled");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};
exports['test invokes with correct context'] = function (ASSERT, done) {
var context = {};
var bound = Q.bind(function () {
ASSERT.strictEqual(this, context, "context correct");
}, context);
bound()
.then(function () {
ASSERT.ok(true, "fulfilled");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};
exports['test uses existing context if none given'] = function (ASSERT, done) {
var bound = Q.bind(function () {
return this;
});
var expectedContext = (function () { return this; }).call();
bound()
.then(function (context) {
ASSERT.strictEqual(context, expectedContext, "correct context");
})
.fail(function (reason) {
ASSERT.ok(false, reason);
})
.fin(done);
};
if (module == require.main) {
require('test').run(exports);
}