Skip to content

Commit

Permalink
This PR fixes the Ability to resolve with nothing issue (#389)
Browse files Browse the repository at this point in the history
* Ability to resolve with "nothing"

* Ability to resolve with "nothing" initial commit

* Fixed PR comments

* Changed the logic to resolve the node to anything even if its null or undefined and updated the respective test case

* Reverted test case

* Updated test case as per review feedback

* Updated code as per Tuesday's discussion

* Updated test comments

* Fixed all review comments except for "this is likely incorrect and should be 3"

* Fixed PR comments in test case

* Removed comments

* Fixed last few comments

* Fixed linter issues
  • Loading branch information
sakuntala-motukuri authored Jun 24, 2024
1 parent 7d38b65 commit 1f3b30a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
81 changes: 79 additions & 2 deletions plugins/async-node/core/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import type { Node, InProgressState } from "@player-ui/player";
import { Node, InProgressState, ViewInstance } from "@player-ui/player";
import { Player } from "@player-ui/player";
import { waitFor } from "@testing-library/react";
import { AsyncNodePlugin, AsyncNodePluginPlugin } from "./index";
Expand All @@ -18,7 +18,7 @@ const basicFRFWithActions = {
},
},
{
id: "uhh",
id: "nodeId",
async: "true",
},
],
Expand All @@ -37,6 +37,83 @@ const basicFRFWithActions = {
},
};

const asyncNodeTest = async (resolvedValue: any) => {
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; // Promise would be resolved only once
});
});

let updateNumber = 0;

const player = new Player({ plugins: [plugin] });

let viewInstance: ViewInstance | undefined;

player.hooks.viewController.tap("async-node-test", (vc) => {
vc.hooks.view.tap("async-node-test", (view) => {
viewInstance = view;
view.hooks.onUpdate.tap("async-node-test", () => {
updateNumber++;
});
});
});

player.start(basicFRFWithActions as any);

let view = (player.getState() as InProgressState).controllers.view.currentView
?.lastUpdate;

expect(view).toBeDefined();
expect(view?.actions[0].asset.type).toBe("action");
expect(view?.actions[1]).toBeUndefined();

await waitFor(() => {
expect(deferredResolve).toBeDefined();
});

// Consumer responds with null/undefined
if (deferredResolve) {
deferredResolve(resolvedValue);
}

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.length).toBe(1);

viewInstance.update();

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(1);
};

test("should return current node view when the resolved node is null", async () => {
await asyncNodeTest(null);
});

test("should return current node view when the resolved node is undefined", async () => {
await asyncNodeTest(undefined);
});

test("replaces async nodes with provided node", async () => {
const plugin = new AsyncNodePlugin({
plugins: [new AsyncNodePluginPlugin()],
Expand Down
8 changes: 3 additions & 5 deletions plugins/async-node/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {

name = "AsyncNode";

private resolvedMapping = new Map<string, Node.Node>();
private resolvedMapping = new Map<string, any>();

private currentView: ViewInstance | undefined;

Expand Down Expand Up @@ -167,10 +167,8 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
? options.parseNode(result)
: undefined;

if (parsedNode) {
this.resolvedMapping.set(node.id, parsedNode);
view.updateAsync();
}
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : node);
view.updateAsync();
});

return node;
Expand Down

0 comments on commit 1f3b30a

Please sign in to comment.