diff --git a/src/mixins/computedFields.js b/src/mixins/computedFields.js index d34d969e..38878abf 100644 --- a/src/mixins/computedFields.js +++ b/src/mixins/computedFields.js @@ -21,8 +21,7 @@ export default { } else { // Create a new function with the expression and bind the data context // eslint-disable-next-line no-new-func - value = new Function(expression).bind(data); - return { result: value(), error: null }; + value = new Function(expression).bind(data)(); // NOSONAR. Intentional: author-defined code, not end-user data. } if (value instanceof Date) { diff --git a/tests/e2e/specs/ComputedFields.spec.js b/tests/e2e/specs/ComputedFields.spec.js index 2ff388ff..369c34bd 100644 --- a/tests/e2e/specs/ComputedFields.spec.js +++ b/tests/e2e/specs/ComputedFields.spec.js @@ -290,4 +290,140 @@ describe("Computed fields", { testIsolation: true }, () => { "Hello World" ); }); + + it("Normalizes new Date() in javascript computed field for Date Picker without infinite loop", () => { + setupConsoleStubs({ log: "consoleLog" }); + cy.openAcordeon("collapse-2"); + + // Add a Date Picker without configuring dataFormat (reproduces ticket screen) + cy.get("[data-cy=controls-FormDatePicker]").drag( + "[data-cy=screen-drop-zone]", + { position: "bottom" } + ); + + // Create a javascript calculated property returning new Date() for the Date Picker + cy.get('[data-cy="topbar-calcs"]').click(); + cy.get('[data-cy="calcs-add-property"]').click(); + cy.get('[data-cy="calcs-property-name"]') + .clear() + .type("form_date_picker_1"); + cy.get('[data-cy="calcs-property-description"]') + .clear() + .type("Current date"); + cy.get('[data-cy="calcs-switch-javascript"]').click(); + cy.setVueComponentValue( + '[data-cy="calcs-property-javascript"]', + "return new Date()" + ); + cy.get('[data-cy="calcs-button-save"]').click(); + cy.get('[data-cy="calcs-modal"] .close').click(); + cy.get('[data-cy="calcs-modal"]').should("not.be.visible"); + + // Preview: date is displayed in the field and stored as an ISO string + cy.get("[data-cy=mode-preview]").click(); + cy.get("[data-cy=preview-content]").should("be.visible"); + cy.get( + '[data-cy=preview-content] [data-cy="screen-field-form_date_picker_1"] input' + ) + .invoke("val") + .should("not.be.empty"); + cy.get("#screen-builder-container").should(($div) => { + const value = $div[0].__vue__.previewData.form_date_picker_1; + expect(value).to.be.a("string"); + expect(value).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/); + }); + + // Switch back to Design and add a Line Input and a Submit button + cy.get("[data-cy=mode-editor]").click(); + cy.get("[data-cy=controls-FormInput]").drag( + "[data-cy=screen-element-container]", + { position: "bottom" } + ); + cy.get("[data-cy=controls-FormButton]") + .contains("Submit Button") + .drag("[data-cy=screen-element-container]:last", { position: "bottom" }); + + // Return to Preview, interact with the Line Input, and submit the form + cy.get("[data-cy=mode-preview]").click(); + cy.get("[data-cy=preview-content]").should("be.visible"); + cy.get("[data-cy=preview-content] [name=form_input_1]") + .clear() + .type("hello") + .blur(); + cy.get("[data-cy=preview-content] button.btn-primary").click(); + + cy.get( + '[data-cy=preview-content] [data-cy="screen-field-form_date_picker_1"] input' + ) + .invoke("val") + .should("not.be.empty"); + cy.get("#screen-builder-container").should(($div) => { + const data = $div[0].__vue__.previewData; + expect(data.form_input_1).to.equal("hello"); + expect(data.form_date_picker_1).to.be.a("string"); + expect(data.form_date_picker_1).to.match( + /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ + ); + }); + + // Ensure the calculated property did not enter an infinite recalculation loop + cy.get("@consoleLog").then((stub) => { + const calcRunCalls = stub + .getCalls() + .filter( + (call) => + call.args[0] === '%c✅ %cCalc "form_date_picker_1" has %cRUN' + ); + expect(calcRunCalls.length).to.be.greaterThan(0); + expect(calcRunCalls.length).to.be.lessThan(10); + }); + }); + + it("Normalizes new Date() in javascript computed field for Date Picker with Date dataFormat without infinite loop", () => { + setupConsoleStubs({ log: "consoleLog" }); + cy.openAcordeon("collapse-2"); + + cy.get("[data-cy=controls-FormDatePicker]").drag( + "[data-cy=screen-drop-zone]", + { position: "bottom" } + ); + cy.get("[data-cy=screen-element-container]").click(); + cy.setMultiselect("[data-cy=inspector-dataFormat]", "Date"); + + cy.get('[data-cy="topbar-calcs"]').click(); + cy.get('[data-cy="calcs-add-property"]').click(); + cy.get('[data-cy="calcs-property-name"]') + .clear() + .type("form_date_picker_1"); + cy.get('[data-cy="calcs-property-description"]') + .clear() + .type("Current date"); + cy.get('[data-cy="calcs-switch-javascript"]').click(); + cy.setVueComponentValue( + '[data-cy="calcs-property-javascript"]', + "return new Date()" + ); + cy.get('[data-cy="calcs-button-save"]').click(); + cy.get('[data-cy="calcs-modal"] .close').click(); + cy.get('[data-cy="calcs-modal"]').should("not.be.visible"); + + cy.get("[data-cy=mode-preview]").click(); + cy.get("[data-cy=preview-content]").should("be.visible"); + cy.get( + '[data-cy=preview-content] [data-cy="screen-field-form_date_picker_1"] input' + ) + .invoke("val") + .should("not.be.empty"); + + cy.get("@consoleLog").then((stub) => { + const calcRunCalls = stub + .getCalls() + .filter( + (call) => + call.args[0] === '%c✅ %cCalc "form_date_picker_1" has %cRUN' + ); + expect(calcRunCalls.length).to.be.greaterThan(0); + expect(calcRunCalls.length).to.be.lessThan(10); + }); + }); }); diff --git a/tests/unit/ComputedFields.spec.js b/tests/unit/ComputedFields.spec.js new file mode 100644 index 00000000..8b1d1530 --- /dev/null +++ b/tests/unit/ComputedFields.spec.js @@ -0,0 +1,62 @@ +import computedFields from "../../src/mixins/computedFields"; + +describe("computedFields.evaluateExpression", () => { + const createContext = (data = {}) => ({ + ...computedFields.methods, + getDataReference: jest.fn(() => data) + }); + + it("normalizes Date returned by javascript calculated properties to an ISO string", () => { + const context = createContext(); + const expectedIso = "2026-11-30T12:34:56.789Z"; + + const response = context.evaluateExpression( + `return new Date("${expectedIso}");`, + "javascript" + ); + + expect(response.error).toBeNull(); + expect(typeof response.result).toBe("string"); + expect(response.result).toBe(expectedIso); + }); + + it("normalizes Date returned by expression calculated properties to an ISO string", () => { + const expectedIso = "2026-11-30T12:34:56.789Z"; + const context = createContext({ + sampleDate: new Date(expectedIso) + }); + + const response = context.evaluateExpression("sampleDate", "expression"); + + expect(response.error).toBeNull(); + expect(typeof response.result).toBe("string"); + expect(response.result).toBe(expectedIso); + }); + + it("returns non-Date values from javascript calculated properties unchanged", () => { + const context = createContext({ firstName: "Ada" }); + + const response = context.evaluateExpression( + "return this.firstName.toUpperCase();", + "javascript" + ); + + expect(response).toEqual({ + result: "ADA", + error: null + }); + }); + + it("returns error object when javascript calculated property throws", () => { + const context = createContext(); + + const response = context.evaluateExpression( + 'throw new Error("boom");', + "javascript" + ); + + expect(response.result).toBeNull(); + expect(response.error).toBeInstanceOf(Error); + expect(response.error.message).toBe("boom"); + }); +});