Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

1.1 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { isEmailTaken } from "./data";

const { mockFindUnique } = vi.hoisted(() => ({
  mockFindUnique: vi.fn(),
}));

vi.mock("@rallly/database", () => ({
  prisma: {
    user: {
      findUnique: mockFindUnique,
    },
  },
}));

describe("isEmailTaken", () => {
  beforeEach(() => {
    mockFindUnique.mockReset();
  });

  it("returns true when another account already uses the address", async () => {
    mockFindUnique.mockResolvedValue({ id: "user-1" });

    await expect(isEmailTaken("[email protected]")).resolves.toBe(true);
  });

  it("returns false when no account uses the address", async () => {
    mockFindUnique.mockResolvedValue(null);

    await expect(isEmailTaken("[email protected]")).resolves.toBe(false);
  });

  it("matches addresses case-insensitively", async () => {
    mockFindUnique.mockResolvedValue({ id: "user-1" });

    await isEmailTaken("[email protected]");

    expect(mockFindUnique).toHaveBeenCalledWith({
      where: { email: "[email protected]" },
      select: { id: true },
    });
  });
});