Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

2.5 KB
Raw Blame
// @vitest-environment node
import { describe, it, expect } from "vitest";
import { findInvalidEmail, normalizeSecondaryEmails } from "./secondaryEmails";

describe("normalizeSecondaryEmails", () => {
  it("returns undefined for a non-array, so the caller rejects instead of wiping", () => {
    expect(normalizeSecondaryEmails(undefined)).toBe(undefined);
    expect(normalizeSecondaryEmails(null)).toBe(undefined);
    expect(normalizeSecondaryEmails("[email protected]")).toBe(undefined);
    expect(normalizeSecondaryEmails({ 0: "[email protected]" })).toBe(undefined);
  });

  it("returns an empty array for an empty array, which clears the addresses", () => {
    expect(normalizeSecondaryEmails([])).toEqual([]);
  });

  it("trims and lowercases every address", () => {
    expect(normalizeSecondaryEmails(["  [email protected]  "])).toEqual([
      "[email protected]",
    ]);
  });

  it("removes addresses that differ only by case or spacing", () => {
    expect(
      normalizeSecondaryEmails(["[email protected]", " [email protected] "]),
    ).toEqual(["[email protected]"]);
  });

  it("drops empty entries and non-string values", () => {
    expect(
      normalizeSecondaryEmails(["[email protected]", "", "   ", 42, null]),
    ).toEqual(["[email protected]"]);
  });

  it("preserves the order of the remaining addresses", () => {
    expect(
      normalizeSecondaryEmails(["[email protected]", "[email protected]", "[email protected]"]),
    ).toEqual(["[email protected]", "[email protected]", "[email protected]"]);
  });
});

describe("findInvalidEmail", () => {
  it("returns undefined when every address is well formed", () => {
    expect(
      findInvalidEmail(["[email protected]", "[email protected]"]),
    ).toBe(undefined);
  });

  it("returns undefined for an empty list", () => {
    expect(findInvalidEmail([])).toBe(undefined);
  });

  it("reports an address with no domain", () => {
    expect(findInvalidEmail(["[email protected]", "not-an-email"])).toBe(
      "not-an-email",
    );
  });

  it("reports an address with no top level domain", () => {
    expect(findInvalidEmail(["john@localhost"])).toBe("john@localhost");
  });

  it("reports an address containing a space", () => {
    expect(findInvalidEmail(["john [email protected]"])).toBe("john [email protected]");
  });

  it("reports the first offending address so the caller can name it", () => {
    expect(findInvalidEmail(["bad-one", "bad-two"])).toBe("bad-one");
  });

  it("rejects a comma, which would split the PostgREST or() filter", () => {
    expect(findInvalidEmail(["john,[email protected]"])).toBe("john,[email protected]");
  });
});