Merge pull request #12324 from appsmithorg/fix/default-value-for-select-widget

fix: default value consistency issues in select widget
This commit is contained in:
Tolulope Adetula 2022-04-04 06:20:01 +01:00 committed by GitHub
commit 3fb3eff92e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 12 deletions

View File

@ -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

View File

@ -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" }`,
],
},
],

View File

@ -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 {