From 4c6e0ed5f3725408f560c7dbadbbffdaec607eee Mon Sep 17 00:00:00 2001 From: Jeffrey Zhao Date: Tue, 31 Jul 2012 23:32:54 +0800 Subject: [PATCH] Unit tests for fromCallback --- tests/jscex-async-powerpack/tests.js | 48 +++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/jscex-async-powerpack/tests.js b/tests/jscex-async-powerpack/tests.js index d22b2b3..1699acb 100644 --- a/tests/jscex-async-powerpack/tests.js +++ b/tests/jscex-async-powerpack/tests.js @@ -229,4 +229,50 @@ exports.setupTests = function (Jscex) { }); }); }); -} \ No newline at end of file + + describe("Binding", function () { + + var test = function (timeout, args, callback) { + if (timeout < 0) { + callback.apply(this, args); + } else { + var _this = this; + setTimeout(function () { + callback.apply(_this, args); + }, timeout); + } + } + + var Binding = Jscex.Async.Binding; + + describe("fromCallback", function () { + + it("should return the only result when the callback is called directly", function () { + var testAsync = Binding.fromCallback(test); + testAsync(-1, [10]).start().result.should.equal(10); + }); + + it("should return the only result when the callback is called asynchronously", function (done) { + var testAsync = Binding.fromCallback(test); + testAsync(1, [10]).start().addEventListener("success", function () { + this.result.should.equal(10); + done(); + }); + }); + + it("should return the first result when the callback is called directly with multiple arguments", function () { + var testAsync = Binding.fromCallback(test); + testAsync(-1, [10, 20]).start().result.should.equal(10); + }); + + it("should return the first result when the callback is called asynchronously with moltiple arguments", function (done) { + var testAsync = Binding.fromCallback(test); + testAsync(1, [10, 20]).start().addEventListener("success", function () { + this.result.should.equal(10); + done(); + }); + }); + }); + + }); +}