From 69e8382811d543b8fc3c7a199d5e11512b4a859e Mon Sep 17 00:00:00 2001 From: xaviergonz Date: Fri, 25 Oct 2019 22:44:10 +0200 Subject: [PATCH] add actionAsync unit test for awaiting later --- test/action-async.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/action-async.ts b/test/action-async.ts index 8670911..0b0e0c2 100644 --- a/test/action-async.ts +++ b/test/action-async.ts @@ -270,6 +270,35 @@ test("it should support async actions within async actions", async () => { expectNoActionsRunning() }) +test("it should support async actions within async actions that are awaited later", async () => { + mobx.configure({ enforceActions: "observed" }) + const values = [] + const x = mobx.observable({ a: 1 }) + mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true }) + + const innerF = actionAsync(async initial => { + x.a = initial // this runs in action + x.a = await task(delay(10, 3)) + await task(delay(30, 0)) + x.a = 6 + return 7 + }) + + const f1 = actionAsync(async initial => { + const futureInnerF = innerF(initial) + x.a = await task(delay(20, 4)) + await task(delay(10, 0)) + x.a = 5 + x.a = await task(futureInnerF) + return x.a + }) + + const v = await f1(2) + expect(v).toBe(7) + expect(values).toEqual([1, 2, 3, 4, 5, 6, 7]) + expectNoActionsRunning() +}) + test("it should support async actions within async actions that throw", async () => { mobx.configure({ enforceActions: "observed" }) const values = []