Last active
February 15, 2022 16:59
-
-
Save Pompeu/d3c456c408f50784380be97c6cb2b92f to your computer and use it in GitHub Desktop.
try mock sendgrid
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
'use strict'; | |
const sendgrid = require('sendgrid')(process.env.MAIL_KEY || 'may secrete key'); | |
function Mailer () { | |
this.sendMail = body => { | |
if(!this.verifyMailbody(body)) { | |
throw new TypeError('invalid body'); | |
} | |
return new Promise((resolve, reject) => { | |
return sendgrid.send({ | |
body | |
}, (err, json) => { | |
err ? reject(err) : resolve(json); | |
}); | |
}); | |
}; | |
this.verifyMailbody = body => { | |
return Object | |
.keys(body) | |
.map(verifyProperts) | |
.reduce(b => b); | |
}; | |
const propertys = ['to', 'from', 'subject', 'text']; | |
function verifyProperts(body, index) { | |
return propertys[index] === body; | |
} | |
} | |
module.exports = Mailer; | |
///testes | |
'use strict'; | |
const chai = require('chai'); | |
const expect = chai.expect; | |
const chaiAsPromise = require('chai-as-promised'); | |
chai.use(chaiAsPromise); | |
const Mailer = require('../mailer/mailer'); | |
const sinon = require('sinon'); | |
require('sinon-as-promised'); | |
describe('mailer', () => { | |
let mail = null; | |
let mailer = null; | |
before(() => { | |
mailer = new Mailer(); | |
mail = { | |
to : '[email protected]', | |
from : '[email protected]', | |
subject : 'Hello World', | |
text : 'My first email through SendGrid.' | |
}; | |
}); | |
describe('mailer', () => { | |
it('should be mailer has method send mail', () => { | |
expect(mailer).to.have.property('sendMail'); | |
}); | |
it('should be mailer mail has method then', () => { | |
expect(mailer.sendMail(mail)).to.be.instanceof(Promise); | |
}); | |
it('should be throw error if body missing properts', () => { | |
expect(() => { | |
mailer.sendMail(); | |
}).to.throw(TypeError, /body no has to,from,subject,text/); | |
}); | |
it('should be method sendiMail recive a body', () => { | |
const spy = sinon.spy(mailer, 'sendMail'); | |
spy.withArgs(mail); | |
mailer.sendMail(mail); | |
expect(spy.withArgs(mail).calledOnce).to.be.true; | |
}); | |
it('should be method sendiMail throw if invalid body ', () => { | |
const mailFail = { | |
a: '[email protected]', | |
from: '[email protected]', | |
subject: 'Hello World', | |
text: 'My first email through SendGrid.' | |
}; | |
expect(function () { | |
mailer.sendMail(mailFail); | |
}).to.throw(TypeError, /^invalid body$/); | |
}); | |
it('should be method sendMail send a mail with success', done => { | |
const mailer = new Mailer(); | |
const mock = sinon.mock(mailer); | |
const success = {success : 'mail success'}; | |
mock.expects('sendMail') | |
.once() | |
.withArgs(mail) | |
.resolves(success); | |
expect(mailer.sendMail(mail)) | |
.to | |
.eventually | |
.equal(success) | |
.notify(done); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment