|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { server, validateHeaders } from '../../mock-server'; |
| 5 | +import { createBackendApiClient } from '../factory'; |
| 6 | + |
| 7 | +describe('EmailApi', () => { |
| 8 | + const apiClient = createBackendApiClient({ |
| 9 | + apiUrl: 'https://api.clerk.test', |
| 10 | + secretKey: 'deadbeef', |
| 11 | + }); |
| 12 | + |
| 13 | + const mockEmail = { |
| 14 | + object: 'email', |
| 15 | + id: 'ema_123', |
| 16 | + slug: null, |
| 17 | + from_email_name: 'noreply', |
| 18 | + reply_to_email_name: null, |
| 19 | + to_email_address: '[email protected]', |
| 20 | + email_address_id: null, |
| 21 | + user_id: null, |
| 22 | + subject: 'Hello', |
| 23 | + body: '<p>hi</p>', |
| 24 | + body_plain: null, |
| 25 | + status: 'queued', |
| 26 | + data: null, |
| 27 | + delivered_by_clerk: true, |
| 28 | + }; |
| 29 | + |
| 30 | + it('sends a transactional email and snake_cases the body', async () => { |
| 31 | + server.use( |
| 32 | + http.post( |
| 33 | + 'https://api.clerk.test/v1/email', |
| 34 | + validateHeaders(async ({ request }) => { |
| 35 | + const body = await request.json(); |
| 36 | + expect(body).toEqual({ |
| 37 | + to: { address: '[email protected]' }, |
| 38 | + from: { address: '[email protected]' }, |
| 39 | + reply_to: { address: '[email protected]' }, |
| 40 | + subject: 'Hello', |
| 41 | + html: '<p>hi</p>', |
| 42 | + }); |
| 43 | + return HttpResponse.json(mockEmail); |
| 44 | + }), |
| 45 | + ), |
| 46 | + ); |
| 47 | + |
| 48 | + const response = await apiClient.emails.create({ |
| 49 | + to: { address: '[email protected]' }, |
| 50 | + from: { address: '[email protected]' }, |
| 51 | + replyTo: { address: '[email protected]' }, |
| 52 | + subject: 'Hello', |
| 53 | + html: '<p>hi</p>', |
| 54 | + }); |
| 55 | + |
| 56 | + expect(response.id).toBe('ema_123'); |
| 57 | + expect(response.toEmailAddress).toBe('[email protected]'); |
| 58 | + expect(response.status).toBe('queued'); |
| 59 | + expect(response.deliveredByClerk).toBe(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it('sends a transactional email with a text body', async () => { |
| 63 | + server.use( |
| 64 | + http.post( |
| 65 | + 'https://api.clerk.test/v1/email', |
| 66 | + validateHeaders(async ({ request }) => { |
| 67 | + const body = await request.json(); |
| 68 | + expect(body).toEqual({ |
| 69 | + to: { address: '[email protected]' }, |
| 70 | + from: { address: '[email protected]' }, |
| 71 | + subject: 'Hello', |
| 72 | + text: 'hi', |
| 73 | + }); |
| 74 | + return HttpResponse.json({ |
| 75 | + ...mockEmail, |
| 76 | + body: null, |
| 77 | + body_plain: 'hi', |
| 78 | + }); |
| 79 | + }), |
| 80 | + ), |
| 81 | + ); |
| 82 | + |
| 83 | + const response = await apiClient.emails.create({ |
| 84 | + to: { address: '[email protected]' }, |
| 85 | + from: { address: '[email protected]' }, |
| 86 | + subject: 'Hello', |
| 87 | + text: 'hi', |
| 88 | + }); |
| 89 | + |
| 90 | + expect(response.id).toBe('ema_123'); |
| 91 | + expect(response.body).toBeNull(); |
| 92 | + expect(response.bodyPlain).toBe('hi'); |
| 93 | + expect(response.status).toBe('queued'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('sends a transactional email addressed by userId', async () => { |
| 97 | + server.use( |
| 98 | + http.post( |
| 99 | + 'https://api.clerk.test/v1/email', |
| 100 | + validateHeaders(async ({ request }) => { |
| 101 | + const body = await request.json(); |
| 102 | + // The nested `userId` must be snake_cased to `user_id` on the wire. |
| 103 | + expect(body).toEqual({ |
| 104 | + to: { user_id: 'user_123' }, |
| 105 | + from: { address: '[email protected]' }, |
| 106 | + subject: 'Hello', |
| 107 | + html: '<p>hi</p>', |
| 108 | + }); |
| 109 | + return HttpResponse.json({ |
| 110 | + ...mockEmail, |
| 111 | + to_email_address: '[email protected]', |
| 112 | + email_address_id: 'idn_123', |
| 113 | + user_id: 'user_123', |
| 114 | + }); |
| 115 | + }), |
| 116 | + ), |
| 117 | + ); |
| 118 | + |
| 119 | + const response = await apiClient.emails.create({ |
| 120 | + to: { userId: 'user_123' }, |
| 121 | + from: { address: '[email protected]' }, |
| 122 | + subject: 'Hello', |
| 123 | + html: '<p>hi</p>', |
| 124 | + }); |
| 125 | + |
| 126 | + expect(response.toEmailAddress).toBe('[email protected]'); |
| 127 | + expect(response.emailAddressId).toBe('idn_123'); |
| 128 | + expect(response.userId).toBe('user_123'); |
| 129 | + }); |
| 130 | +}); |
0 commit comments