diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-10-18 18:03:31 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-10-18 18:03:31 +0100 |
commit | 05b40641883b0502b8b849679f4c583a3e61db2a (patch) | |
tree | 9956f487437a70fcdfcfeefb3475e6a55932f939 /spec | |
parent | 44b3954d56045498adc788ca9517c33b4e56cb54 (diff) | |
download | prosody-05b40641883b0502b8b849679f4c583a3e61db2a.tar.gz prosody-05b40641883b0502b8b849679f4c583a3e61db2a.zip |
util.promise: Add promise.try()
Diffstat (limited to 'spec')
-rw-r--r-- | spec/util_promise_spec.lua | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua index 778a7661..c2a8ba95 100644 --- a/spec/util_promise_spec.lua +++ b/spec/util_promise_spec.lua @@ -331,4 +331,55 @@ describe("util.promise", function () assert.spy(on_finally_err).was_called_with(test_error); end); end); + describe("try()", function () + it("works with functions that return a promise", function () + local resolve; + local p = promise.try(function () + return promise.new(function (_resolve) + resolve = _resolve; + end); + end); + assert.is_function(resolve); + local on_resolved = spy.new(function () end); + p:next(on_resolved); + assert.spy(on_resolved).was_not_called(); + resolve("foo"); + assert.spy(on_resolved).was_called_with("foo"); + end); + + it("works with functions that return a value", function () + local p = promise.try(function () + return "foo"; + end); + local on_resolved = spy.new(function () end); + p:next(on_resolved); + assert.spy(on_resolved).was_called_with("foo"); + end); + + it("works with functions that return a promise that rejects", function () + local reject; + local p = promise.try(function () + return promise.new(function (_, _reject) + reject = _reject; + end); + end); + assert.is_function(reject); + local on_rejected = spy.new(function () end); + p:catch(on_rejected); + assert.spy(on_rejected).was_not_called(); + reject("foo"); + assert.spy(on_rejected).was_called_with("foo"); + end); + + it("works with functions that throw errors", function () + local test_error = {}; + local p = promise.try(function () + error(test_error); + end); + local on_rejected = spy.new(function () end); + p:catch(on_rejected); + assert.spy(on_rejected).was_called(1); + assert.spy(on_rejected).was_called_with(test_error); + end); + end); end); |