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

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

describe('Broadcast', function () {

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

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

  it('should put sockets in the all room', function () {
    var sock_1 = new Socket();
    var sock_2 = new Socket();
    var sock_3 = new Socket();

    Room.get('all').length().should.equal(3);
  });

  it('should broadcast to all sockets', function () {
    var sock_1 = new Socket();
    var sock_2 = new Socket();
    var sock_3 = new Socket();

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

    var callCount = 0;
    sock_1.broadcast('hello');
    callCount.should.equal(2);
  });

  it('should broadcast to a room', function () {
    var room = Room.get('random');

    var sock_1 = new Socket();
    var sock_2 = new Socket();
    var sock_3 = new Socket();

    sock_1.join('random');
    sock_2.join('random');
    sock_3.join('random');

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

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

    var callCount = 0;
    sock_1.broadcast.to('random').emit('hello');
    callCount.should.equal(2);
  });

});