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.

1.9 KB
Raw Blame
import { canChangeEmail } from "@/lib/email/can-change-email";
import { getEmailDomainBlockFlags } from "@/lib/email/get-email-domain-block-flags";
import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("@/lib/email/get-email-domain-block-flags", () => ({
  getEmailDomainBlockFlags: vi.fn(),
}));

const getEmailDomainBlockFlagsMock = vi.mocked(getEmailDomainBlockFlags);

describe("canChangeEmail", () => {
  beforeEach(() => {
    getEmailDomainBlockFlagsMock.mockReset();
  });

  it("always rejects disposable domains", async () => {
    getEmailDomainBlockFlagsMock.mockResolvedValue({
      isDisposable: true,
      matchesBlockedTerms: false,
    });

    await expect(
      canChangeEmail({
        currentEmail: "[email protected]",
        newEmail: "[email protected]",
      }),
    ).resolves.toBe(false);
  });

  it("rejects when current email is generic with a + alias", async () => {
    getEmailDomainBlockFlagsMock.mockResolvedValue({
      isDisposable: false,
      matchesBlockedTerms: false,
    });

    await expect(
      canChangeEmail({
        currentEmail: "[email protected]",
        newEmail: "[email protected]",
      }),
    ).resolves.toBe(false);
  });

  it("rejects when new email matches blocked terms", async () => {
    getEmailDomainBlockFlagsMock.mockResolvedValue({
      isDisposable: false,
      matchesBlockedTerms: true,
    });

    await expect(
      canChangeEmail({
        currentEmail: "[email protected]",
        newEmail: "[email protected]",
      }),
    ).resolves.toBe(false);
  });

  it("allows when neither plus-alias nor terms apply", async () => {
    getEmailDomainBlockFlagsMock.mockResolvedValue({
      isDisposable: false,
      matchesBlockedTerms: false,
    });

    await expect(
      canChangeEmail({
        currentEmail: "[email protected]",
        newEmail: "[email protected]",
      }),
    ).resolves.toBe(true);
  });
});