Skip to content

Instantly share code, notes, and snippets.

@igorescobar
Created May 20, 2014 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorescobar/228f598b76cdbd443e98 to your computer and use it in GitHub Desktop.
Save igorescobar/228f598b76cdbd443e98 to your computer and use it in GitHub Desktop.
Testing sequelizejs success callback with mocked data
/*globals describe, it, context*/
/*jslint node: true, indent: 2*/
'use strict';
var HalBuilder,
chai = require('chai'),
should = chai.should(),
assert = chai.assert,
expect = chai.expect,
sinon = require('sinon'),
helpers = require('../test_helper');
HalBuilder = require('../../../helpers/hal_builder.js');
describe('HalBuilder', function () {
it('should have the #search method', function () {
expect(typeof HalBuilder.search).to.be.equal("function");
});
context("#search", function(){
describe("with no conditions", function(){
it("should should validate overall structure", function(){
var model = { tableName: 'Activities', findAndCountAll: sinon.stub() };
var mocked_return = {
count: 300,
rows: []
};
// BOOM!!! fn(mocked_return)
model.findAndCountAll.returns({success: function(fn) { fn(mocked_return); }});
var req = {
url: "http://test.domain.local/api/call/find",
_parsedUrl: {query: ''},
param: sinon.stub()
};
var iterate_callback = sinon.spy();
var success_callback = sinon.spy();
HalBuilder.search(req, model, {}, iterate_callback, success_callback);
sinon.assert.notCalled(iterate_callback, {});
sinon.assert.calledOnce(success_callback, {});
});
it("should return basic fields", function(){
var model = { tableName: 'Activities', findAndCountAll: sinon.stub() };
var mocked_return = {
count: 300,
rows: []
};
model.findAndCountAll.returns({success: function(fn) {
fn(mocked_return);
}});
var req = {
url: "http://test.domain.local/api/call/find",
_parsedUrl: {query: ''},
param: sinon.stub()
};
var iterate_callback = sinon.spy();
HalBuilder.search(req, model, {}, iterate_callback, function(result){
expect(result.resourceType).to.be.equal('activities');
expect(result.totalResults).to.be.equal(300);
expect(result.currentPage).to.be.equal(1);
expect(result.pageSize).to.be.equal(100);
expect(result.query).to.be.equal('');
expect(result.resources).to.be.eql([]);
expect(result.totalPages).to.be.eql(3);
expect(result.totalPages).to.be.eql(3);
expect(result._links.self.href).to.be.equal('http://test.domain.local/api/call/find');
expect(result._links.next.href).to.be.equal('http://test.domain.local/api/call/find?page=2');
expect(result._links.last.href).to.be.equal('http://test.domain.local/api/call/find?page=3');
});
sinon.assert.calledWith(model.findAndCountAll, { limit: 100, offset: 0 })
});
it("should return call iterate_callback", function(){
var model = { tableName: 'Activities', findAndCountAll: sinon.stub() };
var mocked_return = {
count: 300,
rows: [{teste: "teste"}]
};
model.findAndCountAll.returns({success: function(fn) {
fn(mocked_return);
}});
var req = {
url: "http://test.domain.local/api/call/find",
_parsedUrl: {query: ''},
param: sinon.stub()
};
HalBuilder.search(req, model, {}, function(result){
result.teste = "teste atendimento";
return result;
}, function(result){
expect(result.resources[0].teste).to.be.equal("teste atendimento");
});
sinon.assert.calledWith(model.findAndCountAll, { limit: 100, offset: 0 })
});
it("should paginate", function(){
var model = { tableName: 'Activities', findAndCountAll: sinon.stub() };
var mocked_return = {
count: 300,
rows: []
};
model.findAndCountAll.returns({success: function(fn) {
fn(mocked_return);
}});
var req = {
url: "http://test.domain.local/api/call/find?page=2",
_parsedUrl: {query: ''},
param: sinon.stub()
};
req.param.withArgs('page').returns(2);
req.param.withArgs('per_page').returns(10);
HalBuilder.search(req, model, {}, function(){}, function(result){
expect(result._links.self.href).to.be.equal('http://test.domain.local/api/call/find?page=2');
expect(result._links.previous.href).to.be.equal('http://test.domain.localapi/call/find?page=1');
expect(result._links.next.href).to.be.equal('http://test.domain.local/api/call/find?page=3');
expect(result._links.last.href).to.be.equal('http://test.domain.localapi/call/find?page=30');
});
sinon.assert.calledWith(model.findAndCountAll, { limit: 10, offset: 10 })
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment