From ae5801faa0417aaac591202f69a9de635a77ae98 Mon Sep 17 00:00:00 2001 From: PrasadMadine Date: Fri, 13 Sep 2024 10:12:54 +0530 Subject: [PATCH 1/4] fix: bug toast message for when export default is not present in js object-36185 --- .../JSFunctionExecution_spec.ts | 12 +++++++ app/client/src/sagas/EvalErrorHandler.ts | 6 +++- .../src/workers/Evaluation/JSObject/index.ts | 31 +++++++++++++------ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/client/cypress/e2e/Regression/ServerSide/JsFunctionExecution/JSFunctionExecution_spec.ts b/app/client/cypress/e2e/Regression/ServerSide/JsFunctionExecution/JSFunctionExecution_spec.ts index 201b69d0e61b..bcbad3de8f85 100644 --- a/app/client/cypress/e2e/Regression/ServerSide/JsFunctionExecution/JSFunctionExecution_spec.ts +++ b/app/client/cypress/e2e/Regression/ServerSide/JsFunctionExecution/JSFunctionExecution_spec.ts @@ -578,4 +578,16 @@ return "yes";`; entityType: entityItems.JSObject, }); }); + it("11. Should throw an error when JS Object is empty", () => { + const jsObjectEmptyToastMessage = + "JS object must contain 'export default'."; + jsEditor.CreateJSObject(` `, { + paste: true, + completeReplace: true, + toRun: false, + prettify: false, + }); + + agHelper.AssertContains(jsObjectEmptyToastMessage); + }); }); diff --git a/app/client/src/sagas/EvalErrorHandler.ts b/app/client/src/sagas/EvalErrorHandler.ts index ee33f00ada4a..8c3a1a01f27b 100644 --- a/app/client/src/sagas/EvalErrorHandler.ts +++ b/app/client/src/sagas/EvalErrorHandler.ts @@ -278,7 +278,11 @@ export function* evalErrorHandler( break; } case EvalErrorTypes.PARSE_JS_ERROR: { - toast.show(`${error.message} at: ${error.context?.entity.name}`, { + let errorMessage = error.message; + if (!!error.context) { + errorMessage = `${error.message} at: ${error.context?.propertyPath}`; + } + toast.show(errorMessage, { kind: "error", }); AppsmithConsole.error({ diff --git a/app/client/src/workers/Evaluation/JSObject/index.ts b/app/client/src/workers/Evaluation/JSObject/index.ts index e826dfbd5a64..f8d4ee4443c7 100644 --- a/app/client/src/workers/Evaluation/JSObject/index.ts +++ b/app/client/src/workers/Evaluation/JSObject/index.ts @@ -219,15 +219,28 @@ export function saveResolvedFunctionsAndJSUpdates( } if (!correctFormat && !isUndefined(entity.body)) { - const errors = { - type: EvalErrorTypes.PARSE_JS_ERROR, - context: { - entity: entity, - propertyPath: entityName + ".body", - }, - message: "Start object with export default", - }; - dataTreeEvalRef.errors.push(errors); + if (entity.body.trim() !== "") { + const errors = { + type: EvalErrorTypes.PARSE_JS_ERROR, + context: { + entity: entity, + propertyPath: entityName + ".body", + }, + message: "Start object with export default", + }; + dataTreeEvalRef.errors.push(errors); + } else { + const errors = { + type: EvalErrorTypes.PARSE_JS_ERROR, + context: { + entity: entity, + propertyPath: entityName, + }, + message: "JS object must contain 'export default'.", + show: false, + }; + dataTreeEvalRef.errors.push(errors); + } } return jsUpdates; From 9e6364c676557bc426645910e21223b11a04ad34 Mon Sep 17 00:00:00 2001 From: PrasadMadine Date: Fri, 13 Sep 2024 11:42:56 +0530 Subject: [PATCH 2/4] fix: removed at:JS Object path from toast message --- app/client/src/sagas/EvalErrorHandler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/client/src/sagas/EvalErrorHandler.ts b/app/client/src/sagas/EvalErrorHandler.ts index 8c3a1a01f27b..c49ff576fad2 100644 --- a/app/client/src/sagas/EvalErrorHandler.ts +++ b/app/client/src/sagas/EvalErrorHandler.ts @@ -280,13 +280,13 @@ export function* evalErrorHandler( case EvalErrorTypes.PARSE_JS_ERROR: { let errorMessage = error.message; if (!!error.context) { - errorMessage = `${error.message} at: ${error.context?.propertyPath}`; + errorMessage = `${error.message}`; } toast.show(errorMessage, { kind: "error", }); AppsmithConsole.error({ - text: `${error.message} at: ${error.context?.propertyPath}`, + text: `${error.message}`, }); break; } From 4161ef09bac35258f16443ac8dd55c69970c4d59 Mon Sep 17 00:00:00 2001 From: PrasadMadine Date: Tue, 17 Sep 2024 08:56:54 +0530 Subject: [PATCH 3/4] fix: Added unit test cases and added raw text to messages file --- app/client/src/ce/constants/messages.ts | 4 ++ .../src/workers/Evaluation/JSObject/index.ts | 9 +++- .../src/workers/Evaluation/JSObject/test.ts | 47 ++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/client/src/ce/constants/messages.ts b/app/client/src/ce/constants/messages.ts index 8407a185902a..de7134a05899 100644 --- a/app/client/src/ce/constants/messages.ts +++ b/app/client/src/ce/constants/messages.ts @@ -2502,3 +2502,7 @@ export const EMPTY_DATASOURCE_TOOLTIP_SIDEBUTTON = () => "Create a datasource to power your app with data."; export const FIELD_REQUIRED_MESSAGE = () => `This field is required`; +export const EMPTY_JS_OBJECT_ERROR_MESSAGE = () => + "JS object must contain 'export default'."; +export const JS_OBJECT_DEFAULT_EXPORT_ERROR_MESSAGE = () => + "Start object with export default"; diff --git a/app/client/src/workers/Evaluation/JSObject/index.ts b/app/client/src/workers/Evaluation/JSObject/index.ts index f8d4ee4443c7..7caa48e027de 100644 --- a/app/client/src/workers/Evaluation/JSObject/index.ts +++ b/app/client/src/workers/Evaluation/JSObject/index.ts @@ -21,6 +21,11 @@ import JSObjectCollection from "./Collection"; import ExecutionMetaData from "../fns/utils/ExecutionMetaData"; import { jsPropertiesState } from "./jsPropertiesState"; import { getFixedTimeDifference } from "workers/common/DataTreeEvaluator/utils"; +import { + createMessage, + EMPTY_JS_OBJECT_ERROR_MESSAGE, + JS_OBJECT_DEFAULT_EXPORT_ERROR_MESSAGE, +} from "ee/constants/messages"; /** * Here we update our unEvalTree according to the change in JSObject's body @@ -226,7 +231,7 @@ export function saveResolvedFunctionsAndJSUpdates( entity: entity, propertyPath: entityName + ".body", }, - message: "Start object with export default", + message: createMessage(JS_OBJECT_DEFAULT_EXPORT_ERROR_MESSAGE), }; dataTreeEvalRef.errors.push(errors); } else { @@ -236,7 +241,7 @@ export function saveResolvedFunctionsAndJSUpdates( entity: entity, propertyPath: entityName, }, - message: "JS object must contain 'export default'.", + message: createMessage(EMPTY_JS_OBJECT_ERROR_MESSAGE), show: false, }; dataTreeEvalRef.errors.push(errors); diff --git a/app/client/src/workers/Evaluation/JSObject/test.ts b/app/client/src/workers/Evaluation/JSObject/test.ts index 72624198d9be..1d5cf31e7d55 100644 --- a/app/client/src/workers/Evaluation/JSObject/test.ts +++ b/app/client/src/workers/Evaluation/JSObject/test.ts @@ -1,5 +1,8 @@ import type { ConfigTree, UnEvalTree } from "entities/DataTree/dataTreeTypes"; -import { getUpdatedLocalUnEvalTreeAfterJSUpdates } from "."; +import { + getUpdatedLocalUnEvalTreeAfterJSUpdates, + saveResolvedFunctionsAndJSUpdates, +} from "."; describe("updateJSCollectionInUnEvalTree", function () { it("updates async value of jsAction", () => { @@ -134,4 +137,46 @@ describe("updateJSCollectionInUnEvalTree", function () { expect(expectedResult).toStrictEqual(actualResult); }); + it("should raise empty toast message when JSObject is empty", () => { + const mockFunction = jest.fn(); + saveResolvedFunctionsAndJSUpdates( + { errors: { push: mockFunction } }, + { body: " " }, + {}, + {}, + "JSObject1", + ); + + expect(mockFunction).toBeCalled; + expect(mockFunction).toHaveBeenCalledWith({ + type: "PARSE_JS_ERROR", + context: { + entity: { body: " " }, + propertyPath: "JSObject1", + }, + message: "JS object must contain 'export default'.", + show: false, + }); + }); + it("should raise appropriate toast message based on JSObject body content", () => { + const mockFunction = jest.fn(); + + saveResolvedFunctionsAndJSUpdates( + { errors: { push: mockFunction } }, + { body: "export" }, + {}, + {}, + "JSObject1", + ); + + expect(mockFunction).toBeCalled(); + expect(mockFunction).toHaveBeenCalledWith({ + type: "PARSE_JS_ERROR", + context: { + entity: { body: "export" }, + propertyPath: "JSObject1.body", + }, + message: "Start object with export default", + }); + }); }); From 0dc86b055f52467f9a190d14099fe26a32a2481a Mon Sep 17 00:00:00 2001 From: PrasadMadine Date: Tue, 1 Oct 2024 16:19:51 +0530 Subject: [PATCH 4/4] fix: linting errors --- app/client/src/sagas/EvalErrorHandler.ts | 2 ++ app/client/src/workers/Evaluation/JSObject/index.ts | 2 ++ app/client/src/workers/Evaluation/JSObject/test.ts | 1 + 3 files changed, 5 insertions(+) diff --git a/app/client/src/sagas/EvalErrorHandler.ts b/app/client/src/sagas/EvalErrorHandler.ts index 219634bec459..7029f7c98e8f 100644 --- a/app/client/src/sagas/EvalErrorHandler.ts +++ b/app/client/src/sagas/EvalErrorHandler.ts @@ -288,9 +288,11 @@ export function* evalErrorHandler( } case EvalErrorTypes.PARSE_JS_ERROR: { let errorMessage = error.message; + if (!!error.context) { errorMessage = `${error.message}`; } + toast.show(errorMessage, { kind: "error", }); diff --git a/app/client/src/workers/Evaluation/JSObject/index.ts b/app/client/src/workers/Evaluation/JSObject/index.ts index 21f15f41194c..bdb99ac37ac6 100644 --- a/app/client/src/workers/Evaluation/JSObject/index.ts +++ b/app/client/src/workers/Evaluation/JSObject/index.ts @@ -242,6 +242,7 @@ export function saveResolvedFunctionsAndJSUpdates( }, message: createMessage(JS_OBJECT_DEFAULT_EXPORT_ERROR_MESSAGE), }; + dataTreeEvalRef.errors.push(errors); } else { const errors = { @@ -253,6 +254,7 @@ export function saveResolvedFunctionsAndJSUpdates( message: createMessage(EMPTY_JS_OBJECT_ERROR_MESSAGE), show: false, }; + dataTreeEvalRef.errors.push(errors); } } diff --git a/app/client/src/workers/Evaluation/JSObject/test.ts b/app/client/src/workers/Evaluation/JSObject/test.ts index 1d80560dcdb8..7df6d1cb431b 100644 --- a/app/client/src/workers/Evaluation/JSObject/test.ts +++ b/app/client/src/workers/Evaluation/JSObject/test.ts @@ -141,6 +141,7 @@ describe("updateJSCollectionInUnEvalTree", function () { }); it("should raise empty toast message when JSObject is empty", () => { const mockFunction = jest.fn(); + saveResolvedFunctionsAndJSUpdates( { errors: { push: mockFunction } }, { body: " " },