55 lines
2.8 KiB
TypeScript
55 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { evaluateConditionalLogic, filterVisibleFields, isFieldVisible } from "./visibility.js";
|
|
import type { FormField } from "./schema.js";
|
|
|
|
function field(key: string, conditionalLogic: FormField["conditionalLogic"] = []): FormField {
|
|
return {
|
|
key,
|
|
type: "text",
|
|
label: key,
|
|
required: false,
|
|
conditionalLogic,
|
|
validationRules: {},
|
|
};
|
|
}
|
|
|
|
describe("form field visibility", () => {
|
|
it("shows fields without conditional rules", () => {
|
|
expect(isFieldVisible(field("name"), {})).toBe(true);
|
|
});
|
|
|
|
it("evaluates equality and filled operators", () => {
|
|
const f = field("phone", [
|
|
{ fieldKey: "contact_method", operator: "equals", value: "phone" },
|
|
{ fieldKey: "name", operator: "not_empty" },
|
|
]);
|
|
|
|
expect(isFieldVisible(f, { contact_method: "phone", name: "Ada" })).toBe(true);
|
|
expect(isFieldVisible(f, { contact_method: "email", name: "Ada" })).toBe(false);
|
|
});
|
|
|
|
it("filters a field list", () => {
|
|
const fields = [field("name"), field("details", [{ fieldKey: "kind", operator: "in", value: ["repair", "install"] }])];
|
|
|
|
expect(filterVisibleFields(fields, { kind: "repair" }).map((f) => f.key)).toEqual(["name", "details"]);
|
|
expect(filterVisibleFields(fields, { kind: "service" }).map((f) => f.key)).toEqual(["name"]);
|
|
});
|
|
|
|
it("evaluates negative, contains and empty operators", () => {
|
|
expect(evaluateConditionalLogic({ fieldKey: "status", operator: "not_equals", value: "closed" }, { status: "open" })).toBe(true);
|
|
expect(evaluateConditionalLogic({ fieldKey: "status", operator: "not_equals", value: "closed" }, { status: "closed" })).toBe(false);
|
|
expect(evaluateConditionalLogic({ fieldKey: "notes", operator: "contains", value: "urgent" }, { notes: "Very urgent issue" })).toBe(true);
|
|
expect(evaluateConditionalLogic({ fieldKey: "notes", operator: "contains", value: "urgent" }, { notes: "routine" })).toBe(false);
|
|
expect(evaluateConditionalLogic({ fieldKey: "optional", operator: "empty" }, { optional: "" })).toBe(true);
|
|
expect(evaluateConditionalLogic({ fieldKey: "optional", operator: "empty" }, { optional: "set" })).toBe(false);
|
|
});
|
|
|
|
it("normalizes array and scalar values for membership checks", () => {
|
|
expect(evaluateConditionalLogic({ fieldKey: "tags", operator: "equals", value: "vip,urgent" }, { tags: ["VIP", "urgent"] })).toBe(true);
|
|
expect(evaluateConditionalLogic({ fieldKey: "kind", operator: "in", value: "repair" }, { kind: "repair" })).toBe(true);
|
|
expect(evaluateConditionalLogic({ fieldKey: "kind", operator: "in", value: ["install"] }, { kind: "repair" })).toBe(false);
|
|
expect(evaluateConditionalLogic({ fieldKey: "emptyList", operator: "not_empty" }, { emptyList: [] })).toBe(false);
|
|
expect(evaluateConditionalLogic({ fieldKey: "flag", operator: "not_empty" }, { flag: false })).toBe(false);
|
|
});
|
|
});
|