File size: 6,586 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict';

var should = require('should');
var Room   = require('../../source/room');
var Socket = require('../fake_socket');

describe('Room', function () {

  beforeEach(function () {
    Socket.reset();
  });

  describe('.get', function() {

    it('should get a room', function () {

      // Creates it if it doesn't exist
      var room = Room.get('some-room');
      room.should.be.an.instanceOf(Room);

      // Reuse the same instance
      Room.get('some-room').should.equal(room);

    });

  });

  describe('.flush', function () {

    it('should destroy all the rooms', function () {

      // create a new room
      var room = Room.get('a');

      // add a socket to the room
      var socket = new Socket();
      socket.join('a');
      room.sockets.should.have.length(1);

      // flush all the rooms
      Room.flush();

      // the socket should not longer be in the room
      room.sockets.should.have.length(0);

      // the room should have been removed
      Room.get('a').should.not.equal(room);
    });

  });

  describe(':constructor', function () {

    it('should create a new Room', function () {
      var room = new Room('my-room');

      room.should.have.keys('id', 'sockets', '_namespaces');
      room.id.should.equal('my-room');

      // should not add to Room.rooms
      Room.get('my-room').should.not.equal(room);
    });

  });

  describe(':_join', function () {

    it('should add a socket to a room', function () {
      var room = new Room('my-room');
      var socket = new Socket();

      room._join(socket);
      room.length().should.equal(1);

      // should only add it once
      room._join(socket);
      room.length().should.equal(1);

      // add another socket
      socket = new Socket();

      room._join(socket);
      room.length().should.equal(2);
    });

  });

  describe(':_leave', function () {

    it('should remove a socket from a room', function () {
      var room = new Room('my-awesome-room');
      var socket = new Socket();

      room.length().should.equal(0);

      // should not care if socket isn't in the room
      room._leave(socket);
      room.length().should.equal(0);

      // add the socket
      room._join(socket);
      room.length().should.equal(1);

      // remove the socket
      room._leave(socket);
      room.length().should.equal(0);
    });

  });

  describe(':in', function () {

    it('should proxy Room.get', function () {
      var a = Room.get('a');
      var b = Room.get('b');

      a.in('a').should.equal(a);
      a.in('b').should.equal(b);
      b.in('a').should.equal(a);
      b.in('b').should.equal(b);
    });

  });

  describe(':length', function () {

    it('should count how many sockets are in a room', function () {
      var room = new Room('my-room');
      room.length().should.equal(0);

      room._join('a');
      room.length().should.equal(1);

      room._join('b');
      room.length().should.equal(2);

      room._join('c');
      room.length().should.equal(3);

      room._join('d');
      room.length().should.equal(4);

      room._leave('d');
      room.length().should.equal(3);

      room._leave('c');
      room.length().should.equal(2);

      room._leave('b');
      room.length().should.equal(1);

      room._leave('a');
      room.length().should.equal(0);
    });

  });

  describe(':emit', function () {

    it('should emit to all sockets in a room', function () {
      var room = Room.get('my-room');

      var a = new Socket();
      var b = new Socket();
      var c = new Socket();

      a.join('my-room');
      b.join('my-room');
      c.join('my-room');

      room.length().should.equal(3);

      room.emit('three', 1, 2, 3);

      a.last().should.eql(['three', 1, 2, 3]);
      b.last().should.eql(['three', 1, 2, 3]);
      c.last().should.eql(['three', 1, 2, 3]);

      a.leave('my-room');
      room.length().should.equal(2);

      room.emit('two', 1, 2, 3);

      a.last().should.eql(['three', 1, 2, 3]);
      b.last().should.eql(['two', 1, 2, 3]);
      c.last().should.eql(['two', 1, 2, 3]);

      b.leave('my-room');
      room.length().should.equal(1);

      room.emit('one', 1, 2, 3);

      a.last().should.eql(['three', 1, 2, 3]);
      b.last().should.eql(['two', 1, 2, 3]);
      c.last().should.eql(['one', 1, 2, 3]);

      c.leave('my-room');
      room.length().should.equal(0);

      room.emit('zero', 1, 2, 3);

      a.last().should.eql(['three', 1, 2, 3]);
      b.last().should.eql(['two', 1, 2, 3]);
      c.last().should.eql(['one', 1, 2, 3]);
    });

  });

  describe(':broadcast', function () {

    it('should broadcast to all sockets in a room', function () {
      var room = Room.get('my-room');
      
      var a = new Socket();
      var b = new Socket();
      var c = new Socket();

      a.join('my-room');
      b.join('my-room');
      c.join('my-room');

      // from a
      room.broadcast(a.id, 'three', 1, 2, 3);

      should.equal(undefined, a.last());
      b.last().should.eql(['three', 1, 2, 3]);
      c.last().should.eql(['three', 1, 2, 3]);

      // from b
      room.broadcast(b.id, 'two', 1, 2, 3);

      a.last().should.eql(['two', 1, 2, 3]);
      b.last().should.eql(['three', 1, 2, 3]);
      c.last().should.eql(['two', 1, 2, 3]);

      // from c
      room.broadcast(c.id, 'one', 1, 2, 3);

      a.last().should.eql(['one', 1, 2, 3]);
      b.last().should.eql(['one', 1, 2, 3]);
      c.last().should.eql(['two', 1, 2, 3]);
    });

  });

  describe(':namespace', function () {

    it('should get a namespace for a room', function () {
      var room = new Room('with-a-ns');
      var ns = room.namespace('my-ns');

      room.namespace('my-ns').should.equal(ns);
      room.namespace('my-other-ns').should.not.equal(ns);
    });

  });

  describe(':contains', function () {

    it('should check if a room contains a socket', function () {
      var room = Room.get('your-room');

      var socket = new Socket();
      room.contains(socket).should.equal(false);

      socket.join('your-room');
      room.contains(socket).should.equal(true);

      socket.leave('your-room');
      room.contains(socket).should.equal(false);

      room._join(socket);
      room.contains(socket).should.equal(true);
    });

  });

  describe(':empty', function () {

    it('should empty a room', function () {
      var room = Room.get('that-room');
      var socket = new Socket();
      socket.join('that-room');

      room.length().should.equal(1);

      room.empty();

      room.length().should.equal(0);
    });

  });

});