Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

2.4 KB
Raw Blame
import {
  getNormalizedEmail,
  normalizeEmail,
} from "@/lib/email/normalize-email";
import { describe, expect, it } from "vitest";

describe("normalizeEmail", () => {
  it.each([
    ["[email protected]", "[email protected]", "gmail dots"],
    ["[email protected]", "[email protected]", "gmail dots everywhere"],
    ["[email protected]", "[email protected]", "underscore"],
    ["[email protected]", "[email protected]", "hyphen"],
    ["[email protected]", "[email protected]", "dots on any domain"],
    ["[email protected]", "[email protected]", "plus tag"],
    ["[email protected]", "[email protected]", "plus tag with separators"],
    ["[email protected]", "[email protected]", "googlemail alias"],
    ["  [email protected]  ", "[email protected]", "case and whitespace"],
    [
      "[email protected]",
      "[email protected]",
      "keeps domain dots",
    ],
  ])("%s -> %s (%s)", (input, expected) => {
    expect(normalizeEmail(input)).toBe(expected);
  });

  it.each([
    ["not-an-email", "not-an-email", "no @"],
    ["a@[email protected]", "a@[email protected]", "multiple @"],
    ["[email protected]", "[email protected]", "local part empty after stripping"],
    ["[email protected]", "[email protected]", "local part empty after plus strip"],
  ])("returns trimmed lowercase input for %s (%s)", (input, expected) => {
    expect(normalizeEmail(input)).toBe(expected);
  });

  it.each([
    ["[email protected]", "[email protected]"],
    ["[email protected]", "[email protected]"],
    ["[email protected]", "[email protected]"],
  ])("treats %s and %s as the same address", (a, b) => {
    expect(normalizeEmail(a)).toBe(normalizeEmail(b));
  });

  it("keeps distinct addresses distinct", () => {
    expect(normalizeEmail("[email protected]")).not.toBe(
      normalizeEmail("[email protected]"),
    );
    expect(normalizeEmail("[email protected]")).not.toBe(
      normalizeEmail("[email protected]"),
    );
  });
});

describe("getNormalizedEmail", () => {
  it("returns null for empty values", () => {
    expect(getNormalizedEmail(null)).toBeNull();
    expect(getNormalizedEmail(undefined)).toBeNull();
    expect(getNormalizedEmail("")).toBeNull();
  });

  it("normalizes non-empty values", () => {
    expect(getNormalizedEmail("[email protected]")).toBe("[email protected]");
  });
});