|
| 1 | +vi.mock("@supabase/supabase-js") |
| 2 | +vi.mock("$env/dynamic/private") |
| 3 | +vi.mock("resend") |
| 4 | + |
| 5 | +import { createClient, type User } from "@supabase/supabase-js" |
| 6 | +import { Resend } from "resend" |
| 7 | +import * as mailer from "./mailer" |
| 8 | + |
| 9 | +describe("mailer", () => { |
| 10 | + const mockSend = vi.fn().mockResolvedValue({ id: "mock-email-id" }) |
| 11 | + |
| 12 | + const mockSupabaseClient = { |
| 13 | + auth: { |
| 14 | + admin: { |
| 15 | + getUserById: vi.fn(), |
| 16 | + }, |
| 17 | + }, |
| 18 | + from: vi.fn().mockReturnThis(), |
| 19 | + select: vi.fn().mockReturnThis(), |
| 20 | + eq: vi.fn().mockReturnThis(), |
| 21 | + single: vi.fn(), |
| 22 | + } |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + vi.clearAllMocks() |
| 26 | + const { env } = await import("$env/dynamic/private") |
| 27 | + env.PRIVATE_RESEND_API_KEY = "mock_resend_api_key" |
| 28 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 29 | + ;(createClient as any).mockReturnValue(mockSupabaseClient) |
| 30 | + |
| 31 | + vi.mocked(Resend).mockImplementation( |
| 32 | + () => |
| 33 | + ({ |
| 34 | + emails: { |
| 35 | + send: mockSend, |
| 36 | + }, |
| 37 | + }) as unknown as Resend, |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + describe("sendUserEmail", () => { |
| 42 | + const mockUser = { id: "user123", email: "[email protected]" } |
| 43 | + |
| 44 | + it("sends welcome email", async () => { |
| 45 | + mockSupabaseClient.auth.admin.getUserById.mockResolvedValue({ |
| 46 | + data: { user: { email_confirmed_at: new Date().toISOString() } }, |
| 47 | + error: null, |
| 48 | + }) |
| 49 | + |
| 50 | + mockSupabaseClient.single.mockResolvedValue({ |
| 51 | + data: { unsubscribed: false }, |
| 52 | + error: null, |
| 53 | + }) |
| 54 | + |
| 55 | + await mailer.sendUserEmail({ |
| 56 | + user: mockUser as User, |
| 57 | + subject: "Test", |
| 58 | + |
| 59 | + template_name: "welcome_email", |
| 60 | + template_properties: {}, |
| 61 | + }) |
| 62 | + |
| 63 | + expect(mockSend).toHaveBeenCalled() |
| 64 | + const email = mockSend.mock.calls[0][0] |
| 65 | + expect(email.to).toEqual(["[email protected]"]) |
| 66 | + }) |
| 67 | + |
| 68 | + it("should not send email if user is unsubscribed", async () => { |
| 69 | + const originalConsoleLog = console.log |
| 70 | + console.log = vi.fn() |
| 71 | + |
| 72 | + mockSupabaseClient.auth.admin.getUserById.mockResolvedValue({ |
| 73 | + data: { user: { email_confirmed_at: new Date().toISOString() } }, |
| 74 | + error: null, |
| 75 | + }) |
| 76 | + |
| 77 | + mockSupabaseClient.single.mockResolvedValue({ |
| 78 | + data: { unsubscribed: true }, |
| 79 | + error: null, |
| 80 | + }) |
| 81 | + |
| 82 | + await mailer.sendUserEmail({ |
| 83 | + user: mockUser as User, |
| 84 | + subject: "Test", |
| 85 | + |
| 86 | + template_name: "welcome_email", |
| 87 | + template_properties: {}, |
| 88 | + }) |
| 89 | + |
| 90 | + expect(mockSend).not.toHaveBeenCalled() |
| 91 | + |
| 92 | + expect(console.log).toHaveBeenCalledWith( |
| 93 | + "User unsubscribed. Aborting email. ", |
| 94 | + mockUser.id, |
| 95 | + mockUser.email, |
| 96 | + ) |
| 97 | + |
| 98 | + console.log = originalConsoleLog |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe("sendTemplatedEmail", () => { |
| 103 | + it("sends templated email", async () => { |
| 104 | + await mailer.sendTemplatedEmail({ |
| 105 | + subject: "Test subject", |
| 106 | + |
| 107 | + |
| 108 | + template_name: "welcome_email", |
| 109 | + template_properties: {}, |
| 110 | + }) |
| 111 | + |
| 112 | + expect(mockSend).toHaveBeenCalled() |
| 113 | + const email = mockSend.mock.calls[0][0] |
| 114 | + expect(email.from).toEqual("[email protected]") |
| 115 | + expect(email.to).toEqual(["[email protected]"]) |
| 116 | + expect(email.subject).toEqual("Test subject") |
| 117 | + expect(email.text).toContain("This is a quick sample of a welcome email") |
| 118 | + expect(email.html).toContain(">This is a quick sample of a welcome email") |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments