Skip to content

Commit

Permalink
* Changing specs to run once for the Events class and once for Elemen…
Browse files Browse the repository at this point in the history
…t.Events. Both APIs should work exactly the same and therefore should be in sync, any specialities should tested outside of this. Also, cleaning up those ugly bastards!
  • Loading branch information
cpojer committed Sep 4, 2009
1 parent d9b5292 commit 13ccb06
Showing 1 changed file with 115 additions and 96 deletions.
211 changes: 115 additions & 96 deletions Specs/Class/Mixins.js
Expand Up @@ -119,113 +119,132 @@ describe("Chain Class", {

});

Object.each({

describe("Events Class", {

"before all": function(){
Local.EventsTest = new Class({
Implements: Events,

initialize: function(){
this.called = 0;
}
});
element: function(){
return new Element('div');
},

mixin: function(){
return new Events();
}

"before each": function(){
Local.called = 0;
Local.fn = function(){
return Local.called++;
};
},
}, function(createObject, type){
describe('Events API: ' + type.capitalize(), {

"should add an Event to the Class": function(){
var myTest = new Local.EventsTest();
myTest.addEvent('event', Local.fn);
myTest.fireEvent('event');
value_of(Local.called).should_be(1);
},
'before each': function(){
Local.called = 0;
Local.fn = function(){
return Local.called++;
};
},

"should add multiple Events to the Class": function(){
var myTest = new Local.EventsTest();
myTest.addEvents({
'event1': Local.fn,
'event2': Local.fn
});
myTest.fireEvents('event1', 'event2');
value_of(Local.called).should_be(2);
},
'should add an Event to the Class': function(){
var object = createObject();

"should add a protected event": function(){
var myTest = new Local.EventsTest();
var protectedFn = (function(){ Local.fn(); }).protect();
myTest.addEvent('protected', protectedFn);
myTest.removeEvent('protected', protectedFn);
myTest.fireEvent('protected');
value_of(Local.called).should_be(1);
},
object.addEvent('event', Local.fn).fireEvent('event');

"should remove a specific method for an event": function(){
var myTest = new Local.EventsTest();
var x = 0, fn = function(){ x++; };
myTest.addEvent('event', Local.fn);
myTest.addEvent('event', fn);
myTest.removeEvent('event', Local.fn);
myTest.fireEvent('event');
value_of(x).should_be(1);
value_of(Local.called).should_be(0);
},
value_of(Local.called).should_be(1);
},

"should remove an event and its methods": function(){
var myTest = new Local.EventsTest();
var x = 0, fn = function(){ x++; };
myTest.addEvent('event', Local.fn);
myTest.addEvent('event', fn);
myTest.removeEvents('event');
myTest.fireEvent('event');
value_of(x).should_be(0);
value_of(Local.called).should_be(0);
},
'should add multiple Events to the Class': function(){
createObject().addEvents({
event1: Local.fn,
event2: Local.fn
}).fireEvents('event1', 'event2');

"should remove all events": function(){
var myTest = new Local.EventsTest();
var x = 0, fn = function(){ x++; };
myTest.addEvent('event1', Local.fn);
myTest.addEvent('event2', fn);
myTest.removeEvents();
myTest.fireEvents(['event1', 'event2']);
value_of(x).should_be(0);
value_of(Local.called).should_be(0);
},
value_of(Local.called).should_be(2);
},

"should remove events with an object": function(){
var myTest = new Local.EventsTest();
var events = {
event1: Local.fn,
event2: Local.fn
};
myTest.addEvent('event1', function(){ Local.fn.call(this); }).addEvents(events);
myTest.fireEvent('event1');
value_of(Local.called).should_be(2);
myTest.removeEvents(events);
myTest.fireEvent('event1');
value_of(Local.called).should_be(3);
myTest.fireEvent('event2');
value_of(Local.called).should_be(3);
},

"should be able to remove event during firing": function(){
var myTest = new Local.EventsTest();
myTest.addEvent('event', Local.fn);
myTest.addEvent('event', function(){
Local.fn();
myTest.removeEvent('event', arguments.callee);
});
myTest.addEvent('event', function(){ Local.fn(); });
myTest.fireEvent('event').fireEvent('event');
value_of(Local.called).should_be(5);
}
'should add a protected event': function(){
var object = createObject();
var protectedFn = (function(){ Local.fn(); }).protect();

object.addEvent('protected', protectedFn).removeEvent('protected', protectedFn).fireEvent('protected');

value_of(Local.called).should_be(1);
},

'should remove a specific method for an event': function(){
var object = createObject();
var x = 0, fn = function(){ x++; };

object.addEvent('event', Local.fn).addEvent('event', fn).removeEvent('event', Local.fn).fireEvent('event');

value_of(x).should_be(1);
value_of(Local.called).should_be(0);
},

'should remove an event and its methods': function(){
var object = createObject();
var x = 0, fn = function(){ x++; };

object.addEvent('event', Local.fn).addEvent('event', fn).removeEvents('event').fireEvent('event');

value_of(x).should_be(0);
value_of(Local.called).should_be(0);
},

'should remove all events': function(){
var object = createObject();
var x = 0, fn = function(){ x++; };

object.addEvent('event1', Local.fn).addEvent('event2', fn).removeEvents().fireEvents(['event1', 'event2']);

value_of(x).should_be(0);
value_of(Local.called).should_be(0);
},

'should remove events with an object': function(){
var object = createObject();
var events = {
event1: Local.fn,
event2: Local.fn
};

object.addEvent('event1', function(){ Local.fn(); }).addEvents(events).fireEvent('event1');
value_of(Local.called).should_be(2);

object.removeEvents(events);
object.fireEvent('event1');
value_of(Local.called).should_be(3);

object.fireEvent('event2');
value_of(Local.called).should_be(3);
},

'should be able to remove event during firing': function(){
createObject().addEvent('event', Local.fn).addEvent('event', function(){
Local.fn();
this.removeEvent('event', arguments.callee);
}).addEvent('event', function(){ Local.fn(); }).fireEvent('event').fireEvent('event');

value_of(Local.called).should_be(5);
},

'should remove an event immediately': function(){
var object = createObject();

var methods = [];

var three = function(){
methods.push(3);
};

object.addEvent('event', function(){
methods.push(1);
this.removeEvent('event', three);
}).addEvent('event', function(){
methods.push(2);
}).addEvent('event', three);

object.fireEvent('event');
value_of(methods).should_be([1, 2]);

object.fireEvent('event');
value_of(methods).should_be([1, 2, 1, 2]);
}
});
});

describe("Options Class", {
Expand Down

0 comments on commit 13ccb06

Please sign in to comment.