aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2018-10-26 09:23:00 +0100
committerMatthew Wild <mwild1@gmail.com>2018-10-26 09:23:00 +0100
commiteb378bd2bcf69ffc013c5294b6ab95caea1a92ef (patch)
tree94a0148c4a64b1475ae209a1c6044fdd44f76d5e /spec
parent94f7f0b95ed1fd420613b9bd47a71e722f773459 (diff)
downloadprosody-eb378bd2bcf69ffc013c5294b6ab95caea1a92ef.tar.gz
prosody-eb378bd2bcf69ffc013c5294b6ab95caea1a92ef.zip
util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
Diffstat (limited to 'spec')
-rw-r--r--spec/util_promise_spec.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua
index 586f1fe9..65d252f6 100644
--- a/spec/util_promise_spec.lua
+++ b/spec/util_promise_spec.lua
@@ -201,6 +201,55 @@ describe("util.promise", function ()
assert.spy(cb2_err).was_called_with("foo");
end);
+ describe("allows callbacks to return", function ()
+ it("pending promises", function ()
+ local r;
+ local p = promise.resolve()
+ local cb = spy.new(function ()
+ return promise.new(function (resolve)
+ r = resolve;
+ end);
+ end);
+ local cb2 = spy.new(function () end);
+ p:next(cb):next(cb2);
+ assert.spy(cb).was_called(1);
+ assert.spy(cb2).was_called(0);
+ r("hello");
+ assert.spy(cb).was_called(1);
+ assert.spy(cb2).was_called(1);
+ assert.spy(cb2).was_called_with("hello");
+ end);
+
+ it("resolved promises", function ()
+ local p = promise.resolve()
+ local cb = spy.new(function ()
+ return promise.resolve("hello");
+ end);
+ local cb2 = spy.new(function () end);
+ p:next(cb):next(cb2);
+ assert.spy(cb).was_called(1);
+ assert.spy(cb2).was_called(1);
+ assert.spy(cb2).was_called_with("hello");
+ end);
+
+ it("rejected promises", function ()
+ local p = promise.resolve()
+ local cb = spy.new(function ()
+ return promise.reject("hello");
+ end);
+ local cb2 = spy.new(function ()
+ return promise.reject("goodbye");
+ end);
+ local cb3 = spy.new(function () end);
+ p:next(cb):catch(cb2):catch(cb3);
+ assert.spy(cb).was_called(1);
+ assert.spy(cb2).was_called(1);
+ assert.spy(cb2).was_called_with("hello");
+ assert.spy(cb3).was_called(1);
+ assert.spy(cb3).was_called_with("goodbye");
+ end);
+ end);
+
describe("race()", function ()
it("works with fulfilled promises", function ()
local p1, p2 = promise.resolve("yep"), promise.resolve("nope");