Permalink
Browse files

started tests

  • Loading branch information...
1 parent 995087e commit a9ef80dcd9f919714d43a17106d597c658dd889a TJ Holowaychuk committed Apr 26, 2012
Showing with 96 additions and 1 deletion.
  1. +5 −1 Makefile
  2. +11 −0 package.json
  3. +80 −0 test/emitter.js
View
@@ -22,4 +22,8 @@ stats:
@du -hs build/* | sed 's/^/ /'
@echo
-.PHONY: ui stats
+test:
+ @./node_modules/.bin/mocha \
+ --require should
+
+.PHONY: ui stats test
View
@@ -0,0 +1,11 @@
+{
+ "author": "TJ Holowaychuk <tj@vision-media.ca>",
+ "name": "uikit",
+ "description": "UIKit",
+ "version": "0.0.1",
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "should": "*"
+ }
+}
View
@@ -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([]);
+ })
+ })
+})

0 comments on commit a9ef80d

Please sign in to comment.