Skip to content

Commit

Permalink
add additional tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
uxder committed Dec 27, 2011
1 parent 01dc443 commit 1a97e0a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
11 changes: 1 addition & 10 deletions radio.js
@@ -1,15 +1,6 @@
/** /**
* Author: Scott Murphy twitter: @hellocreation, github: uxder * Author: Scott Murphy twitter: @hellocreation, github: uxder
* radio.js - The Chainable, Dependency Free Publish/Subscribe for Javascript * radio.js - The Chainable, Dependency Free Publish/Subscribe for Javascript
ToDo:
- create more tests
- change out the add method so that you never add a fnction..I'm always adding an array
- change out for loops for native array filter, search methods.
- test benchmarks.
*/ */


(function(global) { (function(global) {
Expand Down Expand Up @@ -52,7 +43,7 @@ ToDo:
//if(typeof(listener) == "function") callback = context = listener; //if(typeof(listener) == "function") callback = context = listener;


//run the listener //run the listener
callback.apply(context, [arguments].splice(i,1)); callback.apply(context, arguments);
} }
return this; return this;
}, },
Expand Down
45 changes: 40 additions & 5 deletions tests/core.js
Expand Up @@ -3,10 +3,13 @@
*/ */
var test = { var test = {
selfTest : function() { selfTest : function() {
return this; 1+2;
}, },
add: function() { add: function() {
1+1; 1+1;
},
scopeTest: function() {
this.add();
} }
} }


Expand Down Expand Up @@ -69,15 +72,15 @@ describe("Radio Core Test", function() {
radio('channel1').add(f,f,f); radio('channel1').add(f,f,f);
expect(radio._.channels.channel1.length).toBe(3); expect(radio._.channels.channel1.length).toBe(3);
}); });
it("it should suppport adding listerners with setting the context of 'this'", function() { it("should suppport adding listerners with setting the context of 'this'", function() {
radio('channel1').add([test.selfTest, test]); radio('channel1').add([test.selfTest, test]);
expect(radio._.channels.channel1.length).toBe(1); expect(radio._.channels.channel1.length).toBe(1);
}); });
it("it should a combination of adding functions and anonymous functions", function() { it("should support a combination of adding functions in different ways", function() {
radio('channel1').add(f,[test.selfTest, test], f2); radio('channel1').add(f,[test.selfTest, test], f2);
expect(radio._.channels.channel1.length).toBe(3); expect(radio._.channels.channel1.length).toBe(3);
}); });
it("it should not allow a non-function or array to be added", function() { it("should not allow a non-function or array to be added", function() {
radio('channel1').add("string", f); radio('channel1').add("string", f);
expect(radio._.channels.channel1.length).toBe(1); expect(radio._.channels.channel1.length).toBe(1);
radio('channel1').add(2, f); radio('channel1').add(2, f);
Expand Down Expand Up @@ -128,8 +131,40 @@ describe("Radio Core Test", function() {
expect(radio._.channels.channel1.length).toBe(2); expect(radio._.channels.channel1.length).toBe(2);
radio('channel1').broadcast('test'); radio('channel1').broadcast('test');
expect(test.selfTest).toHaveBeenCalled(); expect(test.selfTest).toHaveBeenCalled();
expect(test.add).toHaveBeenCalled(); expect(test.add).toHaveBeenCalled();
expect(test.selfTest.callCount).toBe(1);
expect(test.add.callCount).toBe(1);
//reset methods
test.selfTest.reset();
test.add.reset();
});
it("should pass it's broadcast arguments to the listener", function() {
spyOn(test, 'add');
spyOn(test, 'selfTest');
radio('channel1').add([test.selfTest, test]);
radio('channel2').add([test.add, test]);
radio('channel1').broadcast('data1', ['somearray','item2'], 'data3');
radio('channel2').broadcast('data1');
expect(test.selfTest).toHaveBeenCalledWith('data1', ['somearray','item2'], 'data3');
expect(test.add).toHaveBeenCalledWith('data1');
});


it("should call the listener and maintain it's set scope", function() {
spyOn(test, 'add');
//add the scopeTest method. Scope test sets the context 'test' as this and calls the add method
radio('channel1').add([test.scopeTest, test]).broadcast('test');
expect(test.add.callCount).toBe(1);

});

it("should call the listener and set the scope as window if it wasn't specified", function() {
spyOn(window, 'f');
//add the scopeTest method. Scope test sets the context 'test' as this and calls the add method
radio('channel1').add(f).broadcast('test');
expect(window.f.callCount).toBe(1);
}); });

}); });




Expand Down

0 comments on commit 1a97e0a

Please sign in to comment.