Skip to content

Commit 977c46e

Browse files
committed
Merge pull request #6 from carlosbaraza/master
Adapt mock to current version of the mocked library.
2 parents 8bc9b7b + dbf35c9 commit 977c46e

3 files changed

Lines changed: 41 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
/node_modules
3+
/bower_components

angular-socket.io-mock.js

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,40 @@
22
var ng = angular.module('btford.socket-io',[])
33
ng.provider('socketFactory',function(){
44
this.$get = function($rootScope){
5-
6-
var obj = {}
7-
obj.events = {}
8-
obj.emits = {}
9-
10-
// intercept 'on' calls and capture the callbacks
11-
obj.on = function(eventName, callback){
12-
if(!this.events[eventName]) this.events[eventName] = []
13-
this.events[eventName].push(callback)
14-
}
15-
16-
// intercept 'emit' calls from the client and record them to assert against in the test
17-
obj.emit = function(eventName){
18-
var args = Array.prototype.slice.call(arguments,1)
19-
20-
if(!this.emits[eventName])
21-
this.emits[eventName] = []
22-
this.emits[eventName].push(args)
23-
}
24-
25-
//simulate an inbound message to the socket from the server (only called from the test)
26-
obj.receive = function(eventName){
27-
var args = Array.prototype.slice.call(arguments,1)
28-
29-
if(this.events[eventName]){
30-
31-
angular.forEach(this.events[eventName], function(callback){
32-
$rootScope.$apply(function() {
33-
34-
callback.apply(this, args)
35-
})
36-
})
37-
}
38-
}
39-
40-
return obj
41-
}
42-
})
5+
return function socketFactory () {
6+
var obj = {};
7+
obj.events = {};
8+
obj.emits = {};
9+
10+
// intercept 'on' calls and capture the callbacks
11+
obj.on = function(eventName, callback){
12+
if(!this.events[eventName]) this.events[eventName] = [];
13+
this.events[eventName].push(callback);
14+
};
15+
16+
// intercept 'emit' calls from the client and record them to assert against in the test
17+
obj.emit = function(eventName){
18+
var args = Array.prototype.slice.call(arguments,1);
19+
20+
if(!this.emits[eventName])
21+
this.emits[eventName] = [];
22+
this.emits[eventName].push(args);
23+
};
24+
25+
//simulate an inbound message to the socket from the server (only called from the test)
26+
obj.receive = function(eventName){
27+
var args = Array.prototype.slice.call(arguments,1);
28+
29+
if (this.events[eventName]) {
30+
angular.forEach(this.events[eventName], function(callback){
31+
$rootScope.$apply(function() {
32+
callback.apply(this, args)
33+
});
34+
});
35+
};
36+
};
37+
38+
return obj;
39+
};
40+
};
41+
});

angular-socket.io-mock.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
describe('Angular Socket.io Mock',function(){
55
beforeEach(module('btford.socket-io'))
66
it('should be able to listen on an event',inject(function(socketFactory){
7-
expect(socketFactory.on('test-event',function(){})).not.toBe(false)
7+
expect(new socketFactory().on('test-event',function(){})).not.toBe(false)
88
}))
99
it('should be able to emit an event',inject(function(socketFactory){
10-
expect(socketFactory.emit('test-event',{})).not.toBe(false)
10+
expect(new socketFactory().emit('test-event',{})).not.toBe(false)
1111
}))
1212
it('should be able to receive an event',inject(function(socketFactory){
13-
expect(socketFactory.receive('test-event',{})).not.toBe(false)
13+
expect(new socketFactory().receive('test-event',{})).not.toBe(false)
1414
}))
1515
})

0 commit comments

Comments
 (0)