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