Skip to content

Commit

Permalink
Merge pull request #3 from jefferson-health/createSpec
Browse files Browse the repository at this point in the history
added update and param tests
  • Loading branch information
ramirezd42 committed Feb 3, 2016
2 parents 1e6ed3f + 200b756 commit 03647d6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
2 changes: 1 addition & 1 deletion test/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var createFns = require('../src/create');

describe('create', function() {
describe('Create Functions', function() {

describe('beforeCreate', function() {

Expand Down
2 changes: 1 addition & 1 deletion test/destroySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var destroyFns = require('../src/destroy');

describe('destroy', function() {
describe('Destroy Functions', function() {

describe('beforeDestroy', function() {

Expand Down
2 changes: 1 addition & 1 deletion test/findAllSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var findAllFns = require('../src/findAll');

describe('findAll', function() {
describe('Find All Functions', function() {
describe('beforeFindAll', function() {
it('should call next by default', () => {
let nextSpy = sinon.spy();
Expand Down
2 changes: 1 addition & 1 deletion test/findSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var findFns = require('../src/find');

describe('find', function() {
describe('Find Functions', function() {

describe('beforeFind', function() {
it('should call next by default', () => {
Expand Down
35 changes: 35 additions & 0 deletions test/paramSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var paramFn = require('../src/param');

describe('Param Function', function() {
describe('param', function() {
it('should call find on the resource for the resourceId and assign the result to req', function (done) {
let resourceId = 1;
let resource = {id: resourceId};
let resolved = Promise.resolve(resource);
let next = sinon.spy();
let req = {
with: [ 'relations' ],
query: {where: {id: {'=' : 1}}}
};
let _this = {
resource: {
find: function() {
return resolved;
}
},
errorHandler: sinon.spy()
};
sinon.spy(_this.resource, 'find');
paramFn.call(_this, req, {}, next, resourceId);

resolved.then(function() {
_this.resource.find.should.have.been.calledWith(resourceId, Object.assign({with: req.with},req.query));
next.should.have.been.calledOnce;
req.resource.should.equal(resource);
done();
});
});
});
});
71 changes: 71 additions & 0 deletions test/updateSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

var updateFns = require('../src/update');

describe('Update Functions', function() {
describe('beforeUpdate', function() {
it('should call next by default', () => {
let nextSpy = sinon.spy();
updateFns.beforeUpdate({}, {}, nextSpy);
nextSpy.should.have.been.calledOnce;
});
});

describe('update', function() {
it('should call update on the resource and assign the result to req', function (done) {
let updatedResource = {id: 1};
let resolved = Promise.resolve(updatedResource);
let next = sinon.spy();
let req = { body: {
id: 0
}};
let _this = {
resource: {
update: function() {
return resolved;
}
},
errorHandler: sinon.spy()
};
sinon.spy(_this.resource, 'update');
updateFns.update.call(_this, req, {}, next);

resolved.then(function() {
_this.resource.update.should.have.been.calledWith(req.body);
next.should.have.been.calledOnce;
req.updatedResource.should.equal(updatedResource);
done();
});
});
});

describe('afterUpdate', function() {
it('should send a 200 response and the updated resource by default', function() {
let res = {
status: function() {
return res;
},
end: function() {
return res;
},
send: function() {
return res;
}
};
let req = {
updatedResource: {
id: 2
}
};
sinon.spy(res,'status');
sinon.spy(res,'send');
sinon.spy(res,'end');
updateFns.afterUpdate(req, res);
res.status.should.have.been.calledOnce;
res.status.should.have.been.calledWith(200);
res.send.should.have.been.calledWith(req.updatedResource);
res.end.should.have.been.calledOnce;
res.end.should.have.been.calledAfter(res.send);
});
});
});

0 comments on commit 03647d6

Please sign in to comment.