diff --git a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Dropdown_spec.js b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Dropdown_spec.js index 1d550d9ca0..3d462ac092 100644 --- a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Dropdown_spec.js +++ b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/DisplayWidgets/Dropdown_spec.js @@ -8,9 +8,6 @@ describe("Dropdown Widget Functionality", function() { before(() => { cy.addDsl(dsl); }); - beforeEach(() => { - cy.wait(7000); - }); it("Add new dropdown widget", () => { cy.get(explorer.addWidget).click(); @@ -93,6 +90,50 @@ describe("Dropdown Widget Functionality", function() { cy.get(formWidgetsPage.dropdownDefaultButton).should("contain", "Blue"); }); + it("should check that special strings are parsed as string in default value", () => { + cy.openPropertyPane("selectwidget"); + cy.updateCodeInput( + ".t--property-control-options", + `[{ + "label": "Blue", + "value": "null" + }, + { + "label": "Green", + "value": 100 + }, + { + "label": "Red", + "value": "120" + }]`, + ); + cy.updateCodeInput(".t--property-control-defaultvalue", "null"); + cy.get(".t--property-control-defaultvalue .t--codemirror-has-error").should( + "not.exist", + ); + cy.get(formWidgetsPage.dropdownDefaultButton).should("contain", "Blue"); + + cy.openPropertyPane("selectwidget"); + cy.updateCodeInput(".t--property-control-defaultvalue", "120"); + cy.get(".t--property-control-defaultvalue .t--codemirror-has-error").should( + "not.exist", + ); + cy.get(formWidgetsPage.dropdownDefaultButton).should("contain", "Red"); + + cy.openPropertyPane("selectwidget"); + cy.updateCodeInput(".t--property-control-defaultvalue", "{{ 100 }}"); + cy.get(".t--property-control-defaultvalue .t--codemirror-has-error").should( + "not.exist", + ); + cy.get(formWidgetsPage.dropdownDefaultButton).should("contain", "Green"); + + cy.openPropertyPane("selectwidget"); + cy.updateCodeInput(".t--property-control-defaultvalue", "{{ null }}"); + cy.get(".t--property-control-defaultvalue .t--codemirror-has-error").should( + "exist", + ); + }); + it("Dropdown Functionality To Check disabled Widget", function() { cy.openPropertyPane("selectwidget"); // Disable the visible JS diff --git a/app/client/src/widgets/SelectWidget/widget/index.test.tsx b/app/client/src/widgets/SelectWidget/widget/index.test.tsx index 9d697ce970..6bc3a069e4 100644 --- a/app/client/src/widgets/SelectWidget/widget/index.test.tsx +++ b/app/client/src/widgets/SelectWidget/widget/index.test.tsx @@ -13,6 +13,32 @@ describe("defaultOptionValueValidation - ", () => { messages: [""], }); }); + it("should get tested with number", () => { + const testValues = [ + [ + "1", + { + isValid: true, + parsed: "1", + messages: [""], + }, + ], + [ + 1, + { + isValid: true, + parsed: 1, + messages: [""], + }, + ], + ]; + + testValues.forEach(([input, expected]) => { + expect( + defaultOptionValueValidation(input, {} as SelectWidgetProps, _), + ).toEqual(expected); + }); + }); it("should get tested with simple string", () => { const input = "green"; @@ -43,6 +69,40 @@ describe("defaultOptionValueValidation - ", () => { messages: [""], }); }); + it("should get tested with valid strings", () => { + const testValues = [ + [ + "undefined", + { + isValid: true, + parsed: "undefined", + messages: [""], + }, + ], + [ + "null", + { + isValid: true, + parsed: "null", + messages: [""], + }, + ], + [ + "true", + { + isValid: true, + parsed: "true", + messages: [""], + }, + ], + ]; + + testValues.forEach(([input, expected]) => { + expect( + defaultOptionValueValidation(input, {} as SelectWidgetProps, _), + ).toEqual(expected); + }); + }); it("should get tested with invalid values", () => { const testValues = [ @@ -52,7 +112,7 @@ describe("defaultOptionValueValidation - ", () => { isValid: false, parsed: {}, messages: [ - `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`, + `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`, ], }, ], @@ -62,7 +122,7 @@ describe("defaultOptionValueValidation - ", () => { isValid: false, parsed: {}, messages: [ - `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`, + `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`, ], }, ], @@ -72,7 +132,7 @@ describe("defaultOptionValueValidation - ", () => { isValid: false, parsed: {}, messages: [ - `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`, + `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`, ], }, ], @@ -82,7 +142,7 @@ describe("defaultOptionValueValidation - ", () => { isValid: false, parsed: {}, messages: [ - `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`, + `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`, ], }, ], @@ -94,7 +154,7 @@ describe("defaultOptionValueValidation - ", () => { isValid: false, parsed: {}, messages: [ - `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`, + `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`, ], }, ], diff --git a/app/client/src/widgets/SelectWidget/widget/index.tsx b/app/client/src/widgets/SelectWidget/widget/index.tsx index c59f561883..a06e690aad 100644 --- a/app/client/src/widgets/SelectWidget/widget/index.tsx +++ b/app/client/src/widgets/SelectWidget/widget/index.tsx @@ -11,12 +11,12 @@ import { import { EvaluationSubstitutionType } from "entities/DataTree/dataTreeFactory"; import { MinimumPopupRows, GRID_DENSITY_MIGRATION_V1 } from "widgets/constants"; import { AutocompleteDataType } from "utils/autocomplete/TernServer"; -import { findIndex, isArray, isNumber, isString } from "lodash"; +import { findIndex, isArray, isNumber, isString, LoDashStatic } from "lodash"; export function defaultOptionValueValidation( value: unknown, props: SelectWidgetProps, - _: any, + _: LoDashStatic, ): ValidationResponse { let isValid; let parsed; @@ -40,7 +40,10 @@ export function defaultOptionValueValidation( */ if (typeof value === "string") { try { - value = JSON.parse(value); + const parsedValue = JSON.parse(value); + if (_.isObject(parsedValue)) { + value = parsedValue; + } } catch (e) {} } @@ -53,7 +56,7 @@ export function defaultOptionValueValidation( } else { isValid = false; parsed = {}; - message = `value does not evaluate to type: string | { "label": "label1", "value": "value1" }`; + message = `value does not evaluate to type: string | number | { "label": "label1", "value": "value1" }`; } return {