Files
calendar/packages/app-store/routing-forms/lib/transformResponse.test.ts
T
cf76cf7aae feat: Multiple Options Paste and Routing Form improvements related to Select fields (#15706)
* Add handlePaste function and integrate it with options fields

* Prevents function running if user does not paste a list, allows list items to be pasted without deleting following items

* Subs useState for rhf useWatch to fix issues with multiple form fields

* Sets options array as optional property in zod schema

* Assigns default value to options array to fix possibly undefined type errors

* Sets placedholder property as optional

* maps selectText to options on mount

* Removes comma & semicolon delimiting for pasted lists (new line only) & defines regex as a constant

* Sets default value of placeholder to empty string, provides default fallback for component text field if placeholder is omitted.

* Fix type error on placeholder

* Extracts handlePaste to service function & tests thoroughly

* Some improvements and tests arent needed

* More fixes

* Fix reporting as well

* TS fixes

* Do transforms to and fro from label

* Add unit tests

* Unit test reporting

* Test Improvements

* Self review feedback addressed

---------

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
2024-08-22 13:38:46 +00:00

145 lines
4.6 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { getFieldResponseForJsonLogic } from "./transformResponse";
describe("getFieldResponseForJsonLogic", () => {
it("should return an empty string if value is undefined", () => {
const field = { type: "text", options: undefined };
const value = undefined;
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toBe("");
});
it("should transform value for a number type field to number", () => {
const field = { type: "number", options: undefined };
const value = "123";
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toBe(123);
});
describe("multiselect", () => {
describe("non-legacy options", () => {
it("should return option ids for the field if the value is an array of option ids", () => {
const field = {
type: "multiselect",
options: [
{ id: "1", label: "Option 1" },
{ id: "2", label: "Option 2" },
],
};
const value = ["1", "2"];
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toEqual(["1", "2"]);
});
it("should return ids if matching labels are provided in value", () => {
const field = {
type: "multiselect",
options: [
{ id: "1", label: "Option 1" },
{ id: "2", label: "Option 2" },
],
};
const value = ["Option 1", "Option 2"];
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toEqual(["1", "2"]);
});
it("should return the value as it is if it doesn't match any of the labels or option ids", () => {
const field = {
type: "multiselect",
options: [
{ id: "1", label: "Option 1" },
{ id: "2", label: "Option 2" },
],
};
const value = ["Option 10", "Option 11"];
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toEqual(["Option 10", "Option 11"]);
});
});
describe("legacy options", () => {
it("should return what is given for a field that has legacy options but the values don't match labels", () => {
const field = {
type: "multiselect",
options: [
{ id: null, label: "Option 1" },
{ id: null, label: "Option 2" },
],
};
const value = ["1", "2"];
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toEqual(["1", "2"]);
});
it("should return matching labels for a field that has legacy options", () => {
const field = {
type: "multiselect",
options: [
{ id: null, label: "Option 1" },
{ id: null, label: "Option 2" },
],
};
const value = ["Option 1", "Option 2"];
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toEqual(["Option 1", "Option 2"]);
});
it("should handle multiselect field with legacy options", () => {
const field = {
type: "multiselect",
options: [
{ label: "Option 1", id: null },
{ label: "Option 2", id: null },
],
};
const value = ["Option 1", "Option 2"];
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toEqual(["Option 1", "Option 2"]);
});
});
});
describe("select", () => {
it("should handle select field with options", () => {
const field = {
type: "select",
options: [
{ id: "1", label: "Option 1" },
{ id: "2", label: "Option 2" },
],
};
const value = "1";
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toBe("1");
});
it("should handle select field with legacy options", () => {
const field = {
type: "select",
options: [
{ label: "Option 1", id: null },
{ label: "Option 2", id: null },
],
};
const value = "Option 1";
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toBe("Option 1");
});
it("should handle select field with number as string", () => {
const field = {
type: "select",
options: [
{ id: "1", label: "Option 1" },
{ id: "2", label: "Option 2" },
],
};
const value = 1;
const result = getFieldResponseForJsonLogic({ field, value });
expect(result).toBe("1");
});
});
});