Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

2.6 KB
Raw Blame
import { describe, expect, it } from "vite-plus/test";
import {
  type CurrentCustomField,
  resolveFieldUpdate,
} from "../../../apps/api/src/custom-field/resolve-field-update";

const dropdown: CurrentCustomField = {
  name: "Size",
  type: "dropdown",
  required: false,
  defaultValue: "Medium",
  options: ["Small", "Medium", "Large"],
  hiddenOptions: [],
};

describe("resolveFieldUpdate", () => {
  it("keeps what the request leaves out", () => {
    expect(resolveFieldUpdate(dropdown, { name: "  T-shirt size " })).toEqual({
      ...dropdown,
      name: "T-shirt size",
      removedOptions: [],
    });
  });

  it("preserves hidden options when editing unrelated fields", () => {
    const field = { ...dropdown, hiddenOptions: ["Large"] };

    expect(resolveFieldUpdate(field, { name: "  T-shirt size " })).toEqual({
      ...field,
      name: "T-shirt size",
      removedOptions: [],
    });
  });

  it("rejects type changes other than dropdown and multiselect", () => {
    expect(() =>
      resolveFieldUpdate(
        { ...dropdown, type: "text", options: null, defaultValue: null },
        { type: "number" },
      ),
    ).toThrow("can't change");
  });

  it("rejects a blank name", () => {
    expect(() => resolveFieldUpdate(dropdown, { name: "  " })).toThrow(
      "Name cannot be empty",
    );
  });

  it("reports removed options and checks the default against new options", () => {
    expect(
      resolveFieldUpdate(dropdown, { options: ["Medium", "XL", "Medium"] }),
    ).toMatchObject({
      options: ["Medium", "XL"],
      removedOptions: ["Small", "Large"],
    });
    expect(() =>
      resolveFieldUpdate(dropdown, { options: ["Small", "Large"] }),
    ).toThrow("Default value must be one of the dropdown options");
  });

  it("converts the default when switching to multiselect", () => {
    expect(resolveFieldUpdate(dropdown, { type: "multiselect" })).toMatchObject(
      { type: "multiselect", defaultValue: '["Medium"]' },
    );
  });

  it("clears the default with null and requires one for required fields", () => {
    expect(
      resolveFieldUpdate(dropdown, { defaultValue: null }).defaultValue,
    ).toBeNull();
    expect(() =>
      resolveFieldUpdate(dropdown, { defaultValue: "", required: true }),
    ).toThrow("Required fields must have a default value");
  });

  it("rejects options on fields without options", () => {
    expect(() =>
      resolveFieldUpdate(
        { ...dropdown, type: "text", options: null, defaultValue: null },
        { options: ["a"] },
      ),
    ).toThrow("Only dropdown and multiselect fields have options");
  });
});