| 1 | var Q = require('../kew') |
| 2 | var originalQ = require('q') |
| 3 | |
| 4 | // create a promise from a literal |
| 5 | exports.testQResolve = function (test) { |
| 6 | var val = "ok" |
| 7 | |
| 8 | Q.resolve(val) |
| 9 | .then(function (data) { |
| 10 | test.equal(data, val, "Promise successfully returned") |
| 11 | test.done() |
| 12 | }) |
| 13 | } |
| 14 | |
| 15 | // create a failed promise from an error literal |
| 16 | exports.testQReject = function (test) { |
| 17 | var err = new Error("hello") |
| 18 | |
| 19 | Q.reject(err) |
| 20 | .fail(function (e) { |
| 21 | test.equal(e, err, "Promise successfully failed") |
| 22 | test.done() |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | // Test Q.stats |
| 27 | exports.testQStatistics = function (test) { |
| 28 | var err = new Error("hello") |
| 29 | |
| 30 | var errorsEmitted = Q.stats.errorsEmitted |
| 31 | var errorsHandled = Q.stats.errorsHandled |
| 32 | |
| 33 | var rejected = Q.reject(err) |
| 34 | test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted") |
| 35 | test.equal(errorsHandled, Q.stats.errorsHandled, "Error hasn't been handled yet") |
| 36 | |
| 37 | rejected.fail(function (e) { |
| 38 | test.equal(e, err, "Promise successfully failed") |
| 39 | test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted") |
| 40 | test.equal(errorsHandled + 1, Q.stats.errorsHandled, "One additional error handled") |
| 41 | }) |
| 42 | |
| 43 | rejected.fail(function (e) { |
| 44 | test.equal(e, err, "Promise successfully failed") |
| 45 | test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted") |
| 46 | test.equal(errorsHandled + 1, Q.stats.errorsHandled, "Only count error handling once") |
| 47 | }) |
| 48 | test.done() |
| 49 | } |
| 50 | |
| 51 | exports.testQDeferredStatistics = function (test) { |
| 52 | var err = new Error("hello") |
| 53 | |
| 54 | var errorsEmitted = Q.stats.errorsEmitted |
| 55 | var errorsHandled = Q.stats.errorsHandled |
| 56 | |
| 57 | var deferred = Q.defer() |
| 58 | |
| 59 | deferred.fail(function (e) { |
| 60 | test.equal(e, err, "Promise successfully failed") |
| 61 | test.equal(errorsEmitted + 1, Q.stats.errorsEmitted, "One additional error emitted") |
| 62 | test.equal(errorsHandled + 1, Q.stats.errorsHandled, "One additional error handled") |
| 63 | test.done() |
| 64 | }) |
| 65 | |
| 66 | var rejected = deferred.reject(err) |
| 67 | |
| 68 | } |
| 69 | |
| 70 | // test Q.all with an empty array |
| 71 | exports.testQEmptySuccess = function (test) { |
| 72 | var promises = [] |
| 73 | |
| 74 | // make sure all results come back |
| 75 | Q.all(promises) |
| 76 | .then(function (data) { |
| 77 | test.equal(data.length, 0, "No records should be returned") |
| 78 | test.done() |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | // test Q.all with only literals |
| 83 | exports.testQAllLiteralsSuccess = function (test) { |
| 84 | var vals = [3, 2, 1] |
| 85 | var promises = [] |
| 86 | |
| 87 | promises.push(vals[0]) |
| 88 | promises.push(vals[1]) |
| 89 | promises.push(vals[2]) |
| 90 | |
| 91 | // make sure all results come back |
| 92 | Q.all(promises) |
| 93 | .then(function (data) { |
| 94 | test.equal(data[0], vals[0], "First val should be returned") |
| 95 | test.equal(data[1], vals[1], "Second val should be returned") |
| 96 | test.equal(data[2], vals[2], "Third val should be returned") |
| 97 | test.done() |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | // test Q.all with only promises |
| 102 | exports.testQAllPromisesSuccess = function (test) { |
| 103 | var vals = [3, 2, 1] |
| 104 | var promises = [] |
| 105 | |
| 106 | promises.push(Q.resolve(vals[0])) |
| 107 | promises.push(Q.resolve(vals[1])) |
| 108 | promises.push(Q.resolve(vals[2])) |
| 109 | |
| 110 | // make sure all results come back |
| 111 | Q.all(promises) |
| 112 | .then(function (data) { |
| 113 | test.equal(data[0], vals[0], "First val should be returned") |
| 114 | test.equal(data[1], vals[1], "Second val should be returned") |
| 115 | test.equal(data[2], vals[2], "Third val should be returned") |
| 116 | test.done() |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | // create a promise which waits for other promises |
| 121 | exports.testQAllAssortedSuccess = function (test) { |
| 122 | var vals = [3, 2, 1] |
| 123 | var promises = [] |
| 124 | |
| 125 | // a promise that returns the value immediately |
| 126 | promises.push(Q.resolve(vals[0])) |
| 127 | |
| 128 | // the value itself |
| 129 | promises.push(vals[1]) |
| 130 | |
| 131 | // a promise which returns in 10ms |
| 132 | var defer = Q.defer() |
| 133 | promises.push(defer.promise) |
| 134 | setTimeout(function () { |
| 135 | defer.resolve(vals[2]) |
| 136 | }, 10) |
| 137 | |
| 138 | // make sure all results come back |
| 139 | Q.all(promises) |
| 140 | .then(function (data) { |
| 141 | test.equal(data[0], vals[0], "First val should be returned") |
| 142 | test.equal(data[1], vals[1], "Second val should be returned") |
| 143 | test.equal(data[2], vals[2], "Third val should be returned") |
| 144 | test.done() |
| 145 | }) |
| 146 | } |
| 147 | |
| 148 | // test Q.all with a failing promise |
| 149 | exports.testQAllError = function (test) { |
| 150 | var vals = [3, 2, 1] |
| 151 | var err = new Error("hello") |
| 152 | var promises = [] |
| 153 | |
| 154 | promises.push(vals[0]) |
| 155 | promises.push(vals[1]) |
| 156 | |
| 157 | var defer = Q.defer() |
| 158 | promises.push(defer.promise) |
| 159 | defer.reject(err) |
| 160 | |
| 161 | // make sure all results come back |
| 162 | Q.all(promises) |
| 163 | .fail(function (e) { |
| 164 | test.equal(e, err) |
| 165 | test.done() |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | // test all var_args |
| 170 | exports.testAllVarArgs = function (test) { |
| 171 | var promises = ['a', 'b'] |
| 172 | |
| 173 | Q.all.apply(Q, promises) |
| 174 | .then(function (results) { |
| 175 | test.equal(promises[0], results[0], "First element should be returned") |
| 176 | test.equal(promises[1], results[1], "Second element should be returned") |
| 177 | test.done() |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | // test all array |
| 182 | exports.testAllArray = function (test) { |
| 183 | var promises = ['a', 'b'] |
| 184 | |
| 185 | Q.all(promises) |
| 186 | .then(function (results) { |
| 187 | test.equal(promises[0], results[0], "First element should be returned") |
| 188 | test.equal(promises[1], results[1], "Second element should be returned") |
| 189 | test.done() |
| 190 | }) |
| 191 | } |
| 192 | |
| 193 | exports.testAllIsPromiseLike = function(test) { |
| 194 | var promises = ['a', originalQ('b')] |
| 195 | |
| 196 | Q.all(promises) |
| 197 | .then(function (results) { |
| 198 | test.equal(promises[0], 'a', "First element should be returned") |
| 199 | test.equal(promises[1], 'b', "Second element should be returned") |
| 200 | test.done() |
| 201 | }) |
| 202 | } |
| 203 | |
| 204 | // test delay |
| 205 | exports.testDelay = function (test) { |
| 206 | var val = "Hello, there" |
| 207 | var startTime = Date.now() |
| 208 | |
| 209 | Q.resolve(val) |
| 210 | .then(function (v) { |
| 211 | return Q.delay(v, 1000) |
| 212 | }) |
| 213 | .then(function (returnVal) { |
| 214 | test.equal(returnVal, val, "Val should be passed through") |
| 215 | |
| 216 | var diff = Date.now() - startTime |
| 217 | |
| 218 | // clock granularity may be off by 15 |
| 219 | test.equal(diff >= 1000 - 15, true, "Should have waited a second. Actually waited " + diff) |
| 220 | test.done() |
| 221 | }) |
| 222 | } |
| 223 | |
| 224 | // test fcall |
| 225 | exports.testFcall = function (test) { |
| 226 | var calledYet = false |
| 227 | var adder = function (a, b) { |
| 228 | calledYet = true |
| 229 | return a + b |
| 230 | } |
| 231 | |
| 232 | Q.fcall(adder, 2, 3) |
| 233 | .then(function (val) { |
| 234 | test.equal(val, 5, "Val should be 2 + 3") |
| 235 | test.done() |
| 236 | }) |
| 237 | test.ok(!calledYet, "fcall() should delay function invocation until next tick") |
| 238 | } |
| 239 | |
| 240 | // test fcall |
| 241 | exports.testFcallError = function (test) { |
| 242 | var error = function () { |
| 243 | throw new Error('my error') |
| 244 | } |
| 245 | |
| 246 | Q.fcall(error) |
| 247 | .then(function (val) { |
| 248 | test.fail('fcall should throw exception') |
| 249 | }, function (err) { |
| 250 | test.equal('my error', err.message) |
| 251 | }) |
| 252 | .then(function () { |
| 253 | test.done() |
| 254 | }) |
| 255 | } |
| 256 | |
| 257 | // test fcall works when fn returns a promise |
| 258 | exports.testFcallGivenPromise = function (test) { |
| 259 | var calledYet = false |
| 260 | var eventualAdd = function (a, b) { |
| 261 | calledYet = true |
| 262 | return Q.resolve(a + b) |
| 263 | } |
| 264 | |
| 265 | Q.fcall(eventualAdd, 2, 3) |
| 266 | .then(function (val) { |
| 267 | test.equal(val, 5, "Val should be 2 + 3") |
| 268 | test.done() |
| 269 | }) |
| 270 | test.ok(!calledYet, "fcall() should delay function invocation until next tick") |
| 271 | } |
| 272 | |
| 273 | // test nfcall, successful case |
| 274 | exports.testNfcall = function (test) { |
| 275 | var nodeStyleEventualAdder = function (a, b, callback) { |
| 276 | setTimeout(function () { |
| 277 | callback(undefined, a + b) |
| 278 | }, 2) |
| 279 | } |
| 280 | |
| 281 | Q.nfcall(nodeStyleEventualAdder, 2, 3) |
| 282 | .then(function (val) { |
| 283 | test.equal(val, 5, "Val should be 2 + 3") |
| 284 | test.done() |
| 285 | }) |
| 286 | } |
| 287 | |
| 288 | // test nfcall, error case |
| 289 | exports.testNfcallErrors = function (test) { |
| 290 | var err = new Error('fail') |
| 291 | |
| 292 | var nodeStyleFailer = function (a, b, callback) { |
| 293 | setTimeout(function() { |
| 294 | callback(err) |
| 295 | }, 2) |
| 296 | } |
| 297 | |
| 298 | Q.nfcall(nodeStyleFailer, 2, 3) |
| 299 | .fail(function (e) { |
| 300 | test.equal(e, err, "Promise successfully failed") |
| 301 | test.done() |
| 302 | }) |
| 303 | } |
| 304 | |
| 305 | // test fcall |
| 306 | exports.testNFcallErrorSync = function (test) { |
| 307 | var error = function () { |
| 308 | throw new Error('my error') |
| 309 | } |
| 310 | |
| 311 | Q.nfcall(error) |
| 312 | .then(function (val) { |
| 313 | test.fail('nfcall should throw exception') |
| 314 | }, function (err) { |
| 315 | test.equal('my error', err.message) |
| 316 | }) |
| 317 | .then(function () { |
| 318 | test.done() |
| 319 | }) |
| 320 | } |
| 321 | |
| 322 | exports.testNcall = function (test) { |
| 323 | function TwoAdder() { |
| 324 | this.a = 2 |
| 325 | } |
| 326 | TwoAdder.prototype.add = function (num, callback) { |
| 327 | setTimeout(function () { |
| 328 | callback(null, this.a + num) |
| 329 | }.bind(this), 10) |
| 330 | } |
| 331 | var adder = new TwoAdder() |
| 332 | Q.ncall(adder.add, adder, 3) |
| 333 | .then(function (val) { |
| 334 | test.equal(val, 5, "Val should be 2 + 3") |
| 335 | test.done() |
| 336 | }) |
| 337 | } |
| 338 | |
| 339 | // test binding a callback function with a promise |
| 340 | exports.testBindPromise = function (test) { |
| 341 | var adder = function (a, b, callback) { |
| 342 | callback(null, a + b) |
| 343 | } |
| 344 | |
| 345 | var boundAdder = Q.bindPromise(adder, null, 2) |
| 346 | boundAdder(3) |
| 347 | .then(function (val) { |
| 348 | test.equal(val, 5, "Val should be 2 + 3") |
| 349 | test.done() |
| 350 | }) |
| 351 | } |
| 352 | |
| 353 | // test checking whether something is a promise |
| 354 | exports.testIsPromise = function (test) { |
| 355 | var kewPromise = Q.defer() |
| 356 | var qPromise = originalQ(10) |
| 357 | var kewLikeObject = { |
| 358 | promise: function () { |
| 359 | return 'not a promise sucka!' |
| 360 | }, |
| 361 | then: function (fn) { |
| 362 | fn('like a promise, brah!') |
| 363 | } |
| 364 | } |
| 365 | test.equal(Q.isPromise(kewPromise), true, 'A Kew promise is a promise') |
| 366 | test.equal(Q.isPromise(qPromise), false, 'A Q promise is not a promise') |
| 367 | test.equal(Q.isPromise(kewLikeObject), false, 'A pretend promise is not a promise') |
| 368 | test.done() |
| 369 | } |
| 370 | |
| 371 | // test checking whether something is a promise-like object |
| 372 | exports.testIsPromiseLike = function (test) { |
| 373 | var kewPromise = Q.defer() |
| 374 | var qPromise = originalQ(10) |
| 375 | var kewLikeObject = { |
| 376 | promise: function () { |
| 377 | return 'not a promise sucka!' |
| 378 | }, |
| 379 | then: function (fn) { |
| 380 | fn('like a promise, brah!') |
| 381 | } |
| 382 | } |
| 383 | var kewLikeFunction = function() {} |
| 384 | kewLikeFunction.then = function(fn) { |
| 385 | fn('like a promise, brah!') |
| 386 | } |
| 387 | test.equal(Q.isPromiseLike(kewPromise), true, 'A Kew promise is promise-like') |
| 388 | test.equal(Q.isPromiseLike(qPromise), true, 'A Q promise is promise-like') |
| 389 | test.equal(Q.isPromiseLike(kewLikeObject), true, 'A pretend promise is a promise-like') |
| 390 | test.equal(Q.isPromiseLike(kewLikeFunction), true, 'A pretend function promise is a promise-like') |
| 391 | |
| 392 | test.done() |
| 393 | } |