Created
January 27, 2011 20:04
-
-
Save zspencer/799124 to your computer and use it in GitHub Desktop.
this allows you to actually test drive executing something at page load. It's pretty much just a wrapper around jQuery's page Load functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PageLoadHandler = (function() { | |
PageLoadHandlerObject = function() {}; | |
PageLoadHandlerObject.prototype = { | |
callBackList: [], | |
execute: function(callBack) { | |
jQuery(callBack); | |
this.callBackList.push(callBack); | |
}, | |
executed: function(functionToVerify) { | |
for(functionIndex in this.callBackList) { | |
if(this.callBackList[functionIndex] == functionToVerify) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
return { | |
create: function() { | |
return this.instance != null ? this.instance : this.instance = new PageLoadHandlerObject(); | |
} | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("PageLoadHandler", function() { | |
var whenPageLoads; | |
beforeEach(function() { | |
whenPageLoads = PageLoadHandler.create(); | |
}); | |
it("behaves as a singleton", function() { | |
otherPageLoader = PageLoadHandler.create(); | |
expect(otherPageLoader).toBe(whenPageLoads); | |
}); | |
describe('execute', function() { | |
it("passes the function it is called with to jQuery's onLoad", function() { | |
spyOn(window,'jQuery'); | |
var bla = function() {} | |
whenPageLoads.execute(bla); | |
expect(window.jQuery).toHaveBeenCalledWith(bla); | |
}); | |
}); | |
describe("executed", function() { | |
it("lets us know we executed a given method", function() { | |
var yarp = function() {} | |
whenPageLoads.execute(yarp); | |
expect(whenPageLoads).toHaveExecuted(yarp); | |
}); | |
it("lets us we did not execute a method", function() { | |
var dorp = function() {} | |
expect(whenPageLoads).not.toHaveExecuted(dorp) | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(function() { | |
this.addMatchers({ | |
toHaveExecuted: function(method) { return this.actual.executed(method); } | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment