diff --git a/app/client/src/components/editorComponents/ActionCreator/Fields.test.ts b/app/client/src/components/editorComponents/ActionCreator/Fields.test.ts new file mode 100644 index 0000000000..c1cf70c25a --- /dev/null +++ b/app/client/src/components/editorComponents/ActionCreator/Fields.test.ts @@ -0,0 +1,80 @@ +jest.mock("sagas/ActionExecution/NavigateActionSaga", () => ({ + __esModule: true, + default: "", + NavigationTargetType: { SAME_WINDOW: "" }, +})); + +import { argsStringToArray } from "./Fields"; + +describe("Test argStringToArray", () => { + const cases = [ + { index: 0, input: "", expected: [""] }, + { index: 1, input: "'a'", expected: ["'a'"] }, + { index: 2, input: "a", expected: ["a"] }, + { index: 3, input: "'a,b,c'", expected: ["'a,b,c'"] }, + { index: 4, input: "a,b,c", expected: ["a", "b", "c"] }, + { index: 5, input: "a, b, c", expected: ["a", " b", " c"] }, + { index: 6, input: "a , b , c", expected: ["a ", " b ", " c"] }, + { index: 7, input: "a\n,\nb,\nc", expected: ["a\n", "\nb", "\nc"] }, + { index: 8, input: "[a,b,c]", expected: ["[a,b,c]"] }, + { index: 9, input: "[a, b, c]", expected: ["[a, b, c]"] }, + { + index: 10, + input: "[\n\ta,\n\tb,\n\tc\n]", + expected: ["[\n\ta,\n\tb,\n\tc\n]"], + }, + { index: 11, input: "{a:1,b:2,c:3}", expected: ["{a:1,b:2,c:3}"] }, + { + index: 12, + input: '{"a":1,"b":2,"c":3}', + expected: ['{"a":1,"b":2,"c":3}'], + }, + { + index: 13, + input: "{\n\ta:1,\n\tb:2,\n\tc:3}", + expected: ["{\n\ta:1,\n\tb:2,\n\tc:3}"], + }, + { + index: 14, + input: "()=>{}", + expected: ["()=>{}"], + }, + { + index: 15, + input: "(a, b)=>{return a+b}", + expected: ["(a, b)=>{return a+b}"], + }, + { + index: 16, + input: "(a, b)=>{\n\treturn a+b;\n\t}", + expected: ["(a, b)=>{\n\treturn a+b;\n\t}"], + }, + { + index: 17, + input: "(\n\ta,\n\tb\n)=>{\n\treturn a+b;\n\t}", + expected: ["(\n\ta,\n\tb\n)=>{\n\treturn a+b;\n\t}"], + }, + { + index: 18, + input: `() => {return 5}`, + expected: ["() => {return 5}"], + }, + { + index: 19, + input: `(a) => {return a + 1}`, + expected: ["(a) => {return a + 1}"], + }, + { + index: 20, + input: `(a, b) => {return a + b}`, + expected: ["(a, b) => {return a + b}"], + }, + ]; + test.each(cases.map((x) => [x.index, x.input, x.expected]))( + "test case %d", + (_, input, expected) => { + const result = argsStringToArray(input as string); + expect(result).toStrictEqual(expected); + }, + ); +}); diff --git a/app/client/src/components/editorComponents/ActionCreator/Fields.tsx b/app/client/src/components/editorComponents/ActionCreator/Fields.tsx index 8a91221d5c..e562f3026b 100644 --- a/app/client/src/components/editorComponents/ActionCreator/Fields.tsx +++ b/app/client/src/components/editorComponents/ActionCreator/Fields.tsx @@ -73,7 +73,7 @@ const NAVIGATION_TARGET_FIELD_OPTIONS = [ }, ]; -export const FUNC_ARGS_REGEX = /((["][^"]*["])|([\[].*[\]])|([\{].*[\}])|(['][^']*['])|([\(].*[\)[=][>][{].*[}])|([^'",][^,"+]*[^'",]*))*/gi; +export const FUNC_ARGS_REGEX = /((["][^"]*["])|([\[][\s\S]*[\]])|([\{][\s\S]*[\}])|(['][^']*['])|([\(][\s\S]*[\)][ ]*=>[ ]*[{][\s\S]*[}])|([^'",][^,"+]*[^'",]*))*/gi; export const ACTION_TRIGGER_REGEX = /^{{([\s\S]*?)\(([\s\S]*?)\)}}$/g; //Old Regex:: /\(\) => ([\s\S]*?)(\([\s\S]*?\))/g; export const ACTION_ANONYMOUS_FUNC_REGEX = /\(\) => (({[\s\S]*?})|([\s\S]*?)(\([\s\S]*?\)))/g; @@ -134,7 +134,7 @@ export const JSToString = (js: string): string => { .join(""); }; -const argsStringToArray = (funcArgs: string): string[] => { +export const argsStringToArray = (funcArgs: string): string[] => { const argsplitMatches = [...funcArgs.matchAll(FUNC_ARGS_REGEX)]; const arr: string[] = []; let isPrevUndefined = true;