Skip to content

Commit

Permalink
Added new common.js tests (expresso-like), with browser-compliant sho…
Browse files Browse the repository at this point in the history
…uld().
  • Loading branch information
rauchg committed Jun 9, 2011
1 parent 579d816 commit 629f2a1
Show file tree
Hide file tree
Showing 4 changed files with 638 additions and 0 deletions.
120 changes: 120 additions & 0 deletions test/events.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
* MIT Licensed
*/

(function (module, io, should) {

module.exports = {

'add listeners': function () {
var event = new io.EventEmitter
, calls = 0;

event.on('test', function (a, b) {
++calls;
a.should().eql('a');
b.should().eql('b');
});

event.emit('test', 'a', 'b');
calls.should().eql(1);
event.on.should().eql(event.addListener);
},

'remove listener': function () {
var event = new io.EventEmitter;
function empty () { }

event.on('test', empty);
event.on('test:more', empty);
event.removeAllListeners('test');

event.listeners('test').should().eql([]);
event.listeners('test:more').should().eql([empty]);
},

'remove all listeners': function () {
var event = new io.EventEmitter;
function empty () { }

event.on('test', empty);
event.on('test:more', empty);
event.removeAllListeners();

event.listeners('test').should().eql([empty]);
event.listeners('test:more').should().eql([empty]);
},

'remove listeners functions': function () {
var event = new io.EventEmitter
, calls = 0;

function one () { ++calls }
function two () { ++calls }
function three () { ++calls }

event.on('one', one);
event.removeListener('one', one);
event.listeners('one').should().eql([]);

event.on('two', two);
event.removeListener('two', one);
event.listeners('two').should().eql([two]);

event.on('three', three);
event.on('three', two);
event.removeListener('three', three);
event.listeners('three').should().eql([two]);
},

'number of arguments': function () {
var event = new io.EventEmitter
, number = [];

event.on('test', function () {
number.push(arguments.length);
});

event.emit('test');
event.emit('test', null);
event.emit('test', null, null);
event.emit('test', null, null, null);
event.emit('test', null, null, null, null);
event.emit('test', null, null, null, null, null);

[0, 1, 2, 3, 4, 5].should().eql(number);
},

'once': function () {
var event = new io.EventEmitter
, calls = 0;

event.once('test', function (a, b) {
++calls;
});

event.emit('test', 'a', 'b');
event.emit('test', 'a', 'b');
event.emit('test', 'a', 'b');

function removed () {
should().fail('not removed');
};

event.once('test:removed', removed);
event.removeListener('test:removed', removed);
event.emit('test:removed');

calls.should().eql(1);
}

};

})(
'undefined' == typeof module ? module = {} : module
, 'undefined' == typeof io ? require('socket.io-client') : io
, 'undefined' == typeof should || !should.fail ? require('should') : should
);
31 changes: 31 additions & 0 deletions test/io.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
* MIT Licensed
*/

(function (module, io, should) {

module.exports = {

'client version number': function () {
io.version.should().match(/([0-9]+)\.([0-9]+)\.([0-9]+)/);
},

'socket.io protocol version': function () {
io.protocol.should().be.a('number');
io.protocol.toString().should().match(/^\d+$/);
},

'socket.io available transports': function () {
(io.transports.length > 0).should().be_true;
}

};

})(
'undefined' == typeof module ? module = {} : module
, 'undefined' == typeof io ? require('socket.io-client') : io
, 'undefined' == typeof should ? require('should') : should
);
Loading

0 comments on commit 629f2a1

Please sign in to comment.