Skip to content

Commit

Permalink
WIP(tests): Getting tests up to par.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Sharp committed Feb 9, 2015
1 parent 98df613 commit 35d463a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 39 deletions.
22 changes: 15 additions & 7 deletions mock/socket-io.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var io = {
connect: createMockSocketObject
};
function io (){
var mockSocketObject = createMockSocketObject();
return mockSocketObject.connect();
}

function createMockSocketObject () {

Expand All @@ -9,7 +10,7 @@ function createMockSocketObject () {
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
},
once: function (ev, fn) {
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
(this._raw._listeners[ev] = this._raw._listeners[ev] || []).push(fn);
fn._once = true;
},
emit: function (ev, data) {
Expand All @@ -20,7 +21,7 @@ function createMockSocketObject () {
this.removeListener(ev, listener);
}
listener.apply(null, Array.prototype.slice.call(args, 1));
}.bind(this));
},this);
}
},
_listeners: {},
Expand All @@ -41,8 +42,15 @@ function createMockSocketObject () {
this._listeners = {};
}
},
disconnect: function () {},
connect: function () {}
disconnect: function () {
this.connected = false;
this.emit('disconnect');
},
connect: function () {
this.connected = true;
this.emit('connect');
return this;
}
};

return socket;
Expand Down
1 change: 0 additions & 1 deletion src/service/angular-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ angular.module('ngSails').provider('$sails', function () {
socket[eventName] = function (event, cb) {
if (cb !== null && angular.isFunction(cb)) {
socket['legacy_' + eventName](event, function (result) {
console.log('!!!', result);
angularify(cb, result);
});
}
Expand Down
160 changes: 129 additions & 31 deletions test/angular-sails.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ describe('Agnular Sails', function() {
$timeout,
$sails,
mockIoSocket,
spy;
spy,
methods = ['get', 'post', 'put', 'delete'],
response = {
success: {
body: 'Success!',
statusCode: 200
},
error: {
body: 'Error!',
statusCode: 500
},
fallbacksuccess: {
message: 'Success!',
status: 200 // previously we looked for 'status'
},
fallbackerror: {
message: 'Error!',
status: 500 // previously we looked for 'status'
}
};

beforeEach(module('ngSails'));

Expand All @@ -14,11 +33,11 @@ describe('Agnular Sails', function() {
$compile = _$compile_;
$timeout = _$timeout_;
$sails = _$sails_;
mockIoSocket = $sails;
mockIoSocket = $sails._raw;
spy = sinon.spy();
}));

describe('#on', function() {
describe('on', function() {

it('should apply asynchronously', function () {
$sails.on('event', spy);
Expand All @@ -27,33 +46,12 @@ describe('Agnular Sails', function() {
expect(spy).to.have.been.not.called;
$timeout.flush();

expect(spy).to.have.been.called;
expect(spy).to.have.been.calledOnce;
});

});

describe('#disconnect', function () {

it('should call the underlying socket.disconnect', function () {
mockIoSocket.disconnect = spy;
$sails.disconnect();
expect(spy).to.have.been.called;
});

});

describe('#connect', function () {

it('should call the underlying socket.connect', function () {
mockIoSocket.connect = spy;
$sails.connect();
expect(spy).to.have.been.called;
});

});


describe('#once', function () {
describe('once', function () {

it('should apply asynchronously', function () {
$sails.once('event', spy);
Expand All @@ -63,23 +61,123 @@ describe('Agnular Sails', function() {
expect(spy).to.have.been.not.called;
$timeout.flush();

expect(spy).to.have.been.called;
expect(spy).to.have.been.calledOnce;
});

it('should only run once', function () {
var counter = 0;
$sails.once('event', function () {
counter += 1;
});
$sails.once('event', spy);

mockIoSocket.emit('event');
mockIoSocket.emit('event');
$timeout.flush();

expect(counter).to.equal(1);
expect(spy).to.have.been.calledOnce;
});

});

methods.forEach(function(method){

describe(method, function() {

it('should return a promise', function () {
var promise = $sails[method]('test');

expect(promise['finally']).to.be.a('function');
expect(promise.then).to.be.a('function');
expect(promise.success).to.be.a('function');
expect(promise.error).to.be.a('function');

});

it('should return chainable success()', function () {
var promise = $sails[method]('test').success();

expect(promise['finally']).to.be.a('function');
expect(promise.then).to.be.a('function');
expect(promise.success).to.be.a('function');
expect(promise.error).to.be.a('function');

});

it('should return chainable error()', function () {
var promise = $sails[method]('test').error();

expect(promise['finally']).to.be.a('function');
expect(promise.then).to.be.a('function');
expect(promise.success).to.be.a('function');
expect(promise.error).to.be.a('function');

});

describe('response', function(){

var errorSpy;

beforeEach(function() {
mockIoSocket.on([method], function(ctx, cb){
cb(response[ctx.url]);
});
errorSpy = sinon.spy();
});

it('should resolve successes', function () {

$sails[method]('success').then(spy, errorSpy);
$scope.$digest();

expect(errorSpy).to.have.been.not.called;
expect(spy).to.have.been.calledOnce;
});

it('should reject errors', function () {

$sails[method]('error').then(errorSpy, spy);
$scope.$digest();

expect(errorSpy).to.have.been.not.called;
expect(spy).to.have.been.calledOnce;
});

it('should resolve successes with jwr fallback', function () {

$sails[method]('fallbacksuccess').then(spy, errorSpy);
$scope.$digest();

expect(errorSpy).to.have.been.not.called;
expect(spy).to.have.been.calledOnce;
});

it('should reject errors with jwr fallback', function () {

$sails[method]('fallbackerror').then(errorSpy, spy);
$scope.$digest();

expect(errorSpy).to.have.been.not.called;
expect(spy).to.have.been.calledOnce;
});

it('should call success for successes', function () {

$sails[method]('success').success(spy).error(errorSpy);
$scope.$digest();

expect(errorSpy).to.have.been.not.called;
expect(spy).to.have.been.calledOnce;
});

it('should call error for errors', function () {

$sails[method]('error').success(errorSpy).error(spy);
$scope.$digest();

expect(errorSpy).to.have.been.not.called;
expect(spy).to.have.been.calledOnce;
});

});

});
});
});

0 comments on commit 35d463a

Please sign in to comment.