diff --git a/Makefile b/Makefile index a8180c2..5408b73 100644 --- a/Makefile +++ b/Makefile @@ -22,4 +22,8 @@ stats: @du -hs build/* | sed 's/^/ /' @echo -.PHONY: ui stats \ No newline at end of file +test: + @./node_modules/.bin/mocha \ + --require should + +.PHONY: ui stats test \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..16b828c --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "author": "TJ Holowaychuk ", + "name": "uikit", + "description": "UIKit", + "version": "0.0.1", + "dependencies": {}, + "devDependencies": { + "mocha": "*", + "should": "*" + } +} diff --git a/test/emitter.js b/test/emitter.js new file mode 100644 index 0000000..d7f37f9 --- /dev/null +++ b/test/emitter.js @@ -0,0 +1,80 @@ + +var Emitter = require('../lib/components/emitter/emitter').Emitter; + +describe('Emitter', function(){ + describe('.on(event, fn)', function(){ + it('should add listeners', function(){ + var emitter = new Emitter; + var calls = []; + + emitter.on('foo', function(val){ + calls.push('one', val); + }); + + emitter.on('foo', function(val){ + calls.push('two', val); + }); + + emitter.emit('foo', 1); + emitter.emit('bar', 1); + emitter.emit('foo', 2); + + calls.should.eql([ 'one', 1, 'two', 1, 'one', 2, 'two', 2 ]); + }) + }) + + describe('.once(event, fn)', function(){ + it('should add a single-shot listener', function(){ + var emitter = new Emitter; + var calls = []; + + emitter.once('foo', function(val){ + calls.push('one', val); + }); + + emitter.emit('foo', 1); + emitter.emit('foo', 2); + emitter.emit('foo', 3); + emitter.emit('bar', 1); + + calls.should.eql([ 'one', 1 ]); + }) + }) + + describe('.off(event, fn)', function(){ + it('should remove a listener', function(){ + var emitter = new Emitter; + var calls = []; + + function one() { calls.push('one'); } + function two() { calls.push('two'); } + + emitter.on('foo', one); + emitter.on('foo', two); + emitter.off('foo', two); + + emitter.emit('foo'); + + calls.should.eql([ 'one' ]); + }) + }) + + describe('.off(event)', function(){ + it('should remove all listeners for an event', function(){ + var emitter = new Emitter; + var calls = []; + + function one() { calls.push('one'); } + function two() { calls.push('two'); } + + emitter.on('foo', one); + emitter.on('foo', two); + emitter.off('foo'); + + emitter.emit('foo'); + emitter.emit('foo'); + + calls.should.eql([]); + }) + }) +}) \ No newline at end of file