|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +const mockGetSession = jest.fn() |
| 6 | +const mockCreate = jest.fn() |
| 7 | +const originalApiKey = process.env.RESEND_API_KEY |
| 8 | + |
| 9 | +process.env.RESEND_API_KEY = 're_test_key' |
| 10 | + |
| 11 | +jest.mock('@repo/auth/auth', () => ({ |
| 12 | + auth: { |
| 13 | + api: { |
| 14 | + getSession: mockGetSession |
| 15 | + } |
| 16 | + } |
| 17 | +})) |
| 18 | + |
| 19 | +jest.mock('resend', () => ({ |
| 20 | + Resend: jest.fn().mockImplementation(() => ({ |
| 21 | + contacts: { |
| 22 | + create: mockCreate |
| 23 | + } |
| 24 | + })) |
| 25 | +})) |
| 26 | + |
| 27 | +jest.mock('next/headers', () => ({ |
| 28 | + headers: jest.fn() |
| 29 | +})) |
| 30 | + |
| 31 | +jest.mock('@/lib/telemetry-server', () => ({ |
| 32 | + captureServerException: jest.fn() |
| 33 | +})) |
| 34 | + |
| 35 | +const { headers } = require('next/headers') |
| 36 | +const { POST } = require('../route') |
| 37 | + |
| 38 | +describe('subscribe me route', () => { |
| 39 | + afterAll(() => { |
| 40 | + if (originalApiKey === undefined) { |
| 41 | + delete process.env.RESEND_API_KEY |
| 42 | + } else { |
| 43 | + process.env.RESEND_API_KEY = originalApiKey |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + jest.clearAllMocks() |
| 49 | + headers.mockResolvedValue(new Headers()) |
| 50 | + }) |
| 51 | + |
| 52 | + it('requires an authenticated user', async () => { |
| 53 | + mockGetSession.mockResolvedValue(null) |
| 54 | + |
| 55 | + const response = await POST() |
| 56 | + |
| 57 | + expect(response.status).toBe(401) |
| 58 | + await expect(response.json()).resolves.toEqual({ error: 'Unauthorized' }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('syncs the current signed-in user email to Resend', async () => { |
| 62 | + mockGetSession.mockResolvedValue({ |
| 63 | + user: { |
| 64 | + email: ' test@example.com ', |
| 65 | + id: 'user-1' |
| 66 | + } |
| 67 | + }) |
| 68 | + mockCreate.mockResolvedValue({ data: { id: 'contact-1' }, error: null }) |
| 69 | + |
| 70 | + const response = await POST() |
| 71 | + |
| 72 | + expect(mockCreate).toHaveBeenCalledWith( |
| 73 | + expect.objectContaining({ |
| 74 | + email: 'test@example.com', |
| 75 | + properties: expect.objectContaining({ |
| 76 | + product: 'newsletter', |
| 77 | + user_type: 'subscriber' |
| 78 | + }) |
| 79 | + }) |
| 80 | + ) |
| 81 | + expect(response.status).toBe(201) |
| 82 | + await expect(response.json()).resolves.toEqual({ |
| 83 | + id: 'contact-1', |
| 84 | + status: 'created', |
| 85 | + success: true |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('returns success when the current user already exists in Resend', async () => { |
| 90 | + mockGetSession.mockResolvedValue({ |
| 91 | + user: { |
| 92 | + email: 'test@example.com', |
| 93 | + id: 'user-1' |
| 94 | + } |
| 95 | + }) |
| 96 | + mockCreate.mockResolvedValue({ |
| 97 | + data: null, |
| 98 | + error: { |
| 99 | + message: 'Contact already exists', |
| 100 | + name: 'validation_error' |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + const response = await POST() |
| 105 | + |
| 106 | + expect(response.status).toBe(200) |
| 107 | + await expect(response.json()).resolves.toEqual({ |
| 108 | + status: 'already_exists', |
| 109 | + success: true |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments