Skip to content

Commit

Permalink
chore: add more test routes
Browse files Browse the repository at this point in the history
  • Loading branch information
gweiying committed Aug 22, 2022
1 parent 702dce3 commit 92bcf99
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/server/services/__tests__/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,42 @@ describe('Mailer tests', () => {
expect(mockFetch).not.toHaveBeenCalled()
})

it('should throw error if response is not ok', async () => {
it('should throw error if postman fails to send mail', async () => {
jest.mock('cross-fetch', () => mockFetch)
jest.mock('../../config', () => ({
logger: console,
postmanApiKey: 'key',
postmanApiUrl: 'url',
}))
const { MailerNode } = require('../email')
const service = new MailerNode()

mockFetch.mockResolvedValue({ ok: false })

await expect(service.sendPostmanMail(testMailBody)).rejects.toThrowError()
expect(mockFetch).toHaveBeenCalled()
})
})

describe('sendTransporterMail', () => {
it('should throw error if transporter fails to send mail', async () => {
const sendMailMock = jest.fn((_, callback) => {
const err = new Error('error')
callback(err, null)
})
jest.mock('nodemailer', () => ({
createTransport: jest.fn().mockImplementation(() => ({
sendMail: sendMailMock,
})),
}))

const { MailerNode } = require('../email')
const service = new MailerNode()
service.initMailer()
await expect(
service.sendTransporterMail(testMailBody),
).rejects.toThrowError()
expect(sendMailMock).toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -96,7 +126,7 @@ describe('Mailer tests', () => {
const { MailerNode } = require('../email')
const service = new MailerNode()

it('should not send mail if email or otp is undefined', async () => {
it('should not send mail if otp is undefined', async () => {
const testEmail = '[email protected]'
const testOtp = undefined
const testIp = '1.1.1.1'
Expand All @@ -105,5 +135,15 @@ describe('Mailer tests', () => {
await service.mailOTP(testEmail, testOtp, testIp)
expect(sendMailSpy).not.toHaveBeenCalled()
})

it('should not send mail if email is undefined', async () => {
const testEmail = undefined
const testOtp = '111111'
const testIp = '1.1.1.1'

const sendMailSpy = jest.spyOn(service, 'sendMail')
await service.mailOTP(testEmail, testOtp, testIp)
expect(sendMailSpy).not.toHaveBeenCalled()
})
})
})

0 comments on commit 92bcf99

Please sign in to comment.