From f083500cc772ce09fe586f9a70b198b6e479e85a Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Thu, 12 Nov 2020 21:28:37 -0400 Subject: [PATCH] feat(contact): Increased contact controller to 100% test coverage --- test/a04-contact.spec.js | 147 +++++++++++++++++++++++++++++++++++---- 1 file changed, 134 insertions(+), 13 deletions(-) diff --git a/test/a04-contact.spec.js b/test/a04-contact.spec.js index 1983e69..0f1d843 100644 --- a/test/a04-contact.spec.js +++ b/test/a04-contact.spec.js @@ -1,13 +1,27 @@ const config = require('../config') const axios = require('axios').default const assert = require('chai').assert +const sinon = require('sinon') // Mock data // const mockData = require('./mocks/contact-mocks') const LOCALHOST = `http://localhost:${config.port}` +const mockContext = require('./mocks/ctx-mock').context +const ContactController = require('../src/modules/contact/controller') +let uut +let sandbox + describe('Contact', () => { + beforeEach(() => { + uut = new ContactController() + + sandbox = sinon.createSandbox() + }) + + afterEach(() => sandbox.restore()) + describe('POST /contact/email', () => { it('should throw error if email property is not provided', async () => { try { @@ -112,12 +126,7 @@ describe('Contact', () => { ) } }) - /* I presented problems here trying to use sandbox in this test */ - /* it('should send email with all input', async () => { - // Mock live network calls. - sandbox.stub( - contactController.transporter, 'sendMail') - .resolves(mockData) + it('should throw error if payloadTitle property is not string', async () => { try { const options = { method: 'POST', @@ -125,18 +134,130 @@ describe('Contact', () => { data: { obj: { email: 'email@email.com', - formMessage: 'test email', - name: 'test name', - payloadtitle: 'test title' + formMessage: 'test message', + payloadTitle: 1 } } } - const result = await axios(options) - console.log('RESULT', result.data) + + await axios(options) + + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + // console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) + assert(false, 'Unexpected result') + } catch (err) { + assert.equal(err.response.status, 500) + assert.include( + err.response.data, + "Property 'payloadTitle' must be a string!" + ) + } + }) + it('should throw error if email list provided is not a array', async () => { + try { + const options = { + method: 'POST', + url: `${LOCALHOST}/contact/email`, + data: { + obj: { + email: 'email@email.com', + formMessage: 'test message', + payloadTitle: 'title', + emailList: 1 + } + } + } + + await axios(options) + + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + // console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) + assert(false, 'Unexpected result') + } catch (err) { + assert.equal(err.response.status, 500) + assert.include( + err.response.data, + "Property 'emailList' must be a array of emails!" + ) + } + }) + it('should throw error if email list provided is a empty array', async () => { + try { + const options = { + method: 'POST', + url: `${LOCALHOST}/contact/email`, + data: { + obj: { + email: 'email@email.com', + formMessage: 'test message', + payloadTitle: 'title', + emailList: [] + } + } + } + + await axios(options) + + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + // console.log(`result stringified: ${JSON.stringify(result, null, 2)}`) + assert(false, 'Unexpected result') + } catch (err) { + assert.equal(err.response.status, 500) + assert.include( + err.response.data, + "Property 'emailList' must be a array of emails!" + ) + } + }) + it('should send email with minimun input', async () => { + try { + // Mock live network calls. + sandbox.stub( + uut.nodemailer, 'sendEmail') + .resolves(true) + + // Mock the context object. + const ctx = mockContext() + ctx.request = { + body: { + obj: { + email: 'email@email.com', + formMessage: 'test message', + payloadTitle: 'title' + } + } + } + await uut.email(ctx) } catch (err) { - // console.log(err) assert(false, 'Unexpected result') } - }) */ + }) + it('should send email with all input', async () => { + try { + // Mock live network calls. + sandbox.stub( + uut.nodemailer, 'sendEmail') + .resolves(true) + + // Mock the context object. + const ctx = mockContext() + ctx.request = { + body: { + obj: { + email: 'email@email.com', + formMessage: 'test message', + payloadTitle: 'title', + emailList: ['email@email.com'] + } + } + } + await uut.email(ctx) + } catch (err) { + assert(false, 'Unexpected result') + } + }) }) })