File size: 5,060 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 |
'use strict';
var Jandal = require('../../index');
var Socket = require('../../source/socket');
var Room = require('../../source/room');
var should = require('should');
var createSocket = function () {
return {
last: null,
reply: null,
on: function (name, callback) {
if (name != 'data') return;
this.reply = callback;
},
write: function (string) {
this.last = string;
}
};
};
describe('Socket', function () {
var conn, socket, expect;
expect = function (string) {
conn.last.should.equal(string);
};
beforeEach(function () {
Room.flush();
conn = createSocket();
socket = new Socket(conn, 'stream');
});
// ----------------------------------------------------------------------------
// EVENTS
// ----------------------------------------------------------------------------
it('should register a listener for the "data" event on instantiation', function () {
conn.reply.should.have.type('function');
});
it('should expose some EventEmitter methods via on() and _emit()', function () {
var total = 0;
socket.on('event with a few arguments', function (a, b, c) {
a.should.equal('one');
b.should.equal('two');
c.should.equal('three');
total = arguments.length;
});
socket._emit('event with a few arguments', 'one', 'two', 'three');
total.should.equal(3);
should.strictEqual(conn.last, null);
});
it('should proxy native EventEmitter events', function () {
socket.on('newListener', function (event, fn) {
event.should.have.type('string');
fn.should.have.type('function');
});
socket.on('should trigger newListener', function () {});
socket.emit('newListener', 'randomEvent', function () {});
should.strictEqual(conn.last, null);
});
// ----------------------------------------------------------------------------
// NAMESPACES
// ----------------------------------------------------------------------------
it('should create namespaces', function () {
var shoe = socket.namespace('shoe');
// Should re-use the same namespace
shoe.should.equal(socket.namespace('shoe'));
});
it('should prefix namespaced events', function () {
var shoe = socket.namespace('shoe');
shoe.emit('event', 'hello', 'world');
expect('shoe.event("hello","world")');
shoe.emit('no_args');
expect('shoe.no_args()');
});
it('should respond to namespaced events', function () {
var shoe = socket.namespace('shoe');
shoe.on('event', function (a, b) {
a.should.have.type('string');
b.should.have.type('string');
});
conn.reply('shoe.event("thats","odd")');
conn.reply('shoe.event("hello","world")');
});
it('should not mix events between namespaces', function () {
var shoe = socket.namespace('shoe');
var sandal = socket.namespace('sandal');
socket.on('event', function () {
type = 'socket';
});
shoe.on('event', function () {
type = 'shoe';
});
sandal.on('event', function () {
type = 'sandal';
});
var type = '';
conn.reply('event("test")');
type.should.equal('socket');
type = '';
conn.reply('shoe.event("test", "thing")');
type.should.equal('shoe');
type = '';
conn.reply('sandal.event(["test", "thing"],"hello")');
type.should.equal('sandal');
});
// ----------------------------------------------------------------------------
// CALLBACKS
// ----------------------------------------------------------------------------
it('should convert functions into callback handlers', function () {
var fn = function () { };
socket.emit('event', fn);
expect('event().fn(0)');
socket.emit('event', fn);
expect('event().fn(1)');
});
it('should trigger callbacks', function () {
socket.on('event', function (callback) {
callback.should.have.type('function');
callback();
});
conn.reply('event().fn(20)');
expect('socket.fn_20()');
});
it('should trigger callback with arguments', function () {
socket.on('event', function (callback) {
callback('hello', 'world');
});
conn.reply('event().fn(10)');
expect('socket.fn_10("hello","world")');
});
it('should run callbacks', function (done) {
var fn = function (a, b) {
a.should.equal('some');
b.should.equal('arguments');
done();
};
socket.emit('event', fn);
conn.reply('socket.fn_0("some","arguments")');
});
// ----------------------------------------------------------------------------
// ROOMS
// ----------------------------------------------------------------------------
it('should join a room', function () {
socket.join('my_room');
socket.room('my_room').length().should.equal(1);
socket.rooms.should.eql(['all', 'my_room']);
});
it('should leave a room', function () {
socket.join('my_room');
socket.leave('my_room');
socket.room('my_room').length().should.equal(0);
socket.rooms.should.eql(['all']);
});
});
|