Skip to content

Commit cad64ab

Browse files
author
Eduardo García Sanz
committed
socket, event and some testing...
1 parent 87afe5d commit cad64ab

File tree

6 files changed

+212
-8
lines changed

6 files changed

+212
-8
lines changed

package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,30 @@
1010
"authors" : [
1111
"Eduardo García Sanz <[email protected]>"
1212
],
13-
"keywords" : ["react", "socket.io"],
13+
"keywords" : [
14+
"react",
15+
"socket.io"
16+
],
1417
"dependencies" : {
18+
"react" : "0.13",
1519
"socket.io-client": "1.3"
1620
},
1721
"devDependencies": {
18-
"jsdom" : "5.4",
19-
"mocha" : "2.2",
20-
"should" : "7.0",
21-
"eslint" : "0.24",
22-
"eslint-plugin-react": "2.5"
22+
"proxyquire": "1.5",
23+
"jsdom" : "5.4",
24+
"mocha" : "2.2",
25+
"should" : "7.0",
26+
"eslint" : "0.24"
2327
},
2428
"scripts" : {
2529
"lint": "eslint src",
2630
"test": "npm run lint && mocha"
2731
},
2832
"eslintConfig" : {
29-
"env" : {
33+
"env" : {
3034
"node": true
3135
},
32-
"rules" : {
36+
"rules": {
3337
"strict" : 0,
3438
"new-cap" : 0,
3539
"eqeqeq" : 2,

src/event.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var React = require('react'),
2+
Socket = require('./socket').socket;
3+
4+
module.exports = React.createClass({
5+
displayName : 'SocketEvent',
6+
propTypes : {
7+
socket : React.PropTypes.string,
8+
name : React.PropTypes.string,
9+
callback: React.PropTypes.func
10+
},
11+
getDefaultProps : function () {
12+
13+
return {
14+
socket: 'default'
15+
};
16+
},
17+
componentWillMount : function () {
18+
19+
Socket(this.props.socket)
20+
.on(this.props.name, this.props.callback);
21+
},
22+
componentWillUnmount: function () {
23+
24+
Socket(this.props.socket)
25+
.off(this.props.name, this.props.callback);
26+
},
27+
render : function () {
28+
29+
return false;
30+
}
31+
});

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
Socket: require('./socket'),
3+
Event : require('./event')
4+
};

src/socket.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var React = require('react'),
2+
io = require('socket.io-client');
3+
4+
var sockets = {};
5+
6+
module.exports = React.createClass({
7+
displayName : 'Socket',
8+
propTypes : {
9+
url : React.PropTypes.string,
10+
name : React.PropTypes.string,
11+
options: React.PropTypes.shape({
12+
reconnection : React.PropTypes.bool,
13+
reconnectionAttempts: React.PropTypes.number,
14+
reconnectionDelay : React.PropTypes.number,
15+
reconnectionDelayMax: React.PropTypes.number,
16+
randomizationFactor : React.PropTypes.number,
17+
timeout : React.PropTypes.number
18+
})
19+
},
20+
getDefaultProps : function () {
21+
22+
return {
23+
name : 'default',
24+
options: {}
25+
};
26+
},
27+
statics : {
28+
socket: function (name) {
29+
30+
name = name || 'default';
31+
32+
if (!sockets.hasOwnProperty(name)) {
33+
34+
throw new Error('There is no "' + name + '" socket mounted.');
35+
}
36+
37+
return sockets[name];
38+
}
39+
},
40+
componentWillMount : function () {
41+
42+
if (sockets.hasOwnProperty(this.props.name)) {
43+
44+
throw new Error('Another "' + this.props.name + '" socket is already mounted.');
45+
}
46+
47+
sockets[this.props.name] = io(this.props.url, this.props.options);
48+
},
49+
componentWillUnmount: function () {
50+
51+
sockets[this.props.name].disconnect();
52+
delete sockets[this.props.name];
53+
},
54+
render : function () {
55+
56+
return false;
57+
}
58+
});

test/mocha.opts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--require should
2+
--reporter spec
3+
--ui bdd
4+
--recursive

test/socket.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
var React = require('react/addons'),
2+
proxyquire = require('proxyquire'),
3+
jsdom = require('jsdom');
4+
5+
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
6+
global.window = document.defaultView;
7+
global.navigator = window.navigator;
8+
9+
var MockedSocket = proxyquire('../src/socket.js', {
10+
'socket.io-client': function () {
11+
12+
return {
13+
disconnect: function () {}
14+
};
15+
}
16+
});
17+
18+
var render = function (props, mock) {
19+
20+
var Socket = mock ? proxyquire('../src/socket.js', {
21+
'socket.io-client': mock
22+
}) : MockedSocket;
23+
24+
var container = document.createElement('div'),
25+
component = React.createElement(Socket, props);
26+
27+
React.render(component, container);
28+
29+
return {
30+
unmount: function () {
31+
32+
React.unmountComponentAtNode(container);
33+
}
34+
};
35+
};
36+
37+
describe('The socket', function () {
38+
39+
it('connects a new io instance on mount and disconnects it on unmount', function (done) {
40+
41+
var props = {
42+
url : 'foo:9000',
43+
options: {
44+
reconnect: false
45+
}
46+
};
47+
48+
render(props, function (url, options) {
49+
50+
(url).should.equal(props.url);
51+
(options).should.equal(props.options);
52+
53+
return {
54+
disconnect: done
55+
}
56+
}).unmount();
57+
});
58+
59+
it('throws an error on socket name duplication', function () {
60+
61+
var props = {
62+
url : 'foo:9000',
63+
name: 'foo'
64+
};
65+
66+
render(props);
67+
68+
(function () {
69+
70+
render(props);
71+
72+
}).should.throw('Another "foo" socket is already mounted.');
73+
});
74+
75+
it('throws an error when trying to get an unknown socket', function () {
76+
77+
(function () {
78+
79+
MockedSocket.socket('faa');
80+
81+
}).should.throw('There is no "faa" socket mounted.');
82+
});
83+
84+
it('return a socket by its name and throws an removes it on unmount', function () {
85+
86+
var props = {
87+
url : 'foo:9000',
88+
name: 'fin'
89+
};
90+
91+
var rendered = render(props);
92+
93+
MockedSocket.socket('fin').should.be.ok;
94+
95+
rendered.unmount();
96+
97+
(function () {
98+
99+
MockedSocket.socket('fin');
100+
101+
}).should.throw('There is no "fin" socket mounted.');
102+
});
103+
});

0 commit comments

Comments
 (0)