From eec002678aa0a5083ff4650c2b42edeabc388318 Mon Sep 17 00:00:00 2001 From: Evin O'Shea <41702196+evinosheaforward@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:00:39 -0400 Subject: [PATCH] Fix/get members 2173 (#2174) * test: add failing test to confirm changed behavior issue-2173 * fix: correct getMembers actions/flowactions categorization issue-2173 * chore: update if/else formatting in getMembers for posterity issue-2173 * test: add explicit expect for flow action is added to actions in getMembers issue-2173 * test: add flow action testing to reflection - members chained test issue-2173 --------- Co-authored-by: Evin O'Shea --- __tests__/core/reflection.test.ts | 24 ++++++++++++++++++++++++ src/core/mst-operations.ts | 14 ++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/__tests__/core/reflection.test.ts b/__tests__/core/reflection.test.ts index 71c9e0cbe..b18f4cc69 100644 --- a/__tests__/core/reflection.test.ts +++ b/__tests__/core/reflection.test.ts @@ -60,8 +60,11 @@ test("reflection - model", () => { const reflection = getMembers(node) expect(reflection.name).toBe("AnonymousModel") expect(reflection.actions.includes("actionName")).toBe(true) + expect(reflection.actions.includes("generatorAction")).toBe(true) expect(reflection.flowActions.includes("generatorAction")).toBe(true) + expect(reflection.flowActions.includes("actionName")).toBe(false) expect(reflection.views.includes("viewName")).toBe(true) + expect(reflection.views.includes("actionName")).toBe(false) expect(reflection.volatile.includes("volatileProperty")).toBe(true) expect(!!reflection.properties.users).toBe(true) expect(!!reflection.properties.isPerson).toBe(true) @@ -165,6 +168,20 @@ test("reflection - members chained", () => { } } }) + .actions((self) => { + function flowActionName() { + return 1 + } + return { + flowActionName, + generatorAction: flow(function* generatorAction() { + const promise = new Promise((resolve) => { + resolve(true) + }) + yield promise + }) + } + }) .views((self) => ({ get viewName() { return 1 @@ -181,8 +198,15 @@ test("reflection - members chained", () => { expect(keys.includes("isPerson")).toBe(true) expect(reflection.actions.includes("actionName")).toBe(true) expect(reflection.actions.includes("anotherAction")).toBe(true) + expect(reflection.actions.includes("flowActionName")).toBe(true) + expect(reflection.actions.includes("generatorAction")).toBe(true) + expect(reflection.flowActions.includes("generatorAction")).toBe(true) + expect(reflection.flowActions.includes("flowActionName")).toBe(false) expect(reflection.views.includes("viewName")).toBe(true) expect(reflection.views.includes("anotherView")).toBe(true) + expect(reflection.views.includes("actionName")).toBe(false) + expect(reflection.views.includes("anotherAction")).toBe(false) + expect(reflection.views.includes("flowActionName")).toBe(false) }) test("reflection - conditionals respected", () => { let swap = true diff --git a/src/core/mst-operations.ts b/src/core/mst-operations.ts index 3b99c0eb1..e58f17225 100644 --- a/src/core/mst-operations.ts +++ b/src/core/mst-operations.ts @@ -870,10 +870,16 @@ export function getMembers(target: IAnyStateTreeNode): IModelReflectionData { else reflected.volatile.push(key) return } - if (descriptor.value._isMSTAction === true) reflected.actions.push(key) - if (descriptor.value._isFlowAction === true) reflected.flowActions.push(key) - else if (isObservableProp(target, key)) reflected.volatile.push(key) - else reflected.views.push(key) + if (descriptor.value._isFlowAction === true) { + reflected.flowActions.push(key) + } + if (descriptor.value._isMSTAction === true) { + reflected.actions.push(key) + } else if (isObservableProp(target, key)) { + reflected.volatile.push(key) + } else { + reflected.views.push(key) + } }) return reflected }