Skip to content

Commit

Permalink
test(formatters): sinon spying on calls
Browse files Browse the repository at this point in the history
  • Loading branch information
thepian committed Mar 19, 2015
1 parent caba43c commit e133cdf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
8 changes: 5 additions & 3 deletions lib/client/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ module.exports = function(ss,options) {
addCall(formatter,true);
break;
case 'string':
var modPath = './formatters/' + nameOrModule;
if (!require.resolve(modPath)) {
var modPath = './formatters/' + nameOrModule, mod;
try {
mod = require(modPath);
}
catch(ex) {
throw new Error('The ' + nameOrModule + ' formatter is not supported by SocketStream internally. Please pass a compatible module instead');
}
var mod = require(modPath);
formatter = mod(ss, config, options);
addCall(formatter,true);
break;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@
"npm-dview": "1.1.2",
"shelljs": "0.3.0",
"should": "4.6.1",
"sinon": "^1.14.1",
"supertest": "0.15.0"
},
"scripts": {
"cover-test": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha test/unit/**/* && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"checkDependencies": "node_modules/.bin/npm-dview",
"lint": "node_modules/.bin/jshint .",
"notes": "node_modules/.bin/notes",
"test": "node_modules/.bin/mocha test/unit/**/* --reporter spec",
"test": "node_modules/.bin/mocha test/unit/**/* --reporter spec --require should --require sinon",
"testDebug": "node_modules/.bin/mocha --debug-brk test/unit/**/* --reporter spec"
},
"directories": {
Expand Down
46 changes: 25 additions & 21 deletions test/unit/client/formatters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path'),
should = require('should'),
sinon = require('sinon'),
ss = require( '../../../lib/socketstream'),
options = ss.client.options;

Expand Down Expand Up @@ -113,10 +114,10 @@ var path = require('path'),


it('should throw an error if the formatter is not supported by SocketStream internally', function() {
should(function() {
(function() {
ss.client.formatters.add('not-there',{});

}).throw(Error);
}).should.throw(Error('The \'./formatters/not-there\' '));
});

});
Expand Down Expand Up @@ -189,7 +190,7 @@ var path = require('path'),
ss.client.forget();
});

it('should forward exceptions returned', function() {
it('should forward exceptions returned', function(done) {

var formatter = {
init: function (root, config) {
Expand All @@ -214,15 +215,20 @@ var path = require('path'),
ss.api.client.formatters = ss.client.formatters.load();

ss.api.client.formatters.should.be.type('object');
var concrete = ss.api.client.formatters.a;
concrete.call('abc/def.a',{},function(out) {
out.should.be.equal('not called');
}, function(err) {
err.message.should.be.equal('c');
});
var concrete = ss.api.client.formatters.a,
resolve = sinon.spy(),
reject = sinon.spy(function(err) {
err.message.should.be.equal('c');
done();
});
concrete.call('abc/def.a',{}, resolve, reject);
//resolve.calledWith().should.equal(false);
//resolve.calledWith({message:''}).should.equal(true);
sinon.assert.notCalled(resolve);
sinon.assert.calledOnce(reject);
});

it('should forward exceptions thrown', function() {
it('should forward exceptions thrown', function(done) {

var formatter = {
init: function (root, config) {
Expand All @@ -247,20 +253,18 @@ var path = require('path'),
ss.api.client.formatters = ss.client.formatters.load();

ss.api.client.formatters.should.be.type('object');
var concrete = ss.api.client.formatters.a;
concrete.call('abc/def.a',{},function(out) {
out.should.be.equal('not called');
}, function(err) {
err.message.should.be.equal('c');
});
var concrete = ss.api.client.formatters.a,
resolve = sinon.spy(),
reject = sinon.spy(function(err) {
err.message.should.be.equal('c');
done();
});
concrete.call('abc/def.a',{},resolve,reject);
sinon.assert.notCalled(resolve);
sinon.assert.calledOnce(reject);
});

it('should format JavaScript comment',function() {
var client = ss.client.define('abc', {
css: './abc/style.css',
code: './abc/index.a',
view: './abc/abc.html'
});

var formatter = {
init: function (root, config) {
Expand Down

0 comments on commit e133cdf

Please sign in to comment.