forked from nullivex/angular-socket.io-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-socket.io-mock.js
More file actions
executable file
·53 lines (39 loc) · 1.43 KB
/
angular-socket.io-mock.js
File metadata and controls
executable file
·53 lines (39 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* global angular: false */
var ng = angular.module("btford.socket-io",[])
ng.provider("socketFactory",function(){
this.$get = function($rootScope){
return function() {
var obj = {}
obj.events = {}
obj.emits = {}
// intercept 'on' calls and capture the callbacks
obj.on = function(eventName, callback){
if(!this.events[eventName]) this.events[eventName] = []
this.events[eventName].push(callback)
}
// intercept 'emit' calls from the client and record them to assert against in the test
obj.emit = function(eventName){
var args = Array.prototype.slice.call(arguments,1)
if(!this.emits[eventName])
this.emits[eventName] = []
this.emits[eventName].push(args)
}
//simulate an inbound message to the socket from the server (only called from the test)
obj.receive = function(eventName){
var args = Array.prototype.slice.call(arguments,1)
if(this.events[eventName]){
angular.forEach(this.events[eventName], function(callback){
$rootScope.$apply(function() {
callback.apply(this, args)
})
})
}
}
//simulate an inbound message to the socket from the server (only called from the test)
obj.forward = function(eventName){
//additional API for forwarding events to the angular context
}
return obj
};
}
})