Software /
code /
prosody
Comparison
spec/util_promise_spec.lua @ 9514:9db707a86a25
util.promise: Add promise:finally()
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 18 Oct 2018 12:13:17 +0100 |
parent | 9511:cb88d729e98d |
child | 9517:b1c6ede17592 |
comparison
equal
deleted
inserted
replaced
9513:4f4f9823bd1d | 9514:9db707a86a25 |
---|---|
262 local cb = spy.new(function () end); | 262 local cb = spy.new(function () end); |
263 p:next(cb); | 263 p:next(cb); |
264 assert.spy(cb).was_called(0); | 264 assert.spy(cb).was_called(0); |
265 end); | 265 end); |
266 end); | 266 end); |
267 describe("finally()", function () | |
268 local p, p2, resolve, reject, on_finally; | |
269 before_each(function () | |
270 p = promise.new(function (_resolve, _reject) | |
271 resolve, reject = _resolve, _reject; | |
272 end); | |
273 on_finally = spy.new(function () end); | |
274 p2 = p:finally(on_finally); | |
275 end); | |
276 it("runs when a promise is resolved", function () | |
277 assert.spy(on_finally).was_called(0); | |
278 resolve("foo"); | |
279 assert.spy(on_finally).was_called(1); | |
280 assert.spy(on_finally).was_not_called_with("foo"); | |
281 end); | |
282 it("runs when a promise is rejected", function () | |
283 assert.spy(on_finally).was_called(0); | |
284 reject("foo"); | |
285 assert.spy(on_finally).was_called(1); | |
286 assert.spy(on_finally).was_not_called_with("foo"); | |
287 end); | |
288 it("returns a promise that fulfills with the original value", function () | |
289 local cb2 = spy.new(function () end); | |
290 p2:next(cb2); | |
291 assert.spy(on_finally).was_called(0); | |
292 assert.spy(cb2).was_called(0); | |
293 resolve("foo"); | |
294 assert.spy(on_finally).was_called(1); | |
295 assert.spy(cb2).was_called(1); | |
296 assert.spy(on_finally).was_not_called_with("foo"); | |
297 assert.spy(cb2).was_called_with("foo"); | |
298 end); | |
299 it("returns a promise that rejects with the original error", function () | |
300 local on_finally_err = spy.new(function () end); | |
301 local on_finally_ok = spy.new(function () end); | |
302 p2:catch(on_finally_err); | |
303 p2:next(on_finally_ok); | |
304 assert.spy(on_finally).was_called(0); | |
305 assert.spy(on_finally_err).was_called(0); | |
306 reject("foo"); | |
307 assert.spy(on_finally).was_called(1); | |
308 -- Since the original promise was rejected, the finally promise should also be | |
309 assert.spy(on_finally_ok).was_called(0); | |
310 assert.spy(on_finally_err).was_called(1); | |
311 assert.spy(on_finally).was_not_called_with("foo"); | |
312 assert.spy(on_finally_err).was_called_with("foo"); | |
313 end); | |
314 it("returns a promise that rejects with an uncaught error inside on_finally", function () | |
315 p = promise.new(function (_resolve, _reject) | |
316 resolve, reject = _resolve, _reject; | |
317 end); | |
318 local test_error = {}; | |
319 on_finally = spy.new(function () error(test_error) end); | |
320 p2 = p:finally(on_finally); | |
321 | |
322 local on_finally_err = spy.new(function () end); | |
323 p2:catch(on_finally_err); | |
324 assert.spy(on_finally).was_called(0); | |
325 assert.spy(on_finally_err).was_called(0); | |
326 reject("foo"); | |
327 assert.spy(on_finally).was_called(1); | |
328 assert.spy(on_finally_err).was_called(1); | |
329 assert.spy(on_finally).was_not_called_with("foo"); | |
330 assert.spy(on_finally).was_not_called_with(test_error); | |
331 assert.spy(on_finally_err).was_called_with(test_error); | |
332 end); | |
333 end); | |
267 end); | 334 end); |