Last active
December 15, 2015 07:09
-
-
Save henrik/5221144 to your computer and use it in GitHub Desktop.
Trigger jQuery DOM ready callbacks any time you like in mocha JS tests.
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
# Code: | |
$ -> # a.k.a. jQuery.ready -> | |
$("#hash-value-when-page-loaded").text(location.hash) | |
# Test: | |
location.hash = "foo" | |
fakeDomReady() | |
$("#hash-value-when-page-loaded").text().should.equal "#foo" |
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
# Wrap jQuery.ready so we can trigger it in | |
# our mocha tests, which replace the body after | |
# the original ready event. | |
window.domReadyFunctions = [] | |
window.fakeDomReady = -> | |
fn() for fn in domReadyFunctions | |
$.fn.oldReady = $.fn.ready | |
$.fn.ready = (fn) -> | |
domReadyFunctions.push(fn) if fn | |
$.fn.oldReady.apply(this, arguments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use Sinonjs, you can just do this...