Skip to content

Commit

Permalink
added unit test and removed parsedFunction as it is not used
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhrathod01 committed Oct 23, 2024
1 parent cb60ee6 commit c12ee70
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 8 deletions.
8 changes: 1 addition & 7 deletions app/client/src/workers/Evaluation/JSObject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import JSObjectCollection from "./Collection";
import ExecutionMetaData from "../fns/utils/ExecutionMetaData";
import { jsPropertiesState } from "./jsPropertiesState";
import { getFixedTimeDifference } from "workers/common/DataTreeEvaluator/utils";
interface ParseJSAction extends ParsedJSSubAction {
parsedFunction: unknown;
}

/**
* Here we update our unEvalTree according to the change in JSObject's body
Expand Down Expand Up @@ -118,7 +115,7 @@ export function saveResolvedFunctionsAndJSUpdates(
JSObjectASTParseTime,
});

const actionsMap: Record<string, ParseJSAction> = {};
const actionsMap: Record<string, ParsedJSSubAction> = {};
const variablesMap: Record<string, { name: string; value: unknown }> = {};

if (success) {
Expand Down Expand Up @@ -170,7 +167,6 @@ export function saveResolvedFunctionsAndJSUpdates(
name: parsedElement.key,
body: functionString,
arguments: params,
parsedFunction: result,
};
}
} catch {
Expand Down Expand Up @@ -314,8 +310,6 @@ export function parseJSActions(
parsedBody.actions = parsedBody.actions.map((action) => {
return {
...action,
// parsedFunction - used only to determine if function is async
parsedFunction: undefined,
} as ParsedJSSubAction;
});
});
Expand Down
127 changes: 126 additions & 1 deletion app/client/src/workers/Evaluation/JSObject/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { ConfigTree, UnEvalTree } from "entities/DataTree/dataTreeTypes";
import { getUpdatedLocalUnEvalTreeAfterJSUpdates } from ".";
import {
getUpdatedLocalUnEvalTreeAfterJSUpdates,
saveResolvedFunctionsAndJSUpdates,
} from ".";
import type { JSUpdate } from "utils/JSPaneUtils";
import type {
JSActionEntity,
JSActionEntityConfig,
} from "ee/entities/DataTree/types";
import DataTreeEvaluator from "workers/common/DataTreeEvaluator";

describe("updateJSCollectionInUnEvalTree", function () {
it("updates async value of jsAction", () => {
Expand Down Expand Up @@ -137,3 +146,119 @@ describe("updateJSCollectionInUnEvalTree", function () {
expect(expectedResult).toStrictEqual(actualResult);
});
});

describe("saveResolvedFunctionsAndJSUpdates", function () {
it("parses JSObject with duplicate actions, variables and updates jsUpdates correctly", () => {
const dataTreeEvalRef = new DataTreeEvaluator({});
const entity: JSActionEntity = {
actionId: "64013546b956c26882acc587",
body: "export default {\n\tmyVar1: [],\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t\t\n\t},\n\tmyFun2: () => {\n\t\t//use async-await or promises\n\t\tyeso\n\t}\n,\n\tmyFun2: () => {\n\t\t//use async-await or promises\n\t\tyeso\n\t}}",
ENTITY_TYPE: "JSACTION",
name: "JSObject1",
pluginType: "JS",
};
const jsUpdates: Record<string, JSUpdate> = {};
const unEvalDataTree: UnEvalTree = {
JSObject1: {
myVar1: "[]",
myVar2: "{}",
myFun1: new String("() => {}"),
myFun2: new String("async () => {\n yeso;\n}"),
body: "export default {\n\tmyVar1: [],\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t\t\n\t},\n\tmyFun2: () => {\n\t\t//use async-await or promises\n\t\tyeso\n\t}\n,\n\tmyFun2: () => {\n\t\t//use async-await or promises\n\t\tyeso\n\t}}",
ENTITY_TYPE: "JSACTION",
meta: {
myFun1: {
arguments: [],
confirmBeforeExecute: false,
},
myFun2: {
arguments: [],
confirmBeforeExecute: false,
},
},
dependencyMap: {
body: ["myFun1", "myFun2"],
},
dynamicBindingPathList: [
{
key: "body",
},
{
key: "myVar1",
},
{
key: "myVar2",
},
{
key: "myFun1",
},
{
key: "myFun2",
},
{
key: "myFun2",
},
],
bindingPaths: {
body: "SMART_SUBSTITUTE",
myVar1: "SMART_SUBSTITUTE",
myVar2: "SMART_SUBSTITUTE",
myFun1: "SMART_SUBSTITUTE",
myFun2: "SMART_SUBSTITUTE",
},
reactivePaths: {
body: "SMART_SUBSTITUTE",
myVar1: "SMART_SUBSTITUTE",
myVar2: "SMART_SUBSTITUTE",
myFun1: "SMART_SUBSTITUTE",
myFun2: "SMART_SUBSTITUTE",
},
pluginType: "JS",
name: "JSObject1",
actionId: "64013546b956c26882acc587",
} as JSActionEntityConfig,
};
const entityName = "JSObject1";

const result = saveResolvedFunctionsAndJSUpdates(
dataTreeEvalRef,
entity,
jsUpdates,
unEvalDataTree,
entityName,
);

const expectedJSUpdates = {
JSObject1: {
parsedBody: {
body: "export default {\n\tmyVar1: [],\n\tmyVar1: [],\n\tmyVar2: {},\n\tmyFun1: () => {\n\t\t//write code here\n\t\t\n\t},\n\tmyFun2: () => {\n\t\t//use async-await or promises\n\t\tyeso\n\t}\n,\n\tmyFun2: () => {\n\t\t//use async-await or promises\n\t\tyeso\n\t}}",
actions: [
{
name: "myFun1",
body: "() => {}",
arguments: [],
},
{
name: "myFun2",
body: "() => {\n yeso;\n}",
arguments: [],
},
],
variables: [
{
name: "myVar1",
value: "[]",
},
{
name: "myVar2",
value: "{}",
},
],
},
id: "64013546b956c26882acc587",
},
};

expect(result).toStrictEqual(expectedJSUpdates);
});
});

0 comments on commit c12ee70

Please sign in to comment.