Skip to content
Snippets Groups Projects
Unverified Commit 7049bb5b authored by Uladzimir Havenchyk's avatar Uladzimir Havenchyk Committed by GitHub
Browse files

Do not count empty lines and duplicates in action option editor (#32507)

* Do not count empty lines and duplicates in action option editor

* fixup! Do not count empty lines and duplicates in action option editor

* Add unit tests for helper function
parent 2b65bf75
Branches
Tags
No related merge requests found
......@@ -16,22 +16,36 @@ import {
} from "./OptionEditor.styled";
const optionsToText = (options: FieldValueOptions) => options.join("\n");
const textToOptions = (text: string): FieldValueOptions =>
text.split("\n").map(option => option.trim());
export const textToOptions = (text: string): FieldValueOptions => {
const options = text
.trim()
.split("\n")
.map(option => option.trim())
.filter(Boolean);
const uniqueOptions = [...new Set(options)];
function cleanOptions(options: FieldValueOptions, fieldType: FieldType) {
return uniqueOptions;
};
function transformOptionsIfNeeded(
options: FieldValueOptions,
fieldType: FieldType,
) {
if (fieldType === "number") {
return options.map(option => Number(option));
}
return options;
}
function getValidationError(options: FieldValueOptions, fieldType: FieldType) {
if (fieldType === "number") {
const isValid = options.every(option => !Number.isNaN(option));
return isValid ? undefined : t`Invalid number format`;
if (fieldType !== "number") {
return;
}
return;
const isValid = options.every(option => !Number.isNaN(option));
return isValid ? undefined : t`Invalid number format`;
}
export interface OptionEditorProps {
......@@ -72,8 +86,13 @@ export const OptionPopover = ({
const handleSave = (closePopover: () => void) => {
setError(null);
const nextOptions = cleanOptions(textToOptions(text), fieldType);
const nextOptions = transformOptionsIfNeeded(
textToOptions(text),
fieldType,
);
const error = getValidationError(nextOptions, fieldType);
if (error) {
setError(error);
} else {
......
......@@ -2,7 +2,11 @@ import { useState } from "react";
import userEvent from "@testing-library/user-event";
import { render, screen, getIcon } from "__support__/ui";
import type { FieldType, FieldValueOptions } from "metabase-types/api";
import { OptionPopover, OptionEditorProps } from "./OptionEditor";
import {
OptionPopover,
OptionEditorProps,
textToOptions,
} from "./OptionEditor";
async function baseSetup({
fieldType = "string",
......@@ -103,5 +107,45 @@ describe("OptionEditor", () => {
expect(onChange).toHaveBeenCalledWith([1, 2]);
});
it("should omit empty lines and duplicates", async () => {
const { input, saveButton, onChange } = await baseSetup({
fieldType: "number",
options: [],
});
userEvent.type(input, "1\n2\n\n2\n\n1\n\n");
userEvent.click(saveButton);
expect(onChange).toHaveBeenCalledWith([1, 2]);
});
describe("given string field type", () => {
it("should omit empty lines and duplicates", async () => {
const { input, saveButton, onChange } = await baseSetup({
fieldType: "string",
options: [],
});
userEvent.type(input, "1\n2\n\n2\n\n1\n\n");
userEvent.click(saveButton);
expect(onChange).toHaveBeenCalledWith(["1", "2"]);
});
});
});
});
describe("textToOptions", () => {
it("should filter duplicates", () => {
const input = "1\n2\n1\n1\n2";
expect(textToOptions(input)).toEqual(["1", "2"]);
});
it("should filter empty values and trim empty space", () => {
const input = " \n 1\n2 \n\n\n ";
expect(textToOptions(input)).toEqual(["1", "2"]);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment