From bed39af8c55558871de753c8e8d075afec9f9702 Mon Sep 17 00:00:00 2001 From: sakuntala_motukuri Date: Tue, 9 Jul 2024 14:56:09 -0400 Subject: [PATCH] Updated test case and code cleanup --- plugins/async-node/core/src/index.test.ts | 87 +++++++++++++++++++++-- plugins/async-node/core/src/index.ts | 12 ---- 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/plugins/async-node/core/src/index.test.ts b/plugins/async-node/core/src/index.test.ts index 824089d25..f9bb91c3d 100644 --- a/plugins/async-node/core/src/index.test.ts +++ b/plugins/async-node/core/src/index.test.ts @@ -37,8 +37,6 @@ const basicFRFWithActions = { }, }; -let count = 0; - const asyncNodeTest = async (resolvedValue: any) => { const plugin = new AsyncNodePlugin({ plugins: [new AsyncNodePluginPlugin()], @@ -48,10 +46,7 @@ const asyncNodeTest = async (resolvedValue: any) => { plugin.hooks.onAsyncNode.tap("test", async (node: Node.Node) => { return new Promise((resolve) => { - setTimeout(() => { - deferredResolve = resolve; - }, 100) - console.log('count--',count++); + deferredResolve = resolve; }); }); @@ -119,6 +114,86 @@ test("should return current node view when the resolved node is undefined", asyn await asyncNodeTest(undefined); }); +test("should handle the promise using .then", async () => { + const plugin = new AsyncNodePlugin({ + plugins: [new AsyncNodePluginPlugin()], + }); + + let deferredResolve: ((value: any) => void) | undefined; + + plugin.hooks.onAsyncNode.tap("test", async (node: Node.Node) => { + return new Promise((resolve) => { + deferredResolve = resolve; + }); + }); + let updateNumber = 0; + + const player = new Player({ plugins: [plugin] }); + + player.hooks.viewController.tap("async-node-test", (vc) => { + vc.hooks.view.tap("async-node-test", (view) => { + view.hooks.onUpdate.tap("async-node-test", (update) => { + updateNumber++; + }); + }); + }); + + player.start(basicFRFWithActions as any); + + let view = (player.getState() as InProgressState).controllers.view.currentView + ?.lastUpdate; + + expect(view).toBeDefined(); + expect(view?.actions[1]).toBeUndefined(); + + await waitFor(() => { + expect(updateNumber).toBe(1); + expect(deferredResolve).toBeDefined(); + }); + + if (deferredResolve) { + deferredResolve([ + { + asset: { + id: "value-1", + type: "text", + value: "1st value in the multinode", + }, + }, + { + id: "another-async", + async: true, + }, + ]); + } + + await waitFor(() => { + expect(updateNumber).toBe(2); + }); + + view = (player.getState() as InProgressState).controllers.view.currentView + ?.lastUpdate; + + expect(view?.actions[0].asset.type).toBe("action"); + expect(view?.actions[1].asset.type).toBe("text"); + expect(view?.actions[2]).toBeUndefined(); + expect(updateNumber).toBe(2); + + if (deferredResolve) { + deferredResolve(null); + } + + await waitFor(() => { + expect(updateNumber).toBe(3); + }); + + view = (player.getState() as InProgressState).controllers.view.currentView + ?.lastUpdate; + + expect(view?.actions[0].asset.type).toBe("action"); + expect(view?.actions.length).toBe(2); +}); + test("replaces async nodes with provided node", async () => { const plugin = new AsyncNodePlugin({ plugins: [new AsyncNodePluginPlugin()], diff --git a/plugins/async-node/core/src/index.ts b/plugins/async-node/core/src/index.ts index 37763900a..027ad78e8 100644 --- a/plugins/async-node/core/src/index.ts +++ b/plugins/async-node/core/src/index.ts @@ -27,9 +27,6 @@ export interface AsyncNodeViewPlugin extends ViewPlugin { asyncNode: AsyncParallelBailHook<[Node.Node], Node.Node>; } -let counter =0; -let count = 0; - /** * Async node plugin used to resolve async nodes in the content * If an async node is present, allow users to provide a replacement node to be rendered when ready @@ -154,20 +151,13 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin { let resolvedNode; if (this.isAsync(node) && this.resolvedMapping.has(node.id)) { const mappedValue = this.resolvedMapping.get(node.id); - count++; - console.log("mappedValue", mappedValue + '-----', count); if (mappedValue) { resolvedNode = mappedValue; } - } else { resolvedNode = null; } - - const newNode = resolvedNode || node; - - console.log("newNode", newNode); if (!resolvedNode && node?.type === NodeType.Async) { queueMicrotask(async () => { this.basePlugin?.hooks.onAsyncNode.call(node).then((result) => { @@ -176,8 +166,6 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin { ? options.parseNode(result) : undefined; - counter++; - console.log("parsedNode---", parsedNode + '-----'+'counter---', counter); this.resolvedMapping.set(node.id, parsedNode ? parsedNode : {}); view.updateAsync(); });