Best JavaScript code snippet using qawolf
mail.test.js
Source:mail.test.js
1import chai from 'chai';2import sgMail from '@sendgrid/mail';3import * as sinon from 'sinon';4import Mail from '../../services/Mail';5import { realUser } from '../testHelpers/testLoginData';6import Util from '../../helpers/Util';7import db from '../../models';8import { deleteTable } from '../testHelpers';9const { User } = db;10const { expect } = chai;11describe('Mail', () => {12 let user = null;13 before(async () => {14 await deleteTable(User);15 user = await User.create({16 ...realUser,17 token: Util.generateRandomString(32)18 });19 });20 describe('sendVerification', () => {21 it('should return a successful status when email has been sent.', async () => {22 const mockSGMailSend = sinon.stub(sgMail, 'send').returns(23 Promise.resolve([24 { statusCode: 202 },25 {26 status: 'success'27 }28 ])29 );30 const res = await Mail.sendVerification(user);31 expect(res).to.be.a('object');32 expect(res)33 .to.have.property('status')34 .that.is.equal('success');35 mockSGMailSend.restore();36 });37 it('should return an error status when sending email fails', async () => {38 const mockSGMailSend = sinon.stub(sgMail, 'send').returns(39 Promise.resolve([40 { statusCode: 203 },41 {42 status: 'error'43 }44 ])45 );46 const res = await Mail.sendVerification(user);47 expect(res).to.be.a('object');48 expect(res)49 .to.have.property('status')50 .that.is.equal('error');51 expect(res).to.have.property('message');52 mockSGMailSend.restore();53 });54 it('should return error if share email is not sent', async () => {55 const mockSGMailSend = sinon.stub(sgMail, 'send').returns(56 Promise.resolve([57 { statusCode: 203 },58 {59 status: 'error'60 }61 ])62 );63 const res = await Mail.shareArticle({64 article: { slug: 'slug', title: 'title' },65 recipient: 'recipient',66 sender: { username: 'username' }67 });68 expect(res).to.be.a('object');69 expect(res)70 .to.have.property('status')71 .that.is.equal('error');72 expect(res).to.have.property('message');73 mockSGMailSend.restore();74 });75 it('should handle unexpected errors and return an error message', async () => {76 const mockSGMailSend = sinon77 .stub(sgMail, 'send')78 .returns(Promise.reject(new Error('mock reject')));79 const res = await Mail.sendVerification(user);80 expect(res).to.be.a('object');81 expect(res)82 .to.have.property('status')83 .that.is.equal('error');84 expect(res).to.have.property('message');85 mockSGMailSend.restore();86 });87 });88 describe('sendPasswordReset', () => {89 it('should return a successful status when email has been sent.', async () => {90 const mockSGMailSend = sinon.stub(sgMail, 'send').returns(91 Promise.resolve([92 { statusCode: 202 },93 {94 status: 'success'95 }96 ])97 );98 const res = await Mail.sendPasswordReset(99 'testingurl@authorshavenand.com',100 'http://localhost:3000/api/v1/reset-password/complete/{token_here}'101 );102 expect(res).to.be.a('object');103 expect(res)104 .to.have.property('status')105 .that.is.equal('success');106 mockSGMailSend.restore();107 });108 it('should return an error status when sending email fails', async () => {109 const mockSGMailSend = sinon.stub(sgMail, 'send').returns(110 Promise.resolve([111 { statusCode: 203 },112 {113 status: 'error'114 }115 ])116 );117 const res = await Mail.sendPasswordReset(118 'testingurl@authorshavenand.com',119 'http://localhost:3000/api/v1/reset-password/complete/{token_here}'120 );121 expect(res).to.be.a('object');122 expect(res)123 .to.have.property('status')124 .that.is.equal('error');125 expect(res).to.have.property('message');126 mockSGMailSend.restore();127 });128 it('should handle unexpected errors and return an error message', async () => {129 const mockSGMailSend = sinon130 .stub(sgMail, 'send')131 .returns(Promise.reject(new Error('mock reject')));132 const res = await Mail.sendPasswordReset(133 'testingurl@authorshavenand.com',134 'http://localhost:3000/api/v1/reset-password/complete/{token_here}'135 );136 expect(res).to.be.a('object');137 expect(res)138 .to.have.property('status')139 .that.is.equal('error');140 expect(res).to.have.property('message');141 mockSGMailSend.restore();142 });143 });...
account.js
Source:account.js
1const sgmail = require("@sendgrid/mail")2sgmail.setApiKey(process.env.sendGridApiKey)3const sendWelcomeMail = (email, name) => {4 console.log("Send welcome mail");5 sgmail.send({6 to: email,7 from: "pateljaykjp1@gmail.com",8 subject: "Thanks for joining us!",9 html: `10 <h1>Welcome To Auditoria <b>${name}</b></h1>11 <br><p>We would like to thank you for signing up to our service.We would love to hear what you think, if there is anything we can improve. If you have any questions, please reply to this Email. We are always happy to help!</p>12 <br><br> <h5><b>Thank You!</b></h5> 13 `14 })15}16const sendCancelationMail = (email, name) => {17 sgmail.send({18 to: email,19 from: "pateljaykjp1@gmail.com",20 subject: "You deleted your Account!",21 html: `22 <h1>Dear ${name},</h1>23 <br><p>Your Auditoria account has been deleted! Give your valuable feedback to us over a mail.</p>24 <br><br> 25 <p> Have a nice day!</p>26 <h5><b>Thank You!</b></h5> 27 `28 })29}30const sendVerificationPendingMail = (email, name) => {31 sgmail.send({32 to: email,33 from: "pateljaykjp1@gmail.com",34 subject: "Regarding Verification",35 html: `36 <h1>Welcome ${name} to Auditoria </h1>37 <br><p> We have received your request, Once we verified it we will let you know.</p>38 <br><br>39 <h5><b>Thank You for joining us!</b></h5> 40 `41 })42}43const sendVerificationAcceptedMail = (email, name) => {44 sgmail.send({45 to: email,46 from: "pateljaykjp1@gmail.com",47 subject: "Regarding Accepted Request",48 html: `49 <h1>Welcome again ${name}</h1>50 <br><p><b>Congratulations!</b> Your request has been Accepted. Now you are part of Auditoria.</p>51 <br><br>52 <h5><b>Thank You!</b></h5> 53 `54 })55}56const sendVerificationRejectedMail = (email, name) => {57 sgmail.send({58 to: email,59 from: "pateljaykjp1@gmail.com",60 subject: "Regarding Rejected Request",61 html: `62 <h1>Welcome ${name}</h1>63 <br><p>We have seen your request but Sorry to say, your request has been Rejected. Now you are part of Auditoria. We hope to see you again</p>64 <br><br>65 <h5><b>Thank You!</b></h5> 66 `67 })68}69const sendTicketConfirmationMail = (name,eventName,amout,eventDate,seatNumbers)=>{70 sgmail.send({71 to: email,72 from: "pateljaykjp1@gmail.com",73 subject: "Tickets Confirmation",74 text:`Hello ${name}, your tickets for ${eventName} on date ${eventDate} is confirmed.\npayment aount : ${amout}\nseats numbers : ${seatNumbers}`75 })76}77const sendTicketFaliedMail = (name,eventName,amout,eventDate,seatNumbers)=>{78 sgmail.send({79 to: email,80 from: "pateljaykjp1@gmail.com",81 subject: "Tickets Falied",82 text:`Hello ${name}, your tickets for ${eventName} on date ${eventDate} is falied.\npayment aount : ${amout}\nseats numbers : ${seatNumbers}`83 })84}85const sendCancleTicketMail = (name,eventName,eventDate,totalPrice)=>{86 sgmail.send({87 to: email,88 from: "pateljaykjp1@gmail.com",89 subject: "Tickets Cancellation",90 text:`Hello ${name}, your tickets for ${eventName} on date ${eventDate} is cancele now.\npayment aount : ${totalPrice}`91 })92}93const sendAuditoriumBookingConfirmationMail=(name,booking,cost)=>{94 sgmail.send({95 to: email,96 from: "pateljaykjp1@gmail.com",97 subject: "Auditorium Booking Confirmation",98 text:`Hello ${name}, your auditorium booking with ${booking.event_name} is confirmed.\nEvent Details,\n Date : ${booking.event_date},Time Slots : ${booking.timeSlots}`99 })100}101const sendAuditoriumBookingFaliedMail=(name,booking,cost)=>{102 sgmail.send({103 to: email,104 from: "pateljaykjp1@gmail.com",105 subject: "Auditorium Booking Confirmation",106 text:`Hello ${name}, your auditorium booking with ${booking.event_name} is falied.`107 })108}109module.exports = {110 sendWelcomeMail,111 sendCancelationMail,112 sendVerificationAcceptedMail,113 sendVerificationPendingMail,114 sendVerificationRejectedMail,115 sendTicketConfirmationMail,116 sendTicketFaliedMail,117 sendCancleTicketMail,118 sendAuditoriumBookingConfirmationMail,119 sendAuditoriumBookingFaliedMail...
email.test.js
Source:email.test.js
1const test = require('ava');2const sinon = require('sinon');3const SGMail = require('@sendgrid/mail');4const moment = require('moment');5const {6 createWeeklyEmail,7 createSampleEmail,8 sendSample,9 send,10 sendEmail,11} = require('../../utils/email');12const { USER, WEEKLY_METRICS, SAMPLE_METRICS, SENDGRID_SUCCESS } = require('../__fixtures__');13const { EMAIL_CONFIG, SENDGRID_API_KEY } = require('../../config');14const HTML_REGEX = /\s?<!doctype html>|(<html\b[^>]*>|<body\b[^>]*>|<x-[^>]+>)+/i;15test('generates weekly email', async t => {16 const email = await createWeeklyEmail({17 user: USER,18 metrics: WEEKLY_METRICS,19 date: moment().format('LL'),20 });21 t.is(email.to, USER.email);22 t.is(email.from, EMAIL_CONFIG.vanityAddress);23 t.is(email.subject, EMAIL_CONFIG.subject);24 t.regex(email.html, HTML_REGEX);25});26test('generates weekly email for empty metrics', async t => {27 const email = await createWeeklyEmail({28 user: USER,29 metrics: [],30 date: moment().format('LL'),31 });32 t.is(email.to, USER.email);33 t.is(email.from, EMAIL_CONFIG.vanityAddress);34 t.is(email.subject, EMAIL_CONFIG.subject);35 t.regex(email.html, HTML_REGEX);36 t.regex(email.html, /It seems like you don't have any repos yet/);37});38test('generates sample email', async t => {39 const email = await createSampleEmail({40 user: USER,41 metrics: SAMPLE_METRICS,42 });43 t.is(email.to, USER.email);44 t.is(email.from, EMAIL_CONFIG.vanityAddress);45 t.is(email.subject, EMAIL_CONFIG.sampleSubject);46 t.regex(email.html, HTML_REGEX);47});48test('generates sample email for empty metrics', async t => {49 const email = await createSampleEmail({50 user: USER,51 metrics: [],52 });53 t.is(email.to, USER.email);54 t.is(email.from, EMAIL_CONFIG.vanityAddress);55 t.is(email.subject, EMAIL_CONFIG.sampleSubject);56 t.regex(email.html, HTML_REGEX);57 t.regex(email.html, /It seems like you don't have any repos yet/);58});59test.serial('sends a weekly email', async t => {60 const SGMailSend = sinon.stub(SGMail, 'send');61 SGMailSend.returns(Promise.resolve(SENDGRID_SUCCESS));62 const data = {63 user: USER,64 metrics: WEEKLY_METRICS,65 date: moment().format('LL'),66 };67 const response = await send(data);68 t.true(SGMailSend.calledOnce);69 sinon.assert.calledWith(SGMailSend, createWeeklyEmail(data));70 t.deepEqual(response, SENDGRID_SUCCESS);71 SGMailSend.restore();72});73test.serial('sends a sample email', async t => {74 const SGMailSend = sinon.stub(SGMail, 'send');75 SGMailSend.returns(Promise.resolve(SENDGRID_SUCCESS));76 const data = {77 user: USER,78 metrics: SAMPLE_METRICS,79 };80 const response = await sendSample(data);81 t.true(SGMailSend.calledOnce);82 sinon.assert.calledWith(SGMailSend, createSampleEmail(data));83 t.deepEqual(response, SENDGRID_SUCCESS);84 SGMailSend.restore();85});86test.serial('sends an email', async t => {87 const SGMailSetApiKey = sinon.stub(SGMail, 'setApiKey');88 const SGMailSend = sinon.stub(SGMail, 'send');89 SGMailSend.returns(Promise.resolve(SENDGRID_SUCCESS));90 const email = {91 to: USER.email,92 from: EMAIL_CONFIG.vanityAddress,93 subject: EMAIL_CONFIG.subject,94 html: '<html><body><h1>Just Another Email</h1></body></html>',95 };96 const response = await sendEmail(email);97 t.true(SGMailSetApiKey.calledOnce);98 sinon.assert.calledWith(SGMailSetApiKey, SENDGRID_API_KEY);99 t.true(SGMailSend.calledOnce);100 sinon.assert.calledWith(SGMailSend, email);101 t.deepEqual(response, SENDGRID_SUCCESS);102 SGMailSetApiKey.restore();103 SGMailSend.restore();104});105test('handles invalid emails', async t => {106 const email = {107 to: 'not_a_valid_email',108 from: EMAIL_CONFIG.vanityAddress,109 subject: EMAIL_CONFIG.subject,110 html: '<html><body><h1>Just Another Email</h1></body></html>',111 };112 const error = await t.throwsAsync(sendEmail(email));113 t.is(error.message, 'Bad Request');...
Using AI Code Generation
1const sgMail = require('@sendgrid/mail');2const { send } = require('qawolf');3const fs = require('fs');4const csv = require('csv-parser');5const results = [];6fs.createReadStream('recipients.csv')7 .pipe(csv())8 .on('data', (data) => results.push(data))9 .on('end', () => {10 results.forEach((recipient) => {11 const msg = {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!