|
| 1 | +import { enableFetchMocks } from 'jest-fetch-mock'; |
| 2 | +import type { ErrorResponse } from '../interfaces'; |
| 3 | +import { Resend } from '../resend'; |
| 4 | +import type { |
| 5 | + CreateBroadcastOptions, |
| 6 | + CreateBroadcastResponseSuccess, |
| 7 | +} from './interfaces/create-broadcast-options.interface'; |
| 8 | + |
| 9 | +enableFetchMocks(); |
| 10 | + |
| 11 | +const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop'); |
| 12 | + |
| 13 | +describe('Broadcasts', () => { |
| 14 | + afterEach(() => fetchMock.resetMocks()); |
| 15 | + |
| 16 | + describe('create', () => { |
| 17 | + it('missing `from`', async () => { |
| 18 | + const response: ErrorResponse = { |
| 19 | + name: 'missing_required_field', |
| 20 | + message: 'Missing `from` field.', |
| 21 | + }; |
| 22 | + |
| 23 | + fetchMock.mockOnce(JSON.stringify(response), { |
| 24 | + status: 422, |
| 25 | + headers: { |
| 26 | + 'content-type': 'application/json', |
| 27 | + Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + const data = await resend.broadcasts.create({} as CreateBroadcastOptions); |
| 32 | + console.log({ data }); |
| 33 | + expect(data).toMatchInlineSnapshot(` |
| 34 | +{ |
| 35 | + "data": null, |
| 36 | + "error": { |
| 37 | + "message": "Missing \`from\` field.", |
| 38 | + "name": "missing_required_field", |
| 39 | + }, |
| 40 | +} |
| 41 | +`); |
| 42 | + }); |
| 43 | + |
| 44 | + it('creates broadcast', async () => { |
| 45 | + const response: CreateBroadcastResponseSuccess = { |
| 46 | + id: '71cdfe68-cf79-473a-a9d7-21f91db6a526', |
| 47 | + }; |
| 48 | + fetchMock.mockOnce(JSON.stringify(response), { |
| 49 | + status: 200, |
| 50 | + headers: { |
| 51 | + 'content-type': 'application/json', |
| 52 | + Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const payload: CreateBroadcastOptions = { |
| 57 | + |
| 58 | + audienceId: '0192f4ed-c2e9-7112-9c13-b04a043e23ee', |
| 59 | + subject: 'Hello World', |
| 60 | + html: '<h1>Hello world</h1>', |
| 61 | + }; |
| 62 | + |
| 63 | + const data = await resend.broadcasts.create(payload); |
| 64 | + expect(data).toMatchInlineSnapshot(` |
| 65 | +{ |
| 66 | + "data": { |
| 67 | + "id": "71cdfe68-cf79-473a-a9d7-21f91db6a526", |
| 68 | + }, |
| 69 | + "error": null, |
| 70 | +} |
| 71 | +`); |
| 72 | + }); |
| 73 | + |
| 74 | + it('creates broadcast with multiple recipients', async () => { |
| 75 | + const response: CreateBroadcastResponseSuccess = { |
| 76 | + id: '124dc0f1-e36c-417c-a65c-e33773abc768', |
| 77 | + }; |
| 78 | + |
| 79 | + fetchMock.mockOnce(JSON.stringify(response), { |
| 80 | + status: 200, |
| 81 | + headers: { |
| 82 | + 'content-type': 'application/json', |
| 83 | + Authorization: 'Bearer re_924b3rjh2387fbewf823', |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const payload: CreateBroadcastOptions = { |
| 88 | + |
| 89 | + audienceId: '0192f4f1-d5f9-7110-8eb5-370552515917', |
| 90 | + subject: 'Hello World', |
| 91 | + text: 'Hello world', |
| 92 | + }; |
| 93 | + const data = await resend.broadcasts.create(payload); |
| 94 | + expect(data).toMatchInlineSnapshot(` |
| 95 | +{ |
| 96 | + "data": { |
| 97 | + "id": "124dc0f1-e36c-417c-a65c-e33773abc768", |
| 98 | + }, |
| 99 | + "error": null, |
| 100 | +} |
| 101 | +`); |
| 102 | + }); |
| 103 | + |
| 104 | + it('creates broadcast with multiple replyTo emails', async () => { |
| 105 | + const response: CreateBroadcastResponseSuccess = { |
| 106 | + id: '124dc0f1-e36c-417c-a65c-e33773abc768', |
| 107 | + }; |
| 108 | + |
| 109 | + fetchMock.mockOnce(JSON.stringify(response), { |
| 110 | + status: 200, |
| 111 | + headers: { |
| 112 | + 'content-type': 'application/json', |
| 113 | + Authorization: 'Bearer re_924b3rjh2387fbewf823', |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const payload: CreateBroadcastOptions = { |
| 118 | + |
| 119 | + audienceId: '0192f4f1-d5f9-7110-8eb5-370552515917', |
| 120 | + |
| 121 | + subject: 'Hello World', |
| 122 | + text: 'Hello world', |
| 123 | + }; |
| 124 | + |
| 125 | + const data = await resend.broadcasts.create(payload); |
| 126 | + expect(data).toMatchInlineSnapshot(` |
| 127 | +{ |
| 128 | + "data": { |
| 129 | + "id": "124dc0f1-e36c-417c-a65c-e33773abc768", |
| 130 | + }, |
| 131 | + "error": null, |
| 132 | +} |
| 133 | +`); |
| 134 | + }); |
| 135 | + |
| 136 | + it('throws an error when an ErrorResponse is returned', async () => { |
| 137 | + const response: ErrorResponse = { |
| 138 | + name: 'invalid_parameter', |
| 139 | + message: |
| 140 | + 'Invalid `from` field. The email address needs to follow the `[email protected]` or `Name <[email protected]>` format', |
| 141 | + }; |
| 142 | + |
| 143 | + fetchMock.mockOnce(JSON.stringify(response), { |
| 144 | + status: 422, |
| 145 | + headers: { |
| 146 | + 'content-type': 'application/json', |
| 147 | + Authorization: 'Bearer re_924b3rjh2387fbewf823', |
| 148 | + }, |
| 149 | + }); |
| 150 | + |
| 151 | + const payload: CreateBroadcastOptions = { |
| 152 | + from: 'resend.com', // Invalid from address |
| 153 | + audienceId: '0192f4f1-d5f9-7110-8eb5-370552515917', |
| 154 | + |
| 155 | + subject: 'Hello World', |
| 156 | + text: 'Hello world', |
| 157 | + }; |
| 158 | + |
| 159 | + const result = resend.broadcasts.create(payload); |
| 160 | + |
| 161 | + await expect(result).resolves.toMatchInlineSnapshot(` |
| 162 | + { |
| 163 | + "data": null, |
| 164 | + "error": { |
| 165 | + "message": "Invalid \`from\` field. The email address needs to follow the \`[email protected]\` or \`Name <[email protected]>\` format", |
| 166 | + "name": "invalid_parameter", |
| 167 | + }, |
| 168 | + } |
| 169 | + `); |
| 170 | + }); |
| 171 | + |
| 172 | + it('returns an error when fetch fails', async () => { |
| 173 | + const originalEnv = process.env; |
| 174 | + process.env = { |
| 175 | + ...originalEnv, |
| 176 | + RESEND_BASE_URL: 'http://invalidurl.noturl', |
| 177 | + }; |
| 178 | + |
| 179 | + const result = await resend.broadcasts.create({ |
| 180 | + |
| 181 | + audienceId: '0192f4f1-d5f9-7110-8eb5-370552515917', |
| 182 | + subject: 'Hello World', |
| 183 | + text: 'Hello world', |
| 184 | + }); |
| 185 | + |
| 186 | + expect(result).toEqual( |
| 187 | + expect.objectContaining({ |
| 188 | + data: null, |
| 189 | + error: { |
| 190 | + message: 'Unable to fetch data. The request could not be resolved.', |
| 191 | + name: 'application_error', |
| 192 | + }, |
| 193 | + }), |
| 194 | + ); |
| 195 | + process.env = originalEnv; |
| 196 | + }); |
| 197 | + |
| 198 | + it('returns an error when api responds with text payload', async () => { |
| 199 | + fetchMock.mockOnce('local_rate_limited', { |
| 200 | + status: 422, |
| 201 | + headers: { |
| 202 | + Authorization: 'Bearer re_924b3rjh2387fbewf823', |
| 203 | + }, |
| 204 | + }); |
| 205 | + |
| 206 | + const result = await resend.broadcasts.create({ |
| 207 | + |
| 208 | + audienceId: '0192f4f1-d5f9-7110-8eb5-370552515917', |
| 209 | + subject: 'Hello World', |
| 210 | + text: 'Hello world', |
| 211 | + }); |
| 212 | + |
| 213 | + expect(result).toEqual( |
| 214 | + expect.objectContaining({ |
| 215 | + data: null, |
| 216 | + error: { |
| 217 | + message: |
| 218 | + 'Internal server error. We are unable to process your request right now, please try again later.', |
| 219 | + name: 'application_error', |
| 220 | + }, |
| 221 | + }), |
| 222 | + ); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments