Created
January 13, 2016 14:54
-
-
Save bendozy/3bc9953c861ad8f3c756 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
global.t = require('moment'); | |
var httpMocks = require('node-mocks-http'), | |
models = require('../../../server/models/'), | |
should = require('should'), | |
cohortController = require('../../../server/controllers/cohorts'); | |
describe('Server Cohort Controller Test', function() { | |
var mockCohort = { | |
"c1": { | |
id: 'cohort-1', | |
color: '#222222', | |
hidden: false, | |
invite_code: 'e331yop', | |
name: 'Test Cohort 1', | |
started_at: '2013-06-11 00:00:00+01', | |
story: 'Test Story 1' | |
}, | |
"c2": { | |
id: 'cohort-2', | |
color: '#000000', | |
hidden: true, | |
invite_code: 'jc31oop', | |
name: 'Test Cohort 2', | |
started_at: '2014-06-11 00:00:00+01', | |
story: 'Test Story 2' | |
}, | |
"c3": { | |
id: 'cohort-3', | |
color: '#FFFFFF', | |
hidden: true, | |
invite_code: 'de345tgd', | |
name: 'Test Cohort 3', | |
started_at: '2015-06-11 00:00:00+01', | |
story: 'Test Story 3' | |
} | |
}; | |
beforeEach(function(done) { | |
models.cohorts.destroy({ | |
where: {} | |
}).then(function() { | |
var cohorts = [mockCohort.c1, mockCohort.c2, mockCohort.c3]; | |
models.cohorts.bulkCreate(cohorts).then(function() { | |
done(); | |
}); | |
}); | |
}); | |
describe('cohorts index method test', function() { | |
var req = httpMocks.createRequest(); | |
var res = httpMocks.createResponse(); | |
cohortController.index(req, res); | |
it('should display all cohorts in the database', function(done) { | |
var data = JSON.parse(res._getData()); | |
data.length.should.equal(3); | |
res.statusCode.should.equal(200); | |
done(); | |
}); | |
var hiddenReq = httpMocks.createRequest({ | |
query: { | |
hidden: true | |
} | |
}); | |
var hiddenRes = httpMocks.createResponse(); | |
cohortController.index(hiddenReq, hiddenRes); | |
it('should display all cohorts where hidden field value is true', function(done) { | |
var data = JSON.parse(hiddenRes._getData()); | |
data.length.should.equal(2); | |
hiddenRes.statusCode.should.equal(200); | |
done(); | |
}); | |
var invalidReq = httpMocks.createRequest({ | |
query: { | |
hidden: 1 | |
} | |
}); | |
var invalidRes = httpMocks.createResponse(); | |
cohortController.index(invalidReq, invalidRes); | |
it('should return 500 status for invalid query parameter', function(done) { | |
invalidRes.statusCode.should.equal(500); | |
done(); | |
}); | |
}); | |
describe('cohorts show method test', function() { | |
var req = httpMocks.createRequest({ | |
params: { | |
id: mockCohort.c2.id | |
} | |
}); | |
var res = httpMocks.createResponse(); | |
cohortController.show(req, res); | |
it('should display a cohort with the correct id', function(done) { | |
var data = JSON.parse(res._getData()); | |
data.name.should.equal(mockCohort.c2.name); | |
data.color.should.equal(mockCohort.c2.color); | |
res.statusCode.should.equal(200); | |
done(); | |
}); | |
var incorrectIdReq = httpMocks.createRequest({ | |
params: { | |
id: "hello" | |
} | |
}); | |
var incorrectIdRes = httpMocks.createResponse(); | |
cohortController.show(incorrectIdReq, incorrectIdRes); | |
it('should return a 404 status for a non existing id', function(done) { | |
var data = JSON.parse(incorrectIdRes._getData()); | |
incorrectIdRes.statusCode.should.equal(404); | |
done(); | |
}); | |
var invalidIdReq = httpMocks.createRequest({ | |
params: { | |
id: 6955454455 | |
} | |
}); | |
var invalidIdRes = httpMocks.createResponse(); | |
cohortController.show(invalidIdReq, invalidIdRes); | |
it('should return a 500 status for an invalid id', function(done) { | |
invalidIdRes.statusCode.should.equal(500); | |
done(); | |
}); | |
}); | |
describe('cohorts update method test', function() { | |
updatedCohort = { | |
color: '#000000', | |
hidden: true, | |
invite_code: 'jupdate', | |
name: 'Updated Cohort', | |
started_at: '2014-06-11 00:00:00+01', | |
story: 'Updated Story' | |
}; | |
var req = httpMocks.createRequest({ | |
params: { | |
id: mockCohort.c2.id | |
}, | |
body: updatedCohort | |
}); | |
var res = httpMocks.createResponse(); | |
cohortController.update(req, res); | |
it('should update a cohort with the correct id', function(done) { | |
var data = JSON.parse(res._getData()); | |
data.name.should.equal(updatedCohort.name); | |
data.invite_code.should.equal(updatedCohort.invite_code); | |
res.statusCode.should.equal(200); | |
done(); | |
}); | |
var incorrectIdReq = httpMocks.createRequest({ | |
params: { | |
id: "hello", | |
body: updatedCohort | |
} | |
}); | |
var incorrectIdRes = httpMocks.createResponse(); | |
cohortController.update(incorrectIdReq, incorrectIdRes); | |
it('should return a 404 status for a non existing id', function(done) { | |
var data = JSON.parse(incorrectIdRes._getData()); | |
incorrectIdRes.statusCode.should.equal(404); | |
done(); | |
}); | |
var invalidIdReq = httpMocks.createRequest({ | |
params: { | |
id: 6955454455, | |
body: updatedCohort | |
} | |
}); | |
var invalidIdRes = httpMocks.createResponse(); | |
cohortController.show(invalidIdReq, invalidIdRes); | |
it('should return a 500 status for an invalid id', function(done) { | |
invalidIdRes.statusCode.should.equal(500); | |
done(); | |
}); | |
}); | |
describe('cohorts create method test', function() { | |
newCohort = { | |
id: 'cohort-5', | |
color: '#00000A', | |
hidden: true, | |
invite_code: 'jc31oowq', | |
name: 'Test Cohort 5', | |
started_at: '2014-06-11 00:00:00+01', | |
story: 'Test Story 5' | |
}; | |
var req = httpMocks.createRequest({ | |
body: newCohort | |
}); | |
var res = httpMocks.createResponse(); | |
cohortController.create(req, res); | |
it('should create a new cohort with the correct details', function(done) { | |
var data = JSON.parse(res._getData()); | |
data.name.should.equal(newCohort.name); | |
data.invite_code.should.equal(newCohort.invite_code); | |
res.statusCode.should.equal(200); | |
done(); | |
}); | |
}); | |
describe('cohorts destroy method test', function() { | |
var req = httpMocks.createRequest({ | |
params: { | |
id: mockCohort.c3.id | |
} | |
}); | |
var res = httpMocks.createResponse(); | |
cohortController.destroy(req, res); | |
it('should return a cohort with the correct id', function(done) { | |
res.statusCode.should.equal(200); | |
done(); | |
}); | |
var incorrectIdReq = httpMocks.createRequest(); | |
var incorrectIdRes = httpMocks.createResponse(); | |
cohortController.destroy(incorrectIdReq, incorrectIdRes); | |
it('should return a 500 status for an null id', function(done) { | |
incorrectIdRes.statusCode.should.equal(500); | |
done(); | |
}); | |
var invalidIdReq = httpMocks.createRequest({ | |
params: { | |
id: 6955454455 | |
} | |
}); | |
var invalidIdRes = httpMocks.createResponse(); | |
cohortController.destroy(invalidIdReq, invalidIdRes); | |
it('should return a 500 status for an invalid id', function(done) { | |
invalidIdRes.statusCode.should.equal(500); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment