-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
81 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { describe, it, expect, beforeAll, vi } from 'vitest'; | ||
import { handler } from './handlers'; | ||
import { input } from './test'; | ||
|
||
describe('handlers', () => { | ||
beforeAll(() => { | ||
|
@@ -33,7 +34,7 @@ describe('handlers', () => { | |
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ to: '[email protected]', subject: 'ok' }), | ||
body: JSON.stringify(input), | ||
}); | ||
await handler({ request }); | ||
expect(global.fetch).toHaveBeenCalledWith( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'; | ||
import { describe, it, expect, vi, beforeAll } from 'vitest'; | ||
import { input } from './test'; | ||
import { mail } from './mail'; | ||
|
||
const originalFetch = global.fetch; | ||
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body | ||
const fixture = { | ||
attachments: input.attachments, | ||
content: [ | ||
{ | ||
type: 'text/plain', | ||
value: input.body, | ||
}, | ||
], | ||
from: { | ||
email: '[email protected]', | ||
}, | ||
personalizations: [ | ||
{ | ||
subject: input.subject, | ||
to: [ | ||
{ | ||
email: input.to, | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
describe('mail', () => { | ||
beforeAll(() => { | ||
global.fetch = vi.fn(); | ||
}); | ||
afterAll(() => { | ||
global.fetch = originalFetch; | ||
vi.clearAllMocks(); | ||
}); | ||
it('makes a request to the sendgrid api', async () => { | ||
global.fetch.mockResolvedValueOnce({ json: () => '{}' }); | ||
const to = '[email protected]'; | ||
const subject = 'hello'; | ||
await mail({ to, subject }); | ||
await mail(input); | ||
expect(global.fetch).toHaveBeenCalledWith( | ||
'https://api.sendgrid.com/v3/mail/send', | ||
{ | ||
|
@@ -24,7 +41,7 @@ describe('mail', () => { | |
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: expect.stringMatching(/hello(.*)ex@mple.com/), | ||
body: JSON.stringify(fixture), | ||
}, | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { params } from './params'; | ||
import { params, parseUrlEncoded } from './params'; | ||
import { input, inputUrlEncoded } from './test'; | ||
|
||
describe.each(['application/json', 'application/x-www-form-urlencoded'])( | ||
'params', | ||
(contentType) => { | ||
const body = { | ||
to: '[email protected]', | ||
subject: 'hello world', | ||
to: input.to, | ||
subject: input.subject, | ||
}; | ||
|
||
const format = { | ||
|
@@ -41,3 +42,11 @@ describe.each(['application/json', 'application/x-www-form-urlencoded'])( | |
}); | ||
}, | ||
); | ||
|
||
describe('parseUrlEncoded()', () => { | ||
it('parses a query string into an object', async () => { | ||
expect(parseUrlEncoded(inputUrlEncoded)).toStrictEqual({ | ||
...input, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const input = { | ||
attachments: [ | ||
{ | ||
content: 'aGVsbG8sIHdvcmxkIQ==', | ||
type: 'text/plain', | ||
filename: 'hello.txt', | ||
}, | ||
], | ||
body: 'this is the body', | ||
subject: 'hello', | ||
to: '[email protected]', | ||
}; | ||
|
||
export const inputUrlEncoded = `to=${input.to}&subject=${input.subject}&body=${input.body}&attachments[][type]=text/plain&attachments[][content]=aGVsbG8sIHdvcmxkIQ==&attachments[][filename]=hello.txt`; |