fix: default value consistency issues in select widget

This commit is contained in:
Tolulope Adetula 2022-03-29 08:07:01 +01:00
parent 23c12f4afc
commit a12c183c12
2 changed files with 75 additions and 4 deletions

View File

@ -13,6 +13,40 @@ describe("defaultOptionValueValidation - ", () => {
messages: [""],
});
});
it("should get tested with number", () => {
const testValues = [
[
"{{1}}",
{
isValid: true,
parsed: 1,
messages: [""],
},
],
[
"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 +77,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 = [

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 {