diff options
Diffstat (limited to 'spec/util_promise_spec.lua')
-rw-r--r-- | spec/util_promise_spec.lua | 176 |
1 files changed, 175 insertions, 1 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua index 65d252f6..67f33416 100644 --- a/spec/util_promise_spec.lua +++ b/spec/util_promise_spec.lua @@ -17,7 +17,7 @@ describe("util.promise", function () p:next(cb); assert.spy(cb).was_called(1); end); - it("notifies on fulfilment of pending promises", function () + it("notifies on fulfillment of pending promises", function () local r; local p = promise.new(function (resolve) r = resolve; @@ -248,6 +248,30 @@ describe("util.promise", function () assert.spy(cb3).was_called(1); assert.spy(cb3).was_called_with("goodbye"); end); + + it("ordinary values", function () + local p = promise.resolve() + local cb = spy.new(function () + return "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("nil", function () + local p = promise.resolve() + local cb = spy.new(function () + return + 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(nil); + end); end); describe("race()", function () @@ -328,6 +352,130 @@ describe("util.promise", function () assert.spy(cb_err).was_called(1); assert.equal("fail", result); end); + it("works with non-numeric keys", function () + local r1, r2; + local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); + local p = promise.all({ [true] = p1, [false] = p2 }); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r2("yep"); + assert.spy(cb).was_called(0); + r1("nope"); + assert.spy(cb).was_called(1); + assert.same({ [true] = "nope", [false] = "yep" }, result); + end); + it("passes through non-promise values", function () + local r1; + local p1 = promise.new(function (resolve) r1 = resolve end); + local p = promise.all({ [true] = p1, [false] = "yep" }); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r1("nope"); + assert.spy(cb).was_called(1); + assert.same({ [true] = "nope", [false] = "yep" }, result); + end); + end); + describe("all_settled()", function () + it("works with fulfilled promises", function () + local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); + local p = promise.all_settled({ p1, p2 }); + local result; + p:next(function (v) + result = v; + end); + assert.same({ + { status = "fulfilled", value = "yep" }; + { status = "fulfilled", value = "nope" }; + }, result); + end); + it("works with pending promises", function () + local r1, r2; + local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); + local p = promise.all_settled({ p1, p2 }); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r2("yep"); + assert.spy(cb).was_called(0); + r1("nope"); + assert.spy(cb).was_called(1); + assert.same({ + { status = "fulfilled", value = "nope" }; + { status = "fulfilled", value = "yep" }; + }, result); + end); + it("works when some promises reject", function () + local r1, r2; + local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (_, reject) r2 = reject end); + local p = promise.all_settled({ p1, p2 }); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r2("this fails"); + assert.spy(cb).was_called(0); + r1("this succeeds"); + assert.spy(cb).was_called(1); + assert.same({ + { status = "fulfilled", value = "this succeeds" }; + { status = "rejected", reason = "this fails" }; + }, result); + end); + it("works with non-numeric keys", function () + local r1, r2; + local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); + local p = promise.all_settled({ foo = p1, bar = p2 }); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r2("yep"); + assert.spy(cb).was_called(0); + r1("nope"); + assert.spy(cb).was_called(1); + assert.same({ + foo = { status = "fulfilled", value = "nope" }; + bar = { status = "fulfilled", value = "yep" }; + }, result); + end); + it("passes through non-promise values", function () + local r1; + local p1 = promise.new(function (resolve) r1 = resolve end); + local p = promise.all_settled({ foo = p1, bar = "yep" }); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r1("nope"); + assert.spy(cb).was_called(1); + assert.same({ + foo = { status = "fulfilled", value = "nope" }; + bar = "yep"; + }, result); + end); end); describe("catch()", function () it("works", function () @@ -344,6 +492,32 @@ describe("util.promise", function () assert.same({ foo = true }, result); end); end); + describe("join()", function () + it("works", function () + local r1, r2; + local res1, res2; + local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); + + local p = promise.join(function (_res1, _res2) + res1, res2 = _res1, _res2; + return promise.resolve("works"); + end, p1, p2); + + local result; + local cb = spy.new(function (v) + result = v; + end); + p:next(cb); + assert.spy(cb).was_called(0); + r2("yep"); + assert.spy(cb).was_called(0); + r1("nope"); + assert.spy(cb).was_called(1); + assert.same("works", result); + assert.equals("nope", res1); + assert.equals("yep", res2); + end); + end); it("promises may be resolved by other promises", function () local r1, r2; local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); |