From 06bf4696b4ab1aa77f0d82013b5e3740170d2276 Mon Sep 17 00:00:00 2001 From: Hetu Nandu Date: Mon, 28 Feb 2022 15:05:43 +0530 Subject: [PATCH] fix: Promises in Table buttons (#11239) --- .../Chart_Widget_Loading_spec.js | 2 +- .../Table_Widget_Add_button_spec.js | 32 +++++++++++++++++-- .../src/widgets/TableWidget/widget/index.tsx | 2 +- app/client/src/workers/evaluate.ts | 15 +++++++-- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Chart_Widget_Loading_spec.js b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Chart_Widget_Loading_spec.js index 623ff378d5..18645ee9a5 100644 --- a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Chart_Widget_Loading_spec.js +++ b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Chart_Widget_Loading_spec.js @@ -91,7 +91,7 @@ describe("Chart Widget Skeleton Loading Functionality", function() { cy.reload(); //Step12: - cy.wait(2000); + cy.wait(1000); cy.get(".t--widget-chartwidget div[class*='bp3-skeleton']").should("exist"); //Step13: diff --git a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Table_Widget_Add_button_spec.js b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Table_Widget_Add_button_spec.js index be91bea9cf..7934a50b6f 100644 --- a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Table_Widget_Add_button_spec.js +++ b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Table_Widget_Add_button_spec.js @@ -40,8 +40,36 @@ describe("Table Widget property pane feature validation", function() { .last() .invoke("text") .then((text) => { - const someText = text; - expect(someText).to.equal("Successful tobias.funke@reqres.in"); + expect(text).to.equal("Successful tobias.funke@reqres.in"); + }); + + // Open column details of "id". + cy.editColumn("id"); + + cy.get(widgetsPage.toggleOnClick).click({ force: true }); + cy.get(".t--property-control-onclick").then(($el) => { + cy.updateCodeInput( + $el, + "{{showAlert('Successful' + currentRow.email).then(() => showAlert('second alert')) }}", + ); + }); + + cy.get(commonlocators.editPropBackButton).click({ + force: true, + }); + + // Validating the button action by clicking + cy.get(widgetsPage.tableBtn) + .last() + .click({ force: true }); + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(3000); + + cy.get(widgetsPage.toastActionText) + .last() + .invoke("text") + .then((text) => { + expect(text).to.equal("second alert"); }); }); it("2. Table Button color validation", function() { diff --git a/app/client/src/widgets/TableWidget/widget/index.tsx b/app/client/src/widgets/TableWidget/widget/index.tsx index 56da7ad78e..3f6b8e6fed 100644 --- a/app/client/src/widgets/TableWidget/widget/index.tsx +++ b/app/client/src/widgets/TableWidget/widget/index.tsx @@ -893,7 +893,7 @@ class TableWidget extends BaseWidget { ); const { jsSnippets } = getDynamicBindings(action); const modifiedAction = jsSnippets.reduce((prev: string, next: string) => { - return prev + `{{(currentRow) => { ${next} }}} `; + return prev + `{{(currentRow) => { return (${next}) }}} `; }, ""); if (modifiedAction) { super.executeAction({ diff --git a/app/client/src/workers/evaluate.ts b/app/client/src/workers/evaluate.ts index 3c764913fb..ece0f63e59 100644 --- a/app/client/src/workers/evaluate.ts +++ b/app/client/src/workers/evaluate.ts @@ -23,6 +23,7 @@ export type EvalResult = { export enum EvaluationScriptType { EXPRESSION = "EXPRESSION", ANONYMOUS_FUNCTION = "ANONYMOUS_FUNCTION", + ASYNC_ANONYMOUS_FUNCTION = "ASYNC_ANONYMOUS_FUNCTION", TRIGGERS = "TRIGGERS", } @@ -44,6 +45,14 @@ export const EvaluationScripts: Record = { } callback(${ScriptTemplate}) `, + [EvaluationScriptType.ASYNC_ANONYMOUS_FUNCTION]: ` + async function callback (script) { + const userFunction = script; + const result = await userFunction?.apply(THIS_CONTEXT, ARGUMENTS); + return result; + } + callback(${ScriptTemplate}) + `, [EvaluationScriptType.TRIGGERS]: ` async function closedFunction () { const result = await ${ScriptTemplate}; @@ -58,9 +67,11 @@ const getScriptType = ( isTriggerBased = false, ): EvaluationScriptType => { let scriptType = EvaluationScriptType.EXPRESSION; - if (evalArgumentsExist) { + if (evalArgumentsExist && isTriggerBased) { + scriptType = EvaluationScriptType.ASYNC_ANONYMOUS_FUNCTION; + } else if (evalArgumentsExist && !isTriggerBased) { scriptType = EvaluationScriptType.ANONYMOUS_FUNCTION; - } else if (isTriggerBased) { + } else if (isTriggerBased && !evalArgumentsExist) { scriptType = EvaluationScriptType.TRIGGERS; } return scriptType;