Skip to content

Commit

Permalink
feat: Add missing fields to the GetEmailResponse type (#146)
Browse files Browse the repository at this point in the history
Co-authored-by: Bu Kinoshita <6929565+bukinoshita@users.noreply.github.com>
Co-authored-by: Bu Kinoshita <bukinoshita@gmail.com>
  • Loading branch information
3 people authored Aug 15, 2023
1 parent 4a70e4e commit 6dc92a0
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 5 deletions.
195 changes: 195 additions & 0 deletions src/emails/emails.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import { enableFetchMocks } from 'jest-fetch-mock';
import { Resend } from '../resend';
import { CreateEmailOptions, GetEmailResponse } from './interfaces';

enableFetchMocks();

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

describe('Emails', () => {
afterEach(() => fetchMock.resetMocks());

describe('create', () => {
it('sends email', async () => {
const payload: CreateEmailOptions = {
from: 'bu@resend.com',
to: 'zeno@resend.com',
subject: 'Hello World',
html: '<h1>Hello world</h1>',
};

fetchMock.mockOnce(
JSON.stringify({
id: '1234',
}),
{
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

const data = await resend.emails.create(payload);
expect(data).toMatchInlineSnapshot(`
{
"id": "1234",
}
`);
});
});

describe('send', () => {
it('sends email', async () => {
const payload: CreateEmailOptions = {
from: 'bu@resend.com',
to: 'zeno@resend.com',
subject: 'Hello World',
html: '<h1>Hello world</h1>',
};

fetchMock.mockOnce(
JSON.stringify({
id: '1234',
}),
{
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

const data = await resend.emails.send(payload);
expect(data).toMatchInlineSnapshot(`
{
"id": "1234",
}
`);
});
});

describe('get', () => {
describe('when email not found', () => {
it('returns error', async () => {
fetchMock.mockOnce(
JSON.stringify({
name: 'not_found',
message: 'Email not found',
statusCode: 404,
}),
{
status: 404,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

await expect(resend.emails.get('1234')).resolves.toMatchInlineSnapshot(`
{
"message": "Email not found",
"name": "not_found",
"statusCode": 404,
}
`);
});
});

describe('when email found', () => {
it('returns emails with only to', async () => {
const response: GetEmailResponse = {
object: 'email',
id: '123',
to: ['zeno@resend.com'],
from: 'bu@resend.com',
created_at: '321',
subject: 'Test email',
html: '<p>hello hello</p>',
text: null,
bcc: null,
cc: null,
reply_to: null,
last_event: 'sent',
};

fetchMock.mockOnce(JSON.stringify(response), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

await expect(resend.emails.get('1234')).resolves.toMatchInlineSnapshot(`
{
"bcc": null,
"cc": null,
"created_at": "321",
"from": "bu@resend.com",
"html": "<p>hello hello</p>",
"id": "123",
"last_event": "sent",
"object": "email",
"reply_to": null,
"subject": "Test email",
"text": null,
"to": [
"zeno@resend.com",
],
}
`);
});

it('returns emails with to and multiple cc', async () => {
const response: GetEmailResponse = {
object: 'email',
id: '123',
to: ['zeno@resend.com'],
from: 'bu@resend.com',
created_at: '321',
subject: 'Test email',
html: '<p>hello hello</p>',
text: null,
bcc: null,
cc: ['zeno@resend.com', 'bu@resend.com'],
reply_to: null,
last_event: 'sent',
};

fetchMock.mockOnce(JSON.stringify(response), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

await expect(resend.emails.get('1234')).resolves.toMatchInlineSnapshot(`
{
"bcc": null,
"cc": [
"zeno@resend.com",
"bu@resend.com",
],
"created_at": "321",
"from": "bu@resend.com",
"html": "<p>hello hello</p>",
"id": "123",
"last_event": "sent",
"object": "email",
"reply_to": null,
"subject": "Test email",
"text": null,
"to": [
"zeno@resend.com",
],
}
`);
});
});
});
});
2 changes: 1 addition & 1 deletion src/emails/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
CreateEmailOptions,
CreateEmailRequestOptions,
CreateEmailResponse,
GetEmailResponse,
} from './interfaces';
import { GetEmailResponse } from './interfaces/get-email-options.interface';

export class Emails {
constructor(private readonly resend: Resend) {}
Expand Down
11 changes: 7 additions & 4 deletions src/emails/interfaces/get-email-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export interface GetEmailResponse {
bcc?: string[];
cc?: string[];
bcc: string[] | null;
cc: string[] | null;
created_at: string;
from: string;
html?: string;
html: string | null;
id: string;
last_event: string;
reply_to: string[] | null;
subject: string;
text?: string;
text: string | null;
to: string[];
object: 'email';
}
1 change: 1 addition & 0 deletions src/emails/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './create-email-options.interface';
export * from './get-email-options.interface';

0 comments on commit 6dc92a0

Please sign in to comment.