Skip to content

Commit

Permalink
added events controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
isabellachen committed Oct 23, 2017
1 parent c6d7bee commit dafb51f
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 19 deletions.
23 changes: 13 additions & 10 deletions server/controllers/events.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const mongoose = require('mongoose');
const Story = require('../model/story.model');
const Event = require('../model/event.model').Event;
const Attachment = require('../model/event.model').Attachment;
const Coordinates = require('../model/event.model').Coordinates;
const Location = require('../model/event.model').Location;

require('../db')('mapstory-backend-test');

//Adds event to events array within story object
const addEvent = async (ctx, next) => {
Expand All @@ -16,22 +18,22 @@ const addEvent = async (ctx, next) => {
const attachmentsData = ctx.request.body.attachments;
attachments = await Promise.all(attachmentsData.map(async attachment => {
let attachmentData;
if (attachment.type === 'link') {
if (attachment.type === 'Link') {
attachmentData = {
type: attachment.type,
url: attachment.url,
imageUrl: attachment.imageUrl,
urlImg: attachment.urlImg,
title: attachment.title,
};
} else if (attachment.type === 'text') {
} else if (attachment.type === 'Text') {
attachmentData = {
type: attachment.type,
text: attachment.text,
};
} else if (attachment.type === 'image') {
} else if (attachment.type === 'Image') {
attachmentData = {
type: attachment.type,
imageUrl: attachment.imageUrl,
urlImg: attachment.urlImg,
};
} else {
attachmentData = {
Expand All @@ -43,14 +45,15 @@ const addEvent = async (ctx, next) => {
}));
}

const coordinates = ctx.request.body.coordinates;
const locationData = ctx.request.body.location;
const location = await Location.create(locationData);

const eventData = {
title: ctx.request.body.title,
startTime: ctx.request.body.startTime,
mapLocation: ctx.request.body.mapLocation,
dateAndTime: ctx.request.body.dateAndTime,
coordinates,
location,
attachments,
};
const createdEvent = await Event.create(eventData);
Expand Down Expand Up @@ -85,8 +88,7 @@ const editEvent = async (ctx, next) => {
if (data.startTime) updatedProps.map = data.startTime;
if (data.mapLocation) updatedProps.duration = data.mapLocation;
if (data.dateAndTime) updatedProps.tagLine = data.dateAndTime;
if (data.attachments) updatedProps.attachments = data.attachments;
if (data.coordinates) updatedProps.coordinates = data.coordinates;
if (data.attachments) updatedProps.published = data.attachments;

const eventId = ctx.params.eventId;
await Event.findOneAndUpdate({'_id': eventId}, {$set: updatedProps});
Expand All @@ -111,6 +113,7 @@ const deleteEvent = async (ctx, next) => {
}
}
story.save();
// await Event.findByIdAndRemove(ctx.params.eventId)
ctx.status = 204;
} catch (error) {
throw (401, 'Could not edit event!');
Expand Down
17 changes: 8 additions & 9 deletions server/model/event.model.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Story = require('../model/story.model');

const attachmentSchema = new Schema({
type: String,
url: String,
imageUrl: String,
urlImg: String,
title: String,
text: String,
});

const coordinatesSchema = new Schema({
lat: Number,
lng: Number,
const locationSchema = new Schema({
lat: String,
lng: String,
});

const eventSchema = new Schema({
title: String,
startTime: String,
dateAndTime: String,
dateAndTime: Date,
mapLocation: String,
coordinates: [coordinatesSchema],
location: locationSchema,
attachments: [attachmentSchema],
});

const Event = mongoose.model('Event', eventSchema);
const Coordinates = mongoose.model('Coordinates', coordinatesSchema);
const Location = mongoose.model('Location', locationSchema);
const Attachment = mongoose.model('Attachment', attachmentSchema);

module.exports = {
Event,
Coordinates,
Location,
Attachment,
};
165 changes: 165 additions & 0 deletions server/tests/events.controller.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
const chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);
chai.use(chaiAsPromised);
chai.should();
require('chai').should();
const Event = require('../controller/events.controller');
const proxyquire = require('proxyquire');

describe('addEvents', () => {

it('should not create events if no title is provided',
async () => {
const ctx = {
request: {
body: {
title: '',
startTime: 38,
dateAndTime: 'Thurs 21 December',
mapLocation: 'Barcelona',
attachments: [],
}
}
};
await Event.addEvent(ctx).should.be.rejected;
}
);


it('should search for a story on the DB & save to it',
async () => {
const spy = sinon.spy();
const EventController = proxyquire('../controller/events.controller', {
'../model/story.model': {
'@noCallThru': true,
findOne: () => ({
id: 123,
save: spy,
events: [],
})
}
});
const ctx = {
params: {
id: 42
},
request: {
body: {
title: 'This is a story',
startTime: 38,
dateAndTime: 'Thurs 21 December',
mapLocation: 'Barcelona',
attachments: [],
}
}
};
await EventController.addEvent(ctx);
spy.calledOnce.should.equal(true);
}
);


it('should create an event',
async () => {
const spy = sinon.spy();
const ctx = {
params: {
id: 42
},
request: {
body: {
title: 'This is a story',
startTime: 38,
mapLocation: 'Barcelona',
dateAndTime: 'Thurs 21 December',
attachments: [],
}
}
};
const mockEvent = {
title: ctx.request.body.title,
startTime: ctx.request.body.startTime,
mapLocation: ctx.request.body.mapLocation,
dateAndTime: ctx.request.body.dateAndTime,
attachments: ctx.request.body.attachments
};
const EventController = proxyquire('../controller/events.controller',

{
'../model/story.model': {
'@noCallThru': true,
findOne: () => ({
id: 123,
save: spy,
events: [],
})
}
},

{
'../model/event.model': {
'@noCallThru': true,
create: () => mockEvent
}
});

await EventController.addEvent(ctx);
ctx.request.body.should.eql(mockEvent);
}
);


it('should be called with the provided arguments',
function () {
var addEventSpy = sinon.spy(Event.addEvent);
Event.addEvent(true, true);
addEventSpy.calledWith(true, true);
// addEventSpy.restore();
}
);



it('should update events when edited', async () => {

});




it('should have unique startTime');

it('should update attachments');

});



describe('Edit Event', function () {


});

describe('Delete Event', function () {

it('should remove event from DB when deleted', async () => {




});

});


describe('Attachments', () => {
it('should validate text format');
it('should validate tweet format');
it('should validate image format');
it('should validate video format');
it('should validate audio format');
it('should validate link format');
});

0 comments on commit dafb51f

Please sign in to comment.