PromucFlow_constructor/app/client/src/workers/evaluate.test.ts

264 lines
7.2 KiB
TypeScript
Raw Normal View History

import evaluate, {
setupEvaluationEnvironment,
evaluateAsync,
isFunctionAsync,
} from "workers/evaluate";
import {
DataTree,
DataTreeWidget,
ENTITY_TYPE,
} from "entities/DataTree/dataTreeFactory";
import { RenderModes } from "constants/WidgetConstants";
describe("evaluateSync", () => {
const widget: DataTreeWidget = {
bottomRow: 0,
isLoading: false,
leftColumn: 0,
parentColumnSpace: 0,
parentRowSpace: 0,
renderMode: RenderModes.CANVAS,
rightColumn: 0,
topRow: 0,
type: "INPUT_WIDGET",
version: 0,
widgetId: "",
widgetName: "",
text: "value",
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
bindingPaths: {},
triggerPaths: {},
validationPaths: {},
logBlackList: {},
};
const dataTree: DataTree = {
Input1: widget,
};
beforeAll(() => {
setupEvaluationEnvironment();
});
it("unescapes string before evaluation", () => {
const js = '\\"Hello!\\"';
feat: JS Editor (#6003) * Changes to add js plugin * routes+reducer+create template * added debugger to js editor page * entity explorer changes * create js function * added copy, move and delete action * added js plugin * added existing js functions to data tree * removed actionconfig for js collection * new js function added to data tree and entity as well * parsing flow added * changes to data tree * parse and update js functions * small changes for def creator for js action * create delete modified * small changes for update * update flow change * entity properties added * removed linting errors * small changes in entity explorer * changes for update * move, copy implementation * conflict resolved * changes for dependecy map creation * Only make the variables the binding paths * Basic eval sync working * Minor fixes * removed unwanted code * entity props and autocomplete * saving in progress show * redirection fix after delete js action * removed unnecessary line * Fixing merge conflict * added sample body * removed dummy data and added plugin Type * few PR comments fixed * automplete fix * few more PR comments fix * PR commnets fix * move and copy api change * js colleciton name refactor & 'move to page' changes & search * view changes * autocomplete added for js collections * removing till async is implemented * small changes * separate js pane response view * Executing functions * js collection to js objects * entity explorer issue and resolve action on page switch * removed unused line * small color fix * js file icon added * added js action to property pane * Property pane changes for actions * property pane changes for js functions * showing syntax error for now * actions sorted in response tab * added js objects to slash and recent entitties * enabling this to be used inside of function * eval fix * feature flag changes for entity explorer and property pane * debugger changes * copy bug fix * small changes for eval * debugger bug fix * chnaged any to specific types * error in console fix * icons update * fixed test case * test case fix * non empty check for functions * evaluate test case fix * added new icons * text change * updated time for debounce for trial * after release mereg * changed icon * after merge * PR comments simple * fixed PR comments - redux form, settings remove * js object interface changes * name refactor * export default change * delete resolve actions chnage * after merge * adding execute fn as 3rd option and removed create new js function * issue 7054 fixed - app crash * execute function on response tab changes * refactor function name part 1 * refactor of js function name * try catch added refactor * test fix * not used line removed * test cases locator fixed Co-authored-by: Nidhi <nidhi.nair93@gmail.com> Co-authored-by: hetunandu <hetu@appsmith.com>
2021-09-08 17:32:22 +00:00
const response = evaluate(js, {}, {});
expect(response.result).toBe("Hello!");
});
it("evaluate string post unescape in v1", () => {
const js = '[1, 2, 3].join("\\\\n")';
const response = evaluate(js, {}, {});
expect(response.result).toBe("1\n2\n3");
});
it("evaluate string without unescape in v2", () => {
self.evaluationVersion = 2;
const js = '[1, 2, 3].join("\\n")';
const response = evaluate(js, {}, {});
expect(response.result).toBe("1\n2\n3");
});
it("throws error for undefined js", () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(() => evaluate(undefined, {})).toThrow(TypeError);
});
it("Returns for syntax errors", () => {
feat: JS Editor (#6003) * Changes to add js plugin * routes+reducer+create template * added debugger to js editor page * entity explorer changes * create js function * added copy, move and delete action * added js plugin * added existing js functions to data tree * removed actionconfig for js collection * new js function added to data tree and entity as well * parsing flow added * changes to data tree * parse and update js functions * small changes for def creator for js action * create delete modified * small changes for update * update flow change * entity properties added * removed linting errors * small changes in entity explorer * changes for update * move, copy implementation * conflict resolved * changes for dependecy map creation * Only make the variables the binding paths * Basic eval sync working * Minor fixes * removed unwanted code * entity props and autocomplete * saving in progress show * redirection fix after delete js action * removed unnecessary line * Fixing merge conflict * added sample body * removed dummy data and added plugin Type * few PR comments fixed * automplete fix * few more PR comments fix * PR commnets fix * move and copy api change * js colleciton name refactor & 'move to page' changes & search * view changes * autocomplete added for js collections * removing till async is implemented * small changes * separate js pane response view * Executing functions * js collection to js objects * entity explorer issue and resolve action on page switch * removed unused line * small color fix * js file icon added * added js action to property pane * Property pane changes for actions * property pane changes for js functions * showing syntax error for now * actions sorted in response tab * added js objects to slash and recent entitties * enabling this to be used inside of function * eval fix * feature flag changes for entity explorer and property pane * debugger changes * copy bug fix * small changes for eval * debugger bug fix * chnaged any to specific types * error in console fix * icons update * fixed test case * test case fix * non empty check for functions * evaluate test case fix * added new icons * text change * updated time for debounce for trial * after release mereg * changed icon * after merge * PR comments simple * fixed PR comments - redux form, settings remove * js object interface changes * name refactor * export default change * delete resolve actions chnage * after merge * adding execute fn as 3rd option and removed create new js function * issue 7054 fixed - app crash * execute function on response tab changes * refactor function name part 1 * refactor of js function name * try catch added refactor * test fix * not used line removed * test cases locator fixed Co-authored-by: Nidhi <nidhi.nair93@gmail.com> Co-authored-by: hetunandu <hetu@appsmith.com>
2021-09-08 17:32:22 +00:00
const response1 = evaluate("wrongJS", {}, {});
expect(response1).toStrictEqual({
result: undefined,
errors: [
{
ch: 1,
code: "W117",
errorMessage: "'wrongJS' is not defined.",
errorSegment: " const result = wrongJS",
errorType: "LINT",
line: 0,
raw: `
function closedFunction () {
const result = wrongJS
return result;
}
closedFunction()
`,
severity: "error",
originalBinding: "wrongJS",
variables: ["wrongJS", undefined, undefined, undefined],
},
{
errorMessage: "ReferenceError: wrongJS is not defined",
errorType: "PARSE",
raw: `
function closedFunction () {
const result = wrongJS
return result;
}
closedFunction()
`,
severity: "error",
originalBinding: "wrongJS",
},
],
});
feat: JS Editor (#6003) * Changes to add js plugin * routes+reducer+create template * added debugger to js editor page * entity explorer changes * create js function * added copy, move and delete action * added js plugin * added existing js functions to data tree * removed actionconfig for js collection * new js function added to data tree and entity as well * parsing flow added * changes to data tree * parse and update js functions * small changes for def creator for js action * create delete modified * small changes for update * update flow change * entity properties added * removed linting errors * small changes in entity explorer * changes for update * move, copy implementation * conflict resolved * changes for dependecy map creation * Only make the variables the binding paths * Basic eval sync working * Minor fixes * removed unwanted code * entity props and autocomplete * saving in progress show * redirection fix after delete js action * removed unnecessary line * Fixing merge conflict * added sample body * removed dummy data and added plugin Type * few PR comments fixed * automplete fix * few more PR comments fix * PR commnets fix * move and copy api change * js colleciton name refactor & 'move to page' changes & search * view changes * autocomplete added for js collections * removing till async is implemented * small changes * separate js pane response view * Executing functions * js collection to js objects * entity explorer issue and resolve action on page switch * removed unused line * small color fix * js file icon added * added js action to property pane * Property pane changes for actions * property pane changes for js functions * showing syntax error for now * actions sorted in response tab * added js objects to slash and recent entitties * enabling this to be used inside of function * eval fix * feature flag changes for entity explorer and property pane * debugger changes * copy bug fix * small changes for eval * debugger bug fix * chnaged any to specific types * error in console fix * icons update * fixed test case * test case fix * non empty check for functions * evaluate test case fix * added new icons * text change * updated time for debounce for trial * after release mereg * changed icon * after merge * PR comments simple * fixed PR comments - redux form, settings remove * js object interface changes * name refactor * export default change * delete resolve actions chnage * after merge * adding execute fn as 3rd option and removed create new js function * issue 7054 fixed - app crash * execute function on response tab changes * refactor function name part 1 * refactor of js function name * try catch added refactor * test fix * not used line removed * test cases locator fixed Co-authored-by: Nidhi <nidhi.nair93@gmail.com> Co-authored-by: hetunandu <hetu@appsmith.com>
2021-09-08 17:32:22 +00:00
const response2 = evaluate("{}.map()", {}, {});
expect(response2).toStrictEqual({
result: undefined,
errors: [
{
errorMessage: "TypeError: {}.map is not a function",
errorType: "PARSE",
raw: `
function closedFunction () {
const result = {}.map()
return result;
}
closedFunction()
`,
severity: "error",
originalBinding: "{}.map()",
},
],
});
});
it("evaluates value from data tree", () => {
const js = "Input1.text";
feat: JS Editor (#6003) * Changes to add js plugin * routes+reducer+create template * added debugger to js editor page * entity explorer changes * create js function * added copy, move and delete action * added js plugin * added existing js functions to data tree * removed actionconfig for js collection * new js function added to data tree and entity as well * parsing flow added * changes to data tree * parse and update js functions * small changes for def creator for js action * create delete modified * small changes for update * update flow change * entity properties added * removed linting errors * small changes in entity explorer * changes for update * move, copy implementation * conflict resolved * changes for dependecy map creation * Only make the variables the binding paths * Basic eval sync working * Minor fixes * removed unwanted code * entity props and autocomplete * saving in progress show * redirection fix after delete js action * removed unnecessary line * Fixing merge conflict * added sample body * removed dummy data and added plugin Type * few PR comments fixed * automplete fix * few more PR comments fix * PR commnets fix * move and copy api change * js colleciton name refactor & 'move to page' changes & search * view changes * autocomplete added for js collections * removing till async is implemented * small changes * separate js pane response view * Executing functions * js collection to js objects * entity explorer issue and resolve action on page switch * removed unused line * small color fix * js file icon added * added js action to property pane * Property pane changes for actions * property pane changes for js functions * showing syntax error for now * actions sorted in response tab * added js objects to slash and recent entitties * enabling this to be used inside of function * eval fix * feature flag changes for entity explorer and property pane * debugger changes * copy bug fix * small changes for eval * debugger bug fix * chnaged any to specific types * error in console fix * icons update * fixed test case * test case fix * non empty check for functions * evaluate test case fix * added new icons * text change * updated time for debounce for trial * after release mereg * changed icon * after merge * PR comments simple * fixed PR comments - redux form, settings remove * js object interface changes * name refactor * export default change * delete resolve actions chnage * after merge * adding execute fn as 3rd option and removed create new js function * issue 7054 fixed - app crash * execute function on response tab changes * refactor function name part 1 * refactor of js function name * try catch added refactor * test fix * not used line removed * test cases locator fixed Co-authored-by: Nidhi <nidhi.nair93@gmail.com> Co-authored-by: hetunandu <hetu@appsmith.com>
2021-09-08 17:32:22 +00:00
const response = evaluate(js, dataTree, {});
expect(response.result).toBe("value");
});
it("disallows unsafe function calls", () => {
const js = "setTimeout(() => {}, 100)";
feat: JS Editor (#6003) * Changes to add js plugin * routes+reducer+create template * added debugger to js editor page * entity explorer changes * create js function * added copy, move and delete action * added js plugin * added existing js functions to data tree * removed actionconfig for js collection * new js function added to data tree and entity as well * parsing flow added * changes to data tree * parse and update js functions * small changes for def creator for js action * create delete modified * small changes for update * update flow change * entity properties added * removed linting errors * small changes in entity explorer * changes for update * move, copy implementation * conflict resolved * changes for dependecy map creation * Only make the variables the binding paths * Basic eval sync working * Minor fixes * removed unwanted code * entity props and autocomplete * saving in progress show * redirection fix after delete js action * removed unnecessary line * Fixing merge conflict * added sample body * removed dummy data and added plugin Type * few PR comments fixed * automplete fix * few more PR comments fix * PR commnets fix * move and copy api change * js colleciton name refactor & 'move to page' changes & search * view changes * autocomplete added for js collections * removing till async is implemented * small changes * separate js pane response view * Executing functions * js collection to js objects * entity explorer issue and resolve action on page switch * removed unused line * small color fix * js file icon added * added js action to property pane * Property pane changes for actions * property pane changes for js functions * showing syntax error for now * actions sorted in response tab * added js objects to slash and recent entitties * enabling this to be used inside of function * eval fix * feature flag changes for entity explorer and property pane * debugger changes * copy bug fix * small changes for eval * debugger bug fix * chnaged any to specific types * error in console fix * icons update * fixed test case * test case fix * non empty check for functions * evaluate test case fix * added new icons * text change * updated time for debounce for trial * after release mereg * changed icon * after merge * PR comments simple * fixed PR comments - redux form, settings remove * js object interface changes * name refactor * export default change * delete resolve actions chnage * after merge * adding execute fn as 3rd option and removed create new js function * issue 7054 fixed - app crash * execute function on response tab changes * refactor function name part 1 * refactor of js function name * try catch added refactor * test fix * not used line removed * test cases locator fixed Co-authored-by: Nidhi <nidhi.nair93@gmail.com> Co-authored-by: hetunandu <hetu@appsmith.com>
2021-09-08 17:32:22 +00:00
const response = evaluate(js, dataTree, {});
expect(response).toStrictEqual({
result: undefined,
errors: [
{
errorMessage: "TypeError: setTimeout is not a function",
errorType: "PARSE",
raw: `
function closedFunction () {
const result = setTimeout(() => {}, 100)
return result;
}
closedFunction()
`,
severity: "error",
originalBinding: "setTimeout(() => {}, 100)",
},
],
});
});
it("has access to extra library functions", () => {
const js = "_.add(1,2)";
feat: JS Editor (#6003) * Changes to add js plugin * routes+reducer+create template * added debugger to js editor page * entity explorer changes * create js function * added copy, move and delete action * added js plugin * added existing js functions to data tree * removed actionconfig for js collection * new js function added to data tree and entity as well * parsing flow added * changes to data tree * parse and update js functions * small changes for def creator for js action * create delete modified * small changes for update * update flow change * entity properties added * removed linting errors * small changes in entity explorer * changes for update * move, copy implementation * conflict resolved * changes for dependecy map creation * Only make the variables the binding paths * Basic eval sync working * Minor fixes * removed unwanted code * entity props and autocomplete * saving in progress show * redirection fix after delete js action * removed unnecessary line * Fixing merge conflict * added sample body * removed dummy data and added plugin Type * few PR comments fixed * automplete fix * few more PR comments fix * PR commnets fix * move and copy api change * js colleciton name refactor & 'move to page' changes & search * view changes * autocomplete added for js collections * removing till async is implemented * small changes * separate js pane response view * Executing functions * js collection to js objects * entity explorer issue and resolve action on page switch * removed unused line * small color fix * js file icon added * added js action to property pane * Property pane changes for actions * property pane changes for js functions * showing syntax error for now * actions sorted in response tab * added js objects to slash and recent entitties * enabling this to be used inside of function * eval fix * feature flag changes for entity explorer and property pane * debugger changes * copy bug fix * small changes for eval * debugger bug fix * chnaged any to specific types * error in console fix * icons update * fixed test case * test case fix * non empty check for functions * evaluate test case fix * added new icons * text change * updated time for debounce for trial * after release mereg * changed icon * after merge * PR comments simple * fixed PR comments - redux form, settings remove * js object interface changes * name refactor * export default change * delete resolve actions chnage * after merge * adding execute fn as 3rd option and removed create new js function * issue 7054 fixed - app crash * execute function on response tab changes * refactor function name part 1 * refactor of js function name * try catch added refactor * test fix * not used line removed * test cases locator fixed Co-authored-by: Nidhi <nidhi.nair93@gmail.com> Co-authored-by: hetunandu <hetu@appsmith.com>
2021-09-08 17:32:22 +00:00
const response = evaluate(js, dataTree, {});
expect(response.result).toBe(3);
});
it("evaluates functions with callback data", () => {
const js = "(arg1, arg2) => arg1.value + arg2";
const callbackData = [{ value: "test" }, "1"];
const response = evaluate(js, dataTree, {}, callbackData);
expect(response.result).toBe("test1");
});
it("handles EXPRESSIONS with new lines", () => {
let js = "\n";
let response = evaluate(js, dataTree, {});
expect(response.errors.length).toBe(0);
js = "\n\n\n";
response = evaluate(js, dataTree, {});
expect(response.errors.length).toBe(0);
});
it("handles TRIGGERS with new lines", () => {
let js = "\n";
let response = evaluate(js, dataTree, {}, undefined);
expect(response.errors.length).toBe(0);
js = "\n\n\n";
response = evaluate(js, dataTree, {}, undefined);
expect(response.errors.length).toBe(0);
});
it("handles ANONYMOUS_FUNCTION with new lines", () => {
let js = "\n";
let response = evaluate(js, dataTree, {}, undefined);
expect(response.errors.length).toBe(0);
js = "\n\n\n";
response = evaluate(js, dataTree, {}, undefined);
expect(response.errors.length).toBe(0);
});
});
describe("evaluateAsync", () => {
it("runs and completes", async () => {
const js = "(() => new Promise((resolve) => { resolve(123) }))()";
self.postMessage = jest.fn();
await evaluateAsync(js, {}, "TEST_REQUEST", {});
expect(self.postMessage).toBeCalledWith({
requestId: "TEST_REQUEST",
responseData: {
finished: true,
result: { errors: [], result: 123, triggers: [] },
},
type: "PROCESS_TRIGGER",
});
});
it("runs and returns errors", async () => {
jest.restoreAllMocks();
const js = "(() => new Promise((resolve) => { randomKeyword }))()";
self.postMessage = jest.fn();
await evaluateAsync(js, {}, "TEST_REQUEST_1", {});
expect(self.postMessage).toBeCalledWith({
requestId: "TEST_REQUEST_1",
responseData: {
finished: true,
result: {
errors: [
{
errorMessage: expect.stringContaining(
"randomKeyword is not defined",
),
errorType: "PARSE",
originalBinding: expect.stringContaining("Promise"),
raw: expect.stringContaining("Promise"),
severity: "error",
},
],
triggers: [],
result: undefined,
},
},
type: "PROCESS_TRIGGER",
});
});
});
describe("isFunctionAsync", () => {
it("identifies async functions", () => {
// eslint-disable-next-line @typescript-eslint/ban-types
const cases: Array<{ script: Function | string; expected: boolean }> = [
{
script: () => {
return 1;
},
expected: false,
},
{
script: () => {
return new Promise((resolve) => {
resolve(1);
});
},
expected: true,
},
{
script: "() => { showAlert('yo') }",
expected: true,
},
];
for (const testCase of cases) {
let testFunc = testCase.script;
if (typeof testFunc === "string") {
testFunc = eval(testFunc);
}
const actual = isFunctionAsync(testFunc, {});
expect(actual).toBe(testCase.expected);
}
});
});