2022-02-22 08:43:35 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
|
import { defaultOptionValueValidation, MultiSelectWidgetProps } from ".";
|
|
|
|
|
|
2022-08-03 13:17:07 +00:00
|
|
|
const props = {
|
|
|
|
|
serverSideFiltering: false,
|
|
|
|
|
options: [
|
|
|
|
|
{ label: "Blue", value: "BLUE" },
|
|
|
|
|
{ label: "Green", value: "GREEN" },
|
|
|
|
|
{ label: "Red", value: "RED" },
|
|
|
|
|
{ label: "2022", value: 2022 },
|
|
|
|
|
{ label: "true", value: "true" },
|
|
|
|
|
{ label: "null", value: "null" },
|
|
|
|
|
{ label: "undefined", value: "undefined" },
|
|
|
|
|
{ label: "1", value: "1" },
|
|
|
|
|
{ label: "2", value: "2" },
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DEFAULT_ERROR_MESSAGE =
|
|
|
|
|
"value should match: Array<string | number> | Array<{label: string, value: string | number}>";
|
|
|
|
|
const MISSING_FROM_OPTIONS =
|
|
|
|
|
"Some or all default values are missing from options. Please update the values.";
|
|
|
|
|
const MISSING_FROM_OPTIONS_AND_WRONG_FORMAT =
|
|
|
|
|
"Default value is missing in options. Please use [{label : <string | num>, value : < string | num>}] format to show default for server side data";
|
|
|
|
|
|
2022-02-22 08:43:35 +00:00
|
|
|
describe("defaultOptionValueValidation - ", () => {
|
|
|
|
|
it("should get tested with empty string", () => {
|
|
|
|
|
const input = "";
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
|
|
|
|
parsed: [],
|
|
|
|
|
messages: [""],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-22 11:03:27 +00:00
|
|
|
it("should get tested with array of strings", () => {
|
2022-02-22 08:43:35 +00:00
|
|
|
const input = ["green", "red"];
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
2022-08-03 13:17:07 +00:00
|
|
|
isValid: false,
|
2022-02-22 08:43:35 +00:00
|
|
|
parsed: input,
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [MISSING_FROM_OPTIONS],
|
2022-02-22 08:43:35 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-11 04:32:55 +00:00
|
|
|
it("should get tested with array of strings and stringified options", () => {
|
|
|
|
|
const input = ["green", "red"];
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{
|
|
|
|
|
...props,
|
|
|
|
|
options: JSON.stringify(props.options) as unknown,
|
|
|
|
|
} as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
|
|
|
|
).toEqual({
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: input,
|
|
|
|
|
messages: [MISSING_FROM_OPTIONS],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-22 11:03:27 +00:00
|
|
|
it("should get tested with a number", () => {
|
|
|
|
|
const input = 2022;
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-03-22 11:03:27 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
|
|
|
|
parsed: [input],
|
|
|
|
|
messages: [""],
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-03-23 12:08:32 +00:00
|
|
|
it("should get tested with a string", () => {
|
2022-08-03 13:17:07 +00:00
|
|
|
const inputs = [2022, "true", "null", "undefined"];
|
2022-03-23 12:08:32 +00:00
|
|
|
|
|
|
|
|
inputs.forEach((input) => {
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-03-23 12:08:32 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
|
|
|
|
parsed: [input],
|
|
|
|
|
messages: [""],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-03-22 11:03:27 +00:00
|
|
|
|
2022-02-22 08:43:35 +00:00
|
|
|
it("should get tested with array json string", () => {
|
2022-08-03 13:17:07 +00:00
|
|
|
const input = `["GREEN", "RED"]`;
|
2022-02-22 08:43:35 +00:00
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
2022-08-03 13:17:07 +00:00
|
|
|
parsed: ["GREEN", "RED"],
|
2022-02-22 08:43:35 +00:00
|
|
|
messages: [""],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should get tested with array of object json string", () => {
|
|
|
|
|
const input = `[
|
|
|
|
|
{
|
|
|
|
|
"label": "green",
|
2022-08-03 13:17:07 +00:00
|
|
|
"value": "GREEN"
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"label": "red",
|
2022-08-03 13:17:07 +00:00
|
|
|
"value": "RED"
|
2022-02-22 08:43:35 +00:00
|
|
|
}
|
|
|
|
|
]`;
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
|
|
|
|
parsed: [
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
2022-08-03 13:17:07 +00:00
|
|
|
value: "GREEN",
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "red",
|
2022-08-03 13:17:07 +00:00
|
|
|
value: "RED",
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
messages: [""],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-23 12:08:32 +00:00
|
|
|
it("should get tested with comma separated strings", () => {
|
2022-08-03 13:17:07 +00:00
|
|
|
const input = "GREEN, RED";
|
2022-03-23 12:08:32 +00:00
|
|
|
const input2 = "1, 2";
|
2022-02-22 08:43:35 +00:00
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
2022-08-03 13:17:07 +00:00
|
|
|
parsed: ["GREEN", "RED"],
|
2022-02-22 08:43:35 +00:00
|
|
|
messages: [""],
|
|
|
|
|
});
|
2022-03-23 12:08:32 +00:00
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input2,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-03-23 12:08:32 +00:00
|
|
|
).toEqual({
|
|
|
|
|
isValid: true,
|
|
|
|
|
parsed: ["1", "2"],
|
|
|
|
|
messages: [""],
|
|
|
|
|
});
|
2022-02-22 08:43:35 +00:00
|
|
|
});
|
|
|
|
|
|
2022-08-03 13:17:07 +00:00
|
|
|
it("should get tested with string and ServerSide filtering on", () => {
|
|
|
|
|
const input = "YELLOW";
|
2022-02-22 08:43:35 +00:00
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props, serverSideFiltering: true } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
2022-08-03 13:17:07 +00:00
|
|
|
isValid: false,
|
|
|
|
|
parsed: ["YELLOW"],
|
|
|
|
|
messages: [MISSING_FROM_OPTIONS_AND_WRONG_FORMAT],
|
2022-02-22 08:43:35 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should get tested with simple string", () => {
|
|
|
|
|
const input = `{"green"`;
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
2022-08-03 13:17:07 +00:00
|
|
|
isValid: false,
|
2022-02-22 08:43:35 +00:00
|
|
|
parsed: [`{"green"`],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [MISSING_FROM_OPTIONS],
|
2022-02-22 08:43:35 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-03 13:17:07 +00:00
|
|
|
it("should get tested with array of label, value and serverside filtering off", () => {
|
2022-02-22 08:43:35 +00:00
|
|
|
const input = [
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
|
|
|
|
value: "green",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "red",
|
|
|
|
|
value: "red",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual({
|
2022-08-03 13:17:07 +00:00
|
|
|
isValid: false,
|
2022-02-22 08:43:35 +00:00
|
|
|
parsed: [
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
|
|
|
|
value: "green",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "red",
|
|
|
|
|
value: "red",
|
|
|
|
|
},
|
|
|
|
|
],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [MISSING_FROM_OPTIONS],
|
2022-02-22 08:43:35 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should get tested with array of invalid values", () => {
|
|
|
|
|
const testValues = [
|
|
|
|
|
[
|
|
|
|
|
undefined,
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
null,
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
true,
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[undefined],
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[true],
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
["green", "green"],
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
|
|
|
|
messages: ["values must be unique. Duplicate values found"],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
|
|
|
|
value: "green",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
|
|
|
|
value: "green",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
|
|
|
|
messages: ["path:value must be unique. Duplicate values found"],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "green",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
isValid: false,
|
|
|
|
|
parsed: [],
|
2022-08-03 13:17:07 +00:00
|
|
|
messages: [DEFAULT_ERROR_MESSAGE],
|
2022-02-22 08:43:35 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
testValues.forEach(([input, expected]) => {
|
|
|
|
|
expect(
|
2022-08-03 13:17:07 +00:00
|
|
|
defaultOptionValueValidation(
|
|
|
|
|
input,
|
|
|
|
|
{ ...props } as MultiSelectWidgetProps,
|
|
|
|
|
_,
|
|
|
|
|
),
|
2022-02-22 08:43:35 +00:00
|
|
|
).toEqual(expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|