Comparison

spec/util_promise_spec.lua @ 9557:d7fdd418adf8

util.promise: Add tests ensuring returning a promise resolves the current promise with that promise
author Matthew Wild <mwild1@gmail.com>
date Fri, 26 Oct 2018 09:23:00 +0100
parent 9551:7421379ba464
child 10297:da9f21a70e52
comparison
equal deleted inserted replaced
9556:e4c09e335bd9 9557:d7fdd418adf8
197 local cb2_err = spy.new(function () end); 197 local cb2_err = spy.new(function () end);
198 p2:next(cb2, cb2_err); 198 p2:next(cb2, cb2_err);
199 assert.spy(cb2).was_called(0); 199 assert.spy(cb2).was_called(0);
200 assert.spy(cb2_err).was_called(1); 200 assert.spy(cb2_err).was_called(1);
201 assert.spy(cb2_err).was_called_with("foo"); 201 assert.spy(cb2_err).was_called_with("foo");
202 end);
203
204 describe("allows callbacks to return", function ()
205 it("pending promises", function ()
206 local r;
207 local p = promise.resolve()
208 local cb = spy.new(function ()
209 return promise.new(function (resolve)
210 r = resolve;
211 end);
212 end);
213 local cb2 = spy.new(function () end);
214 p:next(cb):next(cb2);
215 assert.spy(cb).was_called(1);
216 assert.spy(cb2).was_called(0);
217 r("hello");
218 assert.spy(cb).was_called(1);
219 assert.spy(cb2).was_called(1);
220 assert.spy(cb2).was_called_with("hello");
221 end);
222
223 it("resolved promises", function ()
224 local p = promise.resolve()
225 local cb = spy.new(function ()
226 return promise.resolve("hello");
227 end);
228 local cb2 = spy.new(function () end);
229 p:next(cb):next(cb2);
230 assert.spy(cb).was_called(1);
231 assert.spy(cb2).was_called(1);
232 assert.spy(cb2).was_called_with("hello");
233 end);
234
235 it("rejected promises", function ()
236 local p = promise.resolve()
237 local cb = spy.new(function ()
238 return promise.reject("hello");
239 end);
240 local cb2 = spy.new(function ()
241 return promise.reject("goodbye");
242 end);
243 local cb3 = spy.new(function () end);
244 p:next(cb):catch(cb2):catch(cb3);
245 assert.spy(cb).was_called(1);
246 assert.spy(cb2).was_called(1);
247 assert.spy(cb2).was_called_with("hello");
248 assert.spy(cb3).was_called(1);
249 assert.spy(cb3).was_called_with("goodbye");
250 end);
202 end); 251 end);
203 252
204 describe("race()", function () 253 describe("race()", function ()
205 it("works with fulfilled promises", function () 254 it("works with fulfilled promises", function ()
206 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); 255 local p1, p2 = promise.resolve("yep"), promise.resolve("nope");