Skip to content

Instantly share code, notes, and snippets.

@zspencer
Created January 27, 2011 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zspencer/799124 to your computer and use it in GitHub Desktop.
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
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();
}
}
})();
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)
});
});
});
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