File size: 3,509 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
'use strict';

var should    = require('should');
var Room      = require('../../source/room');
var Namespace = require('../../source/namespace');
var Broadcast = require('../../source/broadcast');
var Socket    = require('../fake_socket');

describe('Room', function () {

  beforeEach(function () {
    Broadcast.init(Room);
  });

  afterEach(function () {
    Room.flush();
  });

  it('can create rooms', function () {
    var room = Room.get('something');
    room.id.should.equal('something');
  });

  it('one room per room id', function () {
    var room1 = Room.get('anything');
    var room2 = Room.get('anything');

    room1.should.equal(room2);
  });

  it('sockets can join rooms', function () {
    var room = Room.get('chicken');
    room._join(new Socket());

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

  it('sockets can leave rooms', function () {
    var socket = new Socket();

    var room = Room.get('pineapple');
    room.length().should.equal(0);

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

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

  it('check if a socket is in a room', function () {
    var room = Room.get('my_room');
    var socket = new Socket();

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

    room._leave(socket);
    room.contains(socket).should.equal(false);
  });

  it('get another room', function () {
    var room1 = Room.get('room1');
    var room2 = Room.get('room2');

    Room.get('room2').should.equal(room2);
  });

  it('emit to all sockets in a room', function () {
    var room = Room.get('special');
    var socket1 = new Socket();
    var socket2 = new Socket();
    var callCount = 0;

    new Socket.listen(function (event) {
      callCount++;
      event.should.equal('hello');
    });

    room._join(socket1);
    room._join(socket2);
    room.length().should.equal(2);

    room.emit('hello');
    callCount.should.equal(2);
  });

  it('broadcast to all sockets in a room', function () {
    var room = Room.get('broadcast');
    var socket1 = new Socket();
    var socket2 = new Socket();
    var sender = new Socket();
    var callCount = 0;

    room._join(socket1);
    room._join(socket2);
    room._join(sender);

    new Socket.listen(function (event) {
      event.should.equal('the_broadcast');
      this.should.not.equal(sender);
      callCount++;
    });

    room.broadcast(sender.id, 'the_broadcast');
    callCount.should.equal(2);
  });

  it('emit to a room from a namespace', function () {
    var room = Room.get('room');
    var socket1 = new Socket();
    var socket2 = new Socket();

    room._join(socket1);
    room._join(socket2);

    var namespace = room.namespace('namespace');
    var callCount = 0;

    new Socket.listen(function (event) {
      event.should.equal('namespace.event');
      callCount++;
    });

    namespace.emit('event');
    callCount.should.equal(2);
  });

  it('broadcast to a room from a namespace', function () {
    var room = Room.get('room');
    var socket1 = new Socket();
    var socket2 = new Socket();
    var socket3 = new Socket();
    var callCount = 0;

    room._join(socket1);
    room._join(socket2);
    room._join(socket3);

    new Socket.listen(function (event) {
      this.should.not.equal(socket1);
      event.should.equal('namespace.event');
      callCount++;
    });

    var namespace = socket1.namespace('namespace');

    namespace.broadcast('event');
    callCount.should.equal(2);

  });

});