From 1a97e0a1f3cf2f6f0659506a9f75198c4d882206 Mon Sep 17 00:00:00 2001 From: hellocreation Date: Mon, 26 Dec 2011 23:28:30 -1000 Subject: [PATCH] add additional tests. --- radio.js | 11 +---------- tests/core.js | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/radio.js b/radio.js index a7d2371..32d5d76 100644 --- a/radio.js +++ b/radio.js @@ -1,15 +1,6 @@ /** * Author: Scott Murphy twitter: @hellocreation, github: uxder * 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) { @@ -52,7 +43,7 @@ ToDo: //if(typeof(listener) == "function") callback = context = listener; //run the listener - callback.apply(context, [arguments].splice(i,1)); + callback.apply(context, arguments); } return this; }, diff --git a/tests/core.js b/tests/core.js index cdb34b4..41f705c 100644 --- a/tests/core.js +++ b/tests/core.js @@ -3,10 +3,13 @@ */ var test = { selfTest : function() { - return this; + 1+2; }, add: function() { 1+1; + }, + scopeTest: function() { + this.add(); } } @@ -69,15 +72,15 @@ describe("Radio Core Test", function() { radio('channel1').add(f,f,f); 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]); 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); 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); expect(radio._.channels.channel1.length).toBe(1); radio('channel1').add(2, f); @@ -128,8 +131,40 @@ describe("Radio Core Test", function() { expect(radio._.channels.channel1.length).toBe(2); radio('channel1').broadcast('test'); 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); }); + });