friendship
Version:
Manage friendships between users in your app, knowing when they gets online and offline
252 lines (168 loc) • 7.87 kB
JavaScript
var expect = require('expect.js')
, _ = require('underscore')
, Friendship = require('../../lib');
describe('Friendship', function(){
var friendship = new Friendship({
expire: 30000
});
it('should allow to register a friendship', function(){
var curr = 'uid1'
, friends = ['uid2', 'uid3', 'uid4'];
friendship.register(curr, friends);
expect(friendship._friendsOf).to.be.an('object');
expect(_.keys(friendship._friendsOf).length).to.be.equal(1);
expect(friendship._friendsOf[curr]).to.be.an('array');
expect(friendship._friendsOf[curr].length).to.be.equal(3);
_.each(friendship._friendsOf[curr], function(id, idx){
expect(id).to.be.equal('uid' + (idx+2));
});
});
it('should allow to register a friendship with a friend registered', function(){
var curr = 'uid2'
, friends = ['uid1', 'uid3', 'uid5'];
friendship.register(curr, friends);
expect(friendship._friendsOf).to.be.an('object');
expect(_.keys(friendship._friendsOf).length).to.be.equal(2);
expect(friendship._friendsOf[curr]).to.be.an('array');
expect(friendship._friendsOf[curr].length).to.be.equal(3);
_.each(friendship._friendsOf[curr], function(id, idx){
expect(friends.indexOf(id)).to.be.greaterThan(-1);
});
});
it('should allow to checkin with a socketId', function(){
var curr = 'uid1';
friendship.checkin(curr, 'sid1-1');
expect(friendship._socketsOf).to.be.an('object');
expect(_.keys(friendship._socketsOf).length).to.be.equal(1);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(1);
expect(friendship._socketsOf[curr][0]).to.be.equal('sid1-1');
friendship.checkin(curr, 'sid1-2');
expect(_.keys(friendship._socketsOf).length).to.be.equal(1);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(2);
expect(friendship._socketsOf[curr][1]).to.be.equal('sid1-2');
});
it('should allow to other user to checkin with a socketId', function(done){
var curr = 'uid2';
friendship.on('checkin', function(data){
friendship.removeAllListeners('checkin');
expect(data.userId).to.be.equal(curr);
var sockets = data.sockets;
expect(sockets).to.be.an('array');
expect(sockets.length).to.be.equal(2);
expect(sockets[0]).to.be.equal('sid1-1');
expect(sockets[1]).to.be.equal('sid1-2');
expect(friendship._socketsOf).to.be.an('object');
expect(_.keys(friendship._socketsOf).length).to.be.equal(2);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(1);
expect(friendship._socketsOf[curr][0]).to.be.equal('sid2-1');
friendship.checkin(curr, 'sid2-2');
expect(_.keys(friendship._socketsOf).length).to.be.equal(2);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(2);
expect(friendship._socketsOf[curr][1]).to.be.equal('sid2-2');
done();
});
friendship.checkin(curr, 'sid2-1');
});
it('should allow to checkin an existance socketId', function(){
var curr = 'uid1';
friendship.checkin(curr, 'sid1-1');
expect(friendship._socketsOf).to.be.an('object');
expect(_.keys(friendship._socketsOf).length).to.be.equal(2);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(2);
expect(friendship._socketsOf[curr][0]).to.be.equal('sid1-1');
});
it('should allow to expire a socket if not check is called', function(done){
var expire = 250
, friendshipExpire = new Friendship({ expire: expire })
, curr = 'uid1';
friendshipExpire.register(curr, ['uid2', 'uid3']);
friendshipExpire.checkin(curr, 'sid1');
setTimeout(function(){
expect(friendshipExpire._socketsOf[curr][0]).to.be.equal('sid1');
friendshipExpire.check(curr, 'sid1');
setTimeout(function(){
expect(friendshipExpire._socketsOf[curr]).to.not.be.ok();
friendshipExpire.check(curr, 'sid1');
expect(friendshipExpire._socketsOf[curr][0]).to.be.equal('sid1');
done();
}, expire*2);
}, expire/2);
});
it('should allow to checkout a socketId', function(){
var curr = 'uid1';
friendship.checkin(curr, 'sid1-3');
expect(friendship._socketsOf).to.be.an('object');
expect(_.keys(friendship._socketsOf).length).to.be.equal(2);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(3);
expect(friendship._socketsOf[curr][2]).to.be.equal('sid1-3');
expect(friendship._timeoutOf['sid1-3']).to.be.ok();
friendship.checkout(curr, 'sid1-3');
expect(_.keys(friendship._socketsOf).length).to.be.equal(2);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(2);
expect(friendship._timeoutOf['sid1-3']).to.not.be.ok();
});
it('should allow to checkout a userId with all socketIds', function(){
var curr = 'uid1';
expect(_.keys(friendship._friendsOf).length).to.be.equal(2);
expect(_.keys(friendship._socketsOf).length).to.be.equal(2);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(2);
expect(friendship._timeoutOf['sid1-1']).to.be.ok();
expect(friendship._timeoutOf['sid1-2']).to.be.ok();
friendship.checkout(curr);
expect(_.keys(friendship._friendsOf).length).to.be.equal(2);
expect(_.keys(friendship._socketsOf).length).to.be.equal(1);
expect(friendship._socketsOf[curr]).to.not.be.ok();
expect(friendship._timeoutOf['sid1-1']).to.not.be.ok();
expect(friendship._timeoutOf['sid1-2']).to.not.be.ok();
});
it('should allow to unregister a friendship', function(){
var curr = 'uid2';
expect(_.keys(friendship._friendsOf).length).to.be.equal(2);
expect(_.keys(friendship._socketsOf).length).to.be.equal(1);
expect(friendship._socketsOf[curr]).to.be.an('array');
expect(friendship._socketsOf[curr].length).to.be.equal(2);
expect(friendship._timeoutOf['sid2-1']).to.be.ok();
expect(friendship._timeoutOf['sid2-2']).to.be.ok();
friendship.unregister(curr);
expect(_.keys(friendship._friendsOf).length).to.be.equal(1);
expect(_.keys(friendship._socketsOf).length).to.be.equal(0);
expect(friendship._socketsOf[curr]).to.not.be.ok();
expect(friendship._timeoutOf['sid2-1']).to.not.be.ok();
expect(friendship._timeoutOf['sid2-2']).to.not.be.ok();
});
it('should allow throw checkout when there is no more sockets for the user', function(done){
var curr = 'uid1'
, fid = 'uid2'
, fires = 0;
var friendshipOther = new Friendship({ expire: 3000 });
friendshipOther.register(curr, [ fid, 'uid3']);
friendshipOther.register(fid, [ curr, 'uid5']);
friendshipOther.checkin(curr, 'sid11');
friendshipOther.checkin(curr, 'sid12');
friendshipOther.checkin(fid, 'sid21');
friendshipOther.checkin(fid, 'sid22');
friendshipOther.on('checkout', function(data){
fires++;
expect(fires).to.be.equal(1);
expect(data.userId).to.be.equal(curr);
expect(friendshipOther._socketsOf[curr]).to.not.be.ok();
var sockets = data.sockets;
expect(sockets).to.be.an('array');
expect(sockets.length).to.be.equal(2);
expect(sockets[0]).to.be.equal('sid21');
expect(sockets[1]).to.be.equal('sid22');
friendship.removeAllListeners('checkout');
done();
});
friendshipOther.checkout(curr, 'sid11');
friendshipOther.checkout(curr, 'sid12');
});
});