Last active
May 30, 2017 17:50
-
-
Save kirill3333/0a679d2532a0ad2cb55d16665a6348f3 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
const proxyquire = require('proxyquire') | |
const sinon = require('sinon') | |
const faker = require('faker') | |
const assert = require('chai').assert | |
describe('todo/controller', () => { | |
describe('controller', () => { | |
let mdl | |
let modelStub, serializerStub, populateMethodStub, fakeData | |
let fakeSerializedData, fakeError | |
let mongoResponse | |
before(() => { | |
fakeData = faker.helpers.createTransaction() | |
fakeError = faker.lorem.word() | |
populateMethodStub = { | |
populate: sinon.stub().callsFake(() => mongoResponse) | |
} | |
modelStub = { | |
find: sinon.stub().callsFake(() => { | |
return populateMethodStub | |
}), | |
findOne: sinon.stub().callsFake(() => { | |
return populateMethodStub | |
}) | |
} | |
fakeSerializedData = faker.helpers.createTransaction() | |
serializerStub = { | |
serialize: sinon.stub().callsFake(() => { | |
return fakeSerializedData | |
}) | |
} | |
mdl = proxyquire('../todo/controller.js', | |
{ | |
'./model': modelStub, | |
'./serializer': serializerStub | |
} | |
) | |
}) | |
beforeEach(() => { | |
modelStub.find.resetHistory() | |
modelStub.findOne.resetHistory() | |
populateMethodStub.populate.resetHistory() | |
serializerStub.serialize.resetHistory() | |
}) | |
describe('getAll', () => { | |
it('should return serialized search result from mongodb', (done) => { | |
let resolveFn | |
let fakeCallback = new Promise((res, rej) => { | |
resolveFn = res | |
}) | |
mongoResponse = Promise.resolve(fakeData) | |
let fakeRes = { | |
json: sinon.stub().callsFake(() => { | |
resolveFn() | |
}) | |
} | |
mdl.getAll(null, fakeRes, null) | |
fakeCallback.then(() => { | |
sinon.assert.calledOnce(modelStub.find) | |
sinon.assert.calledWith(modelStub.find, {}) | |
sinon.assert.calledOnce(populateMethodStub.populate) | |
sinon.assert.calledWith(populateMethodStub.populate, '_user', '-password') | |
sinon.assert.calledOnce(serializerStub.serialize) | |
sinon.assert.calledWith(serializerStub.serialize, fakeData) | |
sinon.assert.calledOnce(fakeRes.json) | |
sinon.assert.calledWith(fakeRes.json, fakeSerializedData) | |
done() | |
}).catch(done) | |
}) | |
it('should call next callback if mongo db return exception', (done) => { | |
let fakeCallback = (err) => { | |
assert.equal(fakeError, err) | |
done() | |
} | |
mongoResponse = Promise.reject(fakeError) | |
let fakeRes = sinon.mock() | |
mdl.getAll(null, fakeRes, fakeCallback) | |
}) | |
}) | |
describe('getOne', () => { | |
it('should return serialized search result from mongodb', (done) => { | |
let resolveFn | |
let fakeCallback = new Promise((res, rej) => { | |
resolveFn = res | |
}) | |
mongoResponse = Promise.resolve(fakeData) | |
let fakeRes = { | |
json: sinon.stub().callsFake(() => { | |
resolveFn() | |
}) | |
} | |
let fakeReq = { | |
params: { | |
id: faker.random.number() | |
}, | |
user: { | |
_id: faker.random.number() | |
} | |
} | |
let findParams = { | |
'_id': fakeReq.params.id, | |
'_user': fakeReq.user._id | |
} | |
mdl.getOne(fakeReq, fakeRes, null) | |
fakeCallback.then(() => { | |
sinon.assert.calledOnce(modelStub.findOne) | |
sinon.assert.calledWith(modelStub.findOne, findParams) | |
sinon.assert.calledOnce(populateMethodStub.populate) | |
sinon.assert.calledWith(populateMethodStub.populate, '_user', '-password') | |
sinon.assert.calledOnce(serializerStub.serialize) | |
sinon.assert.calledWith(serializerStub.serialize, fakeData) | |
sinon.assert.calledOnce(fakeRes.json) | |
sinon.assert.calledWith(fakeRes.json, fakeSerializedData) | |
done() | |
}).catch(done) | |
}) | |
it('should call next callback if mongodb return exception', (done) => { | |
let fakeReq = { | |
params: { | |
id: faker.random.number() | |
}, | |
user: { | |
_id: faker.random.number() | |
} | |
} | |
let fakeCallback = (err) => { | |
assert.equal(fakeError, err) | |
done() | |
} | |
mongoResponse = Promise.reject(fakeError) | |
let fakeRes = sinon.mock() | |
mdl.getOne(fakeReq, fakeRes, fakeCallback) | |
}) | |
it('should call next callback with error if mongodb return empty result', (done) => { | |
let fakeReq = { | |
params: { | |
id: faker.random.number() | |
}, | |
user: { | |
_id: faker.random.number() | |
} | |
} | |
let expectedError = new Error('No todo item found.') | |
let fakeCallback = (err) => { | |
assert.equal(expectedError.message, err.message) | |
done() | |
} | |
mongoResponse = Promise.resolve(null) | |
let fakeRes = sinon.mock() | |
mdl.getOne(fakeReq, fakeRes, fakeCallback) | |
}) | |
}) | |
}) | |
}) |
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
const proxyquire = require('proxyquire') | |
const sinon = require('sinon') | |
const faker = require('faker') | |
describe('todo/model', () => { | |
describe('todo schema', () => { | |
let mongooseStub, SchemaConstructorSpy | |
let ObjectIdFake, mongooseModelSpy, SchemaSpy | |
before(() => { | |
ObjectIdFake = faker.lorem.word() | |
SchemaConstructorSpy = sinon.spy() | |
SchemaSpy = sinon.spy() | |
class SchemaStub { | |
constructor(...args) { | |
SchemaConstructorSpy(...args) | |
return SchemaSpy | |
} | |
} | |
SchemaStub.Types = { | |
ObjectId: ObjectIdFake | |
} | |
mongooseModelSpy = sinon.spy() | |
mongooseStub = { | |
"Schema": SchemaStub, | |
"model": mongooseModelSpy | |
} | |
proxyquire('../todo/model.js', | |
{ | |
'mongoose': mongooseStub | |
} | |
) | |
}) | |
it('should return new Todo model by schema', () => { | |
let todoSchema = { | |
title: { | |
type: String | |
}, | |
_user: { | |
type: ObjectIdFake, | |
ref: 'User' | |
} | |
} | |
sinon.assert.calledOnce(SchemaConstructorSpy) | |
sinon.assert.calledWith(SchemaConstructorSpy, todoSchema) | |
sinon.assert.calledOnce(mongooseModelSpy) | |
sinon.assert.calledWith(mongooseModelSpy, 'Todo', SchemaSpy) | |
}) | |
}) | |
}) |
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
const proxyquire = require('proxyquire') | |
const sinon = require('sinon') | |
const faker = require('faker') | |
describe('todo/routes', () => { | |
describe('router', () => { | |
let expressStub, controllerStub, RouterStub, rootRouteStub, idRouterStub | |
before(() => { | |
rootRouteStub = { | |
"get": sinon.stub().callsFake(() => rootRouteStub), | |
"post": sinon.stub().callsFake(() => rootRouteStub) | |
} | |
idRouterStub = { | |
"get": sinon.stub().callsFake(() => idRouterStub), | |
"put": sinon.stub().callsFake(() => idRouterStub), | |
"delete": sinon.stub().callsFake(() => idRouterStub) | |
} | |
RouterStub = { | |
route: sinon.stub().callsFake((route) => { | |
if (route === '/:id') { | |
return idRouterStub | |
} | |
return rootRouteStub | |
}) | |
} | |
expressStub = { | |
Router: sinon.stub().returns(RouterStub) | |
} | |
controllerStub = { | |
getAll: sinon.mock(), | |
create: sinon.mock(), | |
getOne: sinon.mock(), | |
update: sinon.mock(), | |
delete: sinon.mock() | |
} | |
proxyquire('../todo/routes.js', | |
{ | |
'express': expressStub, | |
'./controller': controllerStub | |
} | |
) | |
}) | |
it('should map root get router with getAll controller', () => { | |
sinon.assert.calledWith(RouterStub.route, '/') | |
sinon.assert.calledWith(rootRouteStub.get, controllerStub.getAll) | |
}) | |
it('should map root post router with create controller', () => { | |
sinon.assert.calledWith(RouterStub.route, '/') | |
sinon.assert.calledWith(rootRouteStub.post, controllerStub.create) | |
}) | |
it('should map /:id get router with getOne controller', () => { | |
sinon.assert.calledWith(RouterStub.route, '/:id') | |
sinon.assert.calledWith(idRouterStub.get, controllerStub.getOne) | |
}) | |
it('should map /:id put router with update controller', () => { | |
sinon.assert.calledWith(RouterStub.route, '/:id') | |
sinon.assert.calledWith(idRouterStub.put, controllerStub.update) | |
}) | |
it('should map /:id delete router with delete controller', () => { | |
sinon.assert.calledWith(RouterStub.route, '/:id') | |
sinon.assert.calledWith(idRouterStub.delete, controllerStub.delete) | |
}) | |
}) | |
}) |
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
const proxyquire = require('proxyquire') | |
const sinon = require('sinon') | |
describe('todo/serializer', () => { | |
describe('json serializer', () => { | |
let JSONAPISerializerStub, SerializerConstructorSpy | |
before(() => { | |
SerializerConstructorSpy = sinon.spy() | |
class SerializerStub { | |
constructor(...args) { | |
SerializerConstructorSpy(...args) | |
} | |
} | |
JSONAPISerializerStub = { | |
Serializer: SerializerStub | |
} | |
proxyquire('../todo/serializer.js', | |
{ | |
'jsonapi-serializer': JSONAPISerializerStub | |
} | |
) | |
}) | |
it('should return new instance of Serializer', () => { | |
let schema = { | |
attributes: ['title', '_user'] | |
, | |
_user: { | |
ref: 'id', | |
attributes: ['username'] | |
} | |
} | |
sinon.assert.calledOnce(SerializerConstructorSpy) | |
sinon.assert.calledWith(SerializerConstructorSpy, 'todos', schema) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment