Skip to content

Commit 2f36552

Browse files
authored
Merge pull request #10 from kamilkisiela/master
Add forward method and prefix option
2 parents d412c54 + 7e903fa commit 2f36552

File tree

5 files changed

+90
-36
lines changed

5 files changed

+90
-36
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ angular.module('app')
161161

162162
## Changelog
163163

164-
### 1.1.0
164+
### 1.1.1
165+
* Add `forward` support
165166

167+
### 1.1.0
166168
* .receive will acknowledge any .emit events with a callback as the last parameter
167169

168170
### 1.0.0

angular-socket.io-mock.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/* global angular: false */
22
var ng = angular.module('btford.socket-io',[])
33
ng.provider('socketFactory',function(){
4+
var defaultPrefix = 'socket:';
45
this.$get = function($rootScope){
5-
return function socketFactory () {
6+
return function socketFactory (options) {
7+
options = options || {};
8+
var prefix = options.prefix === undefined ? defaultPrefix : options.prefix;
9+
var defaultScope = options.scope || $rootScope;
610
var obj = {};
711
obj.events = {};
812
obj.emits = {};
@@ -25,6 +29,24 @@ ng.provider('socketFactory',function(){
2529
this.emits[eventName].push(args);
2630
};
2731

32+
// when socket.on('someEvent', fn (data) { ... }),
33+
// call scope.$broadcast('someEvent', data)
34+
obj.forward = function (events, scope) {
35+
var self = this;
36+
if (events instanceof Array === false) {
37+
events = [events];
38+
}
39+
if (!scope) {
40+
scope = $rootScope;
41+
}
42+
events.forEach(function (eventName) {
43+
var prefixedEvent = prefix + eventName;
44+
obj.on(eventName, function (data) {
45+
scope.$broadcast(prefixedEvent, data);
46+
});
47+
});
48+
};
49+
2850
//simulate an inbound message to the socket from the server (only called from the test)
2951
obj.receive = function(eventName){
3052
var args = Array.prototype.slice.call(arguments,1);

angular-socket.io-mock.spec.js

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,65 @@
11
/* global expect: false, it: false, describe: false */
22
'use strict';
33

4-
describe('Angular Socket.io Mock',function(){
5-
var socketFactory;
6-
beforeEach(module('btford.socket-io'))
7-
beforeEach(inject(function(_socketFactory_){ socketFactory = _socketFactory_ }));
8-
it('should be able to listen on an event', function(){
9-
expect(new socketFactory().on('test-event',function(){})).not.toBe(false)
10-
});
11-
it('should be able to listen once event', function(){
12-
expect(new socketFactory().once('test-event',function(){})).not.toBe(false)
13-
});
14-
it('should be able to emit an event', function(){
15-
expect(new socketFactory().emit('test-event',{})).not.toBe(false)
16-
});
17-
it('should be able to receive an event', function(){
18-
expect(new socketFactory().receive('test-event',{})).not.toBe(false)
19-
});
20-
it('should be able to acknowledge an emited event only once',function(done){
21-
var socket = new socketFactory();
22-
var timesCalled = 0;
23-
24-
socket.emit('test-event',{}, function(resp){
25-
expect(resp).not.toBe(false);
26-
expect(++timesCalled).toEqual(1);
4+
describe('Angular Socket.io Mock', function () {
5+
var socketFactory;
6+
var $rootScope;
7+
beforeEach(module('btford.socket-io'))
8+
beforeEach(inject(function (_socketFactory_, _$rootScope_) {
9+
socketFactory = _socketFactory_
10+
$rootScope = _$rootScope_;
11+
}));
12+
it('should be able to listen on an event', function () {
13+
expect(new socketFactory().on('test-event', function () {
14+
})).not.toBe(false)
15+
});
16+
it('should be able to listen once event', function () {
17+
expect(new socketFactory().once('test-event', function () {
18+
})).not.toBe(false)
19+
});
20+
it('should be able to emit an event', function () {
21+
expect(new socketFactory().emit('test-event', {})).not.toBe(false)
2722
});
23+
it('should be able to receive an event', function () {
24+
expect(new socketFactory().receive('test-event', {})).not.toBe(false)
25+
});
26+
it('should be able to acknowledge an emited event only once', function (done) {
27+
var socket = new socketFactory();
28+
var timesCalled = 0;
29+
30+
socket.emit('test-event', {}, function (resp) {
31+
expect(resp).not.toBe(false);
32+
expect(++timesCalled).toEqual(1);
33+
});
2834

29-
socket.receive('test-event', {});
30-
socket.receive('test-event', {});
35+
socket.receive('test-event', {});
36+
socket.receive('test-event', {});
3137

32-
setTimeout(function() { // Wait to see if the event was acknowledged twice
33-
done();
34-
}, 100);
35-
});
38+
setTimeout(function () { // Wait to see if the event was acknowledged twice
39+
done();
40+
}, 100);
41+
});
42+
it('should be able to forward an event with default prefix', function () {
43+
var socket = new socketFactory();
44+
spyOn($rootScope, "$broadcast");
45+
socket.forward('test-event');
46+
socket.receive('test-event', {'test-data': 'test-value'});
47+
expect($rootScope.$broadcast).toHaveBeenCalledWith('socket:test-event', {'test-data': 'test-value'});
48+
});
49+
it('should be able to forward an event with custom prefix', function () {
50+
var socket = new socketFactory({prefix: 'test-prefix:'});
51+
spyOn($rootScope, "$broadcast");
52+
socket.forward('test-event');
53+
socket.receive('test-event', {'test-data': 'test-value'});
54+
expect($rootScope.$broadcast).toHaveBeenCalledWith('test-prefix:test-event', {'test-data': 'test-value'});
55+
});
56+
it('should be able to forward events', function () {
57+
var socket = new socketFactory({prefix: 'test-prefix:'});
58+
spyOn($rootScope, "$broadcast").and.callThrough;
59+
socket.forward(['test-event1', 'test-event2']);
60+
socket.receive('test-event1', {'test-data1': 'test-value1'});
61+
socket.receive('test-event2', {'test-data2': 'test-value2'});
62+
expect($rootScope.$broadcast).toHaveBeenCalledWith('test-prefix:test-event1', {'test-data1': 'test-value1'});
63+
expect($rootScope.$broadcast).toHaveBeenCalledWith('test-prefix:test-event2', {'test-data2': 'test-value2'});
64+
});
3665
})

bower.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "angular-socket.io-mock",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
4+
"main": "./angular-socket.io-mock.js",
45
"dependencies": {
5-
"angular": "latest",
6-
"angular-socket-io": "latest"
6+
"angular": "~1.4.5",
7+
"angular-socket-io": "~0.7.0"
78
}
8-
}
9+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-socket.io-mock",
33
"description": "Drop in Mock replacement for angular-socket-io",
4-
"version": "1.1.0",
4+
"version": "1.1.2",
55
"authors": [
66
{
77
"name": "Bryan Tong",

0 commit comments

Comments
 (0)