In an effort to not duplication initialization code, I want to be able to rerun the $.ready function in the setup of each unit test rather than copy the actual code to the setup of the test. In jQuery 1.3.2, I was able to achieve this by overriding the jQuery.ready function with the following code:
- "use strict";
- jQuery.ready = function () {
- // Remember that the DOM is ready
- jQuery.isReady = true;
- // If there are functions bound, to execute
- if (jQuery.readyList) {
- // Execute all of them
- jQuery.each(jQuery.readyList, function () {
- this.call(document, jQuery);
- });
- }
- // Trigger any bound ready events
- jQuery(document).triggerHandler("ready");
- };
This basically would allow me to call the function after the DOM had already been loaded and cause the readyList not to be nulled out so that I could run through those again each time.
Now with jQuery 1.4.2 readyList appears to be hidden within the jQuery closure so it is not accessible to my attempt to override $.ready(), causing the if to fail and the readyList execution to be skipped. Because of this, all of my tests that depend on event handlers being bound fail.
Maybe the way I was doing this override wasn't the proper way anyway, so now am at a point worth asking: Is there a more appropriate way to reinitialize this code?
Obviously, to have proper code coverage, initialization code must be tested. In order to make sure I'm testing the code that is actually used in the live code, it can't be duplicated in test setup. I keep code modularized by having separate $(document).ready initialization block in the separate source files and load these files into the test runner as appropriate.
With readyList now being private within jQuery, please point me in a better direction that doesn't include changing the source code all around. I need a new solution, but I don't want it to require me (or convincing my team) to write jQuery code in a way that is completely different from what is commonly accepted practice just to test it.