11import type { ErrorResponse } from '../../interfaces' ;
22import { Resend } from '../../resend' ;
3+ import { mockSuccessResponse } from '../../test-utils/mock-fetch' ;
4+ import type { GetAttachmentResponseSuccess } from './interfaces' ;
5+ import type { ListAttachmentsResponseSuccess } from './interfaces/list-attachments.interface' ;
36
47const resend = new Resend ( 're_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop' ) ;
58
@@ -41,7 +44,7 @@ describe('Receiving', () => {
4144
4245 describe ( 'when attachment found' , ( ) => {
4346 it ( 'returns attachment with transformed fields' , async ( ) => {
44- const apiResponse = {
47+ const apiResponse : GetAttachmentResponseSuccess = {
4548 object : 'attachment' as const ,
4649 data : {
4750 id : 'att_123' ,
@@ -174,6 +177,33 @@ describe('Receiving', () => {
174177 } ) ;
175178
176179 describe ( 'list' , ( ) => {
180+ const apiResponse : ListAttachmentsResponseSuccess = {
181+ object : 'list' as const ,
182+ has_more : false ,
183+ data : [
184+ {
185+ id : 'att_123' ,
186+ filename : 'document.pdf' ,
187+ content_type : 'application/pdf' ,
188+ content_id : 'cid_123' ,
189+ content_disposition : 'attachment' as const ,
190+ content : 'base64encodedcontent==' ,
191+ } ,
192+ {
193+ id : 'att_456' ,
194+ filename : 'image.png' ,
195+ content_type : 'image/png' ,
196+ content_id : 'cid_456' ,
197+ content_disposition : 'inline' as const ,
198+ content : 'imagebase64==' ,
199+ } ,
200+ ] ,
201+ } ;
202+
203+ const headers = {
204+ Authorization : 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop' ,
205+ } ;
206+
177207 describe ( 'when inbound email not found' , ( ) => {
178208 it ( 'returns error' , async ( ) => {
179209 const response : ErrorResponse = {
@@ -199,28 +229,6 @@ describe('Receiving', () => {
199229
200230 describe ( 'when attachments found' , ( ) => {
201231 it ( 'returns multiple attachments' , async ( ) => {
202- const apiResponse = {
203- object : 'attachment' as const ,
204- data : [
205- {
206- id : 'att_123' ,
207- filename : 'document.pdf' ,
208- content_type : 'application/pdf' ,
209- content_id : 'cid_123' ,
210- content_disposition : 'attachment' as const ,
211- content : 'base64encodedcontent==' ,
212- } ,
213- {
214- id : 'att_456' ,
215- filename : 'image.png' ,
216- content_type : 'image/png' ,
217- content_id : 'cid_456' ,
218- content_disposition : 'inline' as const ,
219- content : 'imagebase64==' ,
220- } ,
221- ] ,
222- } ;
223-
224232 fetchMock . mockOnce ( JSON . stringify ( apiResponse ) , {
225233 status : 200 ,
226234 headers : {
@@ -237,12 +245,12 @@ describe('Receiving', () => {
237245 } ) ;
238246
239247 it ( 'returns empty array when no attachments' , async ( ) => {
240- const apiResponse = {
248+ const emptyResponse = {
241249 object : 'attachment' as const ,
242250 data : [ ] ,
243251 } ;
244252
245- fetchMock . mockOnce ( JSON . stringify ( apiResponse ) , {
253+ fetchMock . mockOnce ( JSON . stringify ( emptyResponse ) , {
246254 status : 200 ,
247255 headers : {
248256 'content-type' : 'application/json' ,
@@ -254,7 +262,74 @@ describe('Receiving', () => {
254262 emailId : '67d9bcdb-5a02-42d7-8da9-0d6feea18cff' ,
255263 } ) ;
256264
257- expect ( result ) . toEqual ( { data : apiResponse , error : null } ) ;
265+ expect ( result ) . toEqual ( { data : emptyResponse , error : null } ) ;
266+ } ) ;
267+ } ) ;
268+
269+ describe ( 'when no pagination options provided' , ( ) => {
270+ it ( 'calls endpoint without query params and return the response' , async ( ) => {
271+ mockSuccessResponse ( apiResponse , {
272+ headers,
273+ } ) ;
274+
275+ const result = await resend . attachments . receiving . list ( {
276+ emailId : '67d9bcdb-5a02-42d7-8da9-0d6feea18cff' ,
277+ } ) ;
278+
279+ expect ( result ) . toEqual ( {
280+ data : apiResponse ,
281+ error : null ,
282+ } ) ;
283+ expect ( fetchMock . mock . calls [ 0 ] [ 0 ] ) . toBe (
284+ 'https://api.resend.com/emails/receiving/67d9bcdb-5a02-42d7-8da9-0d6feea18cff' ,
285+ ) ;
286+ } ) ;
287+ } ) ;
288+
289+ describe ( 'when pagination options are provided' , ( ) => {
290+ it ( 'calls endpoint passing limit param and return the response' , async ( ) => {
291+ mockSuccessResponse ( apiResponse , { headers } ) ;
292+ const result = await resend . attachments . receiving . list ( {
293+ emailId : '67d9bcdb-5a02-42d7-8da9-0d6feea18cff' ,
294+ limit : 10 ,
295+ } ) ;
296+ expect ( result ) . toEqual ( {
297+ data : apiResponse ,
298+ error : null ,
299+ } ) ;
300+ expect ( fetchMock . mock . calls [ 0 ] [ 0 ] ) . toBe (
301+ 'https://api.resend.com/emails/receiving/67d9bcdb-5a02-42d7-8da9-0d6feea18cff?limit=10' ,
302+ ) ;
303+ } ) ;
304+
305+ it ( 'calls endpoint passing after param and return the response' , async ( ) => {
306+ mockSuccessResponse ( apiResponse , { headers } ) ;
307+ const result = await resend . attachments . receiving . list ( {
308+ emailId : '67d9bcdb-5a02-42d7-8da9-0d6feea18cff' ,
309+ after : 'cursor123' ,
310+ } ) ;
311+ expect ( result ) . toEqual ( {
312+ data : apiResponse ,
313+ error : null ,
314+ } ) ;
315+ expect ( fetchMock . mock . calls [ 0 ] [ 0 ] ) . toBe (
316+ 'https://api.resend.com/emails/receiving/67d9bcdb-5a02-42d7-8da9-0d6feea18cff?after=cursor123' ,
317+ ) ;
318+ } ) ;
319+
320+ it ( 'calls endpoint passing before param and return the response' , async ( ) => {
321+ mockSuccessResponse ( apiResponse , { headers } ) ;
322+ const result = await resend . attachments . receiving . list ( {
323+ emailId : '67d9bcdb-5a02-42d7-8da9-0d6feea18cff' ,
324+ before : 'cursor123' ,
325+ } ) ;
326+ expect ( result ) . toEqual ( {
327+ data : apiResponse ,
328+ error : null ,
329+ } ) ;
330+ expect ( fetchMock . mock . calls [ 0 ] [ 0 ] ) . toBe (
331+ 'https://api.resend.com/emails/receiving/67d9bcdb-5a02-42d7-8da9-0d6feea18cff?before=cursor123' ,
332+ ) ;
258333 } ) ;
259334 } ) ;
260335 } ) ;
0 commit comments