-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocha-promise.js
213 lines (190 loc) · 5.63 KB
/
mocha-promise.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var chai = require('chai');
var expect = chai.expect;
var assert = chai.assert;
var Promise = require('bluebird');
var calculate = function (value) {
return new Promise(function (resolve, reject) {
console.log("in calculate : ", value);
setTimeout(function () {
if (value % 2 == 0) {
resolve(value + 1);
} else {
reject(value + 1);
}
}, 500);
});
};
var calculateModel = function (value, callback) {
calculate(value)
.then(function (result) {
console.log("In model then : ", result);
return callback(null, result);
})
.catch(function (result) {
console.log("In model catch : ", result);
return callback(new Error("hello error!"), result);
})
.done();
};
/**
Test Case 1 : send an Odd number in calculate,
Test Case 2 : send an Even number in calculate,
*/
var chainMethod = function (value, callback) {
calculate(value)
.then(function (result) {
console.log("Then Here!!! ", result);
return calculate(result);
})
.catch(function (result) {
console.log("Catch Here!!! ", result);
// return calculate(result + 1);
throw new Error("chainMethod New error");
})
.then(function (result) {
console.log("Finally!!! then ", result);
return callback(result);
})
.catch(function (result) {
console.log("Finally!!! catch ", result);
return callback(result);
})
.done();
};
var safeMethod = function (value, callback) {
calculateModel(value, function (err, result) {
console.log("[serivce] safeMethod callback!!! ", err, result);
if (err) {
calculateModel(value, function (err, result) {
console.log("[serivce] 2 safeMethod callback!!! ", err, result);
if (err) {
console.log("ERR --> err : ", err);
return callback(new Error("Level two error"));
} else {
console.log("ERROR --> result : ", result);
return callback(null, result);
}
});
// return callback(err);
} else {
calculateModel(value, function (err, result) {
console.log("[serivce] 2 safeMethod callback!!! ", err, result);
if (err) {
console.log("RESULT --> err : ", err);
return callback(new Error("Level two error"));
} else {
console.log("RESULT --> result : ", result);
return callback(null, result);
}
});
// return callback(null, result);
}
});
// .then(function (result) {
// console.log("Then Here!!! ", result);
// return calculate(result);
// }, function (result) {
// console.log("Catch Here!!! ", result);
// return calculate(result + 1);
// })
// .then(function (result) {
// console.log("Finally!!! then ", result);
// return callback(result);
// }, function (result) {
// console.log("Finally!!! catch ", result);
// return callback(result);
// });
};
// Comment one and try one by one both the methods, to understand the working of promise flow.
// safeMethod();
// chainMethod();
describe('Promise Testing', function () {
this.timeout(5000);
describe('all parameters are correct for ', function () {
it('Pre test expect', function (dn) {
// var value = new Error("Hello error");
var value = 5;
// expect(value).to.be.an("object");
expect(value)
.to.be.equal(5);
dn();
// expect(value).to.contain("Hello error");
});
it('Test promise calculate()', function (dn) {
var value = 5;
calculate(value)
.then(function (result) {
console.log("THEN Here : ", result);
expect(result)
.to.be.equal(5);
// dn();
})
.catch(function (err) {
console.log("CATCH Here : ", err);
expect(err)
.to.be.equal(6);
// dn();
})
.then(dn, dn);
});
// it('Test promise calculateModel()', function (dn) {
// var value = 5;
// calculateModel(value)
// .then(function (result) {
// console.log("THEN Here : ", result);
// expect(result)
// .to.be.equal(5);
// // dn();
// })
// .catch(function (err) {
// console.log("CATCH Here : ", err);
// expect(err)
// .to.be.equal(4);
// // dn();
// })
// .then(dn, dn);
// });
// it('#chainMethod()', function (dn) {
// var value = 1;
// chainMethod(value, function (result) {
// console.log("1 In IT block : ", result);
// // expect(result).to.be.null;
// expect(result)
// .to.be.number;
// expect(result)
// .to.be.equal(4);
// dn();
// });
// });
it('#safeMethod() ', function (dn) {
var value = 1;
safeMethod(value, function (err, result) {
// try {
console.log("2 In IT block : ", err, result);
console.log("2 In IT block : ", typeof (result));
expect(err)
.to.be.object;
// expect(result).to.be.equal(5);
// expect(err)
// .to.be.an('object');
// expect(result).to.throw(Error);
// expect(result).to.be.
dn();
// } catch (err) {
// console.log("2 Catch In IT block : ", err);
// dn(err);
// }
});
// .then(dn, dn);
});
it('Post test expect', function (dn) {
// var value = new Error("Hello error");
var value = 5;
// expect(value).to.be.an("object");
expect(value)
.to.be.equal(5);
dn();
// expect(value).to.contain("Hello error");
});
});
});