Skip to content

Commit

Permalink
improve options, add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Janda committed Dec 4, 2011
1 parent df6a2e2 commit 8719172
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
11 changes: 5 additions & 6 deletions lib/ant.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ var Ant = module.exports = function(options) {
maxListeners: options['max-listeners'] || 100,
});

self.debug = true;
self.debug = options['debug'] || false;
self.name = options['name'] || 'ant-' + Math.round(Math.random() * 10000);
self.port = options['port'] || 4000;
self._connections = {};

self.server = dnode(function (client, conn) {
conn.on('end', function () {
Expand All @@ -54,15 +53,15 @@ var Ant = module.exports = function(options) {
self.emit('connection.open', connection);
});

self.server.on('ready', function () {
self.emit('ant.listening', self.post);
self.emit('ant.started', self);
self.server.on( 'ready', function () {
self.emit( 'ant.listening', self.post );
self.emit( 'ant.started', self );
});

self.init();
};

util.inherits(Ant, EventEmitter);
util.inherits( Ant, EventEmitter );

Ant.prototype.init = function() {
var self = this;
Expand Down
66 changes: 45 additions & 21 deletions spec/antSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,56 @@ var Ant = require('../lib/ant'),

describe('Ant', function() {

var ant = null;

beforeEach(function() {
ant = new Ant();
ant.start();
describe('constructor', function() {
it( 'should be instance of EventEmitter', function() {
expect( new Ant() instanceof EventEmitter ).toBeTruthy();
})

it( 'should pass name', function() {
expect( new Ant({name: 'foo'}).name ).toEqual('foo');
})

it( 'should pass port', function() {
expect( new Ant({port: 9000}).port ).toEqual(9000);
})

it( 'should pass debug', function() {
expect( new Ant({debug: true}).debug ).toEqual(true);
})

it( 'should have debug disabled by default', function() {
expect( new Ant().debug ).toEqual(false);
})
})

afterEach(function() {
try {
ant.stop();
} catch(err) {}
})
describe( '', function() {
var ant = null;

it('server should be defined', function() {
expect(ant.server.server).toBeDefined();
})
beforeEach(function() {
ant = new Ant();
ant.start();
})

afterEach(function() {
try {
ant.stop();
} catch(err) {}
})

it('server should be defined', function() {
expect(ant.server.server).toBeDefined();
})

describe('.emit', function() {
it('server emit an event', function() {
var s = spyOn(EventEmitter.prototype, 'emit')
ant.emit('foo', {bar: 'baz'});
describe('.emit', function() {
it('server emit an event', function() {
var s = spyOn(EventEmitter.prototype, 'emit')
ant.emit('foo', {bar: 'baz'});

expect(s).toHaveBeenCalledWith('foo', {
name : 'foo',
data : { bar : 'baz' }
});
expect(s).toHaveBeenCalledWith('foo', {
name : 'foo',
data : { bar : 'baz' }
});
})
})
})
})

0 comments on commit 8719172

Please sign in to comment.