Skip to content

Commit

Permalink
feat($off): Fix code and adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSharpieOne committed Jul 2, 2015
1 parent afe0b54 commit 8929895
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-sails",
"version": "1.1.2",
"version": "1.1.4",
"authors": [
"Jan-Oliver Pantel <[email protected]>",
"Evan Sharp <[email protected]>"
Expand Down
10 changes: 7 additions & 3 deletions dist/angular-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ angular.module('ngSails', ['ng']);
}

function wrapEvent(eventName) {
if(socket[eventName] || socket._raw && socket._raw[eventName]){
if(socket[eventName] || socket._raw && socket._raw[eventName]) {
socket['legacy_' + eventName] = socket[eventName] || socket._raw[eventName];
socket[eventName] = function(event, cb) {
if (cb !== null && angular.isFunction(cb)) {
socket['legacy_' + eventName](event, function(result) {
var wrapEventFn = null;
if (eventName == 'off') {
return socket['legacy_' + eventName](event, cb);
}else if (cb !== null && angular.isFunction(cb)) {
socket['legacy_' + eventName](event, wrapEventFn = function(result) {
$rootScope.$evalAsync(cb.bind(socket, result));
});
}
return wrapEventFn;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-sails.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-sails",
"private": true,
"version": "1.1.2",
"version": "1.1.4",
"description": "An angular provider for using the sails socket.io api",
"scripts": {
"build": "gulp build-js",
Expand Down
14 changes: 6 additions & 8 deletions src/service/angular-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,12 @@
socket['legacy_' + eventName] = socket[eventName] || socket._raw[eventName];
socket[eventName] = function(event, cb) {
var wrapEventFn = null;
if (cb !== null && angular.isFunction(cb)) {
if (eventName == 'off') {
socket['legacy_' + eventName](event, cb);
} else {
socket['legacy_' + eventName](event, wrapEventFn = function(result) {
$rootScope.$evalAsync(cb.bind(socket, result));
});
}
if (eventName == 'off') {
return socket['legacy_' + eventName](event, cb);
}else if (cb !== null && angular.isFunction(cb)) {
socket['legacy_' + eventName](event, wrapEventFn = function(result) {
$rootScope.$evalAsync(cb.bind(socket, result));
});
}
return wrapEventFn;
};
Expand Down
91 changes: 87 additions & 4 deletions test/angular-sails-service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,102 @@ describe('Agnular Sails service', function() {
describe('on', function() {

it('should apply asynchronously', function () {
var eventHandler = $sails.on('event', spy);
$sails.on('event', spy);
mockIoSocket.emit('event');

expect(spy).to.have.been.not.called;
$scope.$digest();

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

$sails.off('event', eventHandler);
});

it('should allow multiple listeners for the same event', function () {
var eventSpy = sinon.spy()
$sails.on('event', spy);
$sails.on('event', eventSpy);
mockIoSocket.emit('event');
$scope.$digest();

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

it('should call the correct lisener', function () {
var eventSpy = sinon.spy()
$sails.on('event', spy);
$sails.on('anotherEvent', eventSpy);
mockIoSocket.emit('event');
$scope.$digest();

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

});

describe('off', function() {

describe('by event name only', function(){

it('should remove all event listener by the given name', function () {
var eventSpy = sinon.spy()
$sails.on('event', spy);
$sails.on('event', eventSpy);
$sails.off('event');
mockIoSocket.emit('event');
$scope.$digest();

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

it('should only the listeners that match the given event name', function () {
var eventSpy = sinon.spy()
var anotherEventSpy = sinon.spy()
$sails.on('event', spy);
$sails.on('event', eventSpy);
$sails.on('anotherEvent', anotherEventSpy);

$sails.off('event');
mockIoSocket.emit('event');
mockIoSocket.emit('anotherEvent');
$scope.$digest();

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

});

describe('by event name and function', function(){

it('should remove the listener with that function', function () {
var listener = $sails.on('event', spy);
$sails.off('event', listener);
mockIoSocket.emit('event');
$scope.$digest();

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

it('should only remove the listener with the function', function () {
var eventSpy = sinon.spy()
var anotherEventSpy = sinon.spy()
$sails.on('event', spy);
var listner = $sails.on('event', eventSpy);
$sails.on('anotherEvent', anotherEventSpy);

$sails.off('event', listner);
mockIoSocket.emit('event');
mockIoSocket.emit('anotherEvent');
$scope.$digest();

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

});

});
Expand Down

0 comments on commit 8929895

Please sign in to comment.