Might be stupid question about fixtures

76 views
Skip to first unread message

Tom Malone

unread,
May 14, 2012, 4:24:17 AM5/14/12
to jasmi...@googlegroups.com
I want to test that certain actions occur when an action is clicked, I also want to leave all JavaScript out of my html, so I have attempted to bind my eventhandler using jquery so I end up with something like:

jQuery(function () {
    jQuery('#myButton').bind('click', function (event) {
        event.preventDefault();
        doSomething();
    });
});

Then set up a spy to watch the doSomething() method. The only problem is that it can't find the button with id  myButton.

If I set up a function to run the event handler binding and call it from my spec then everything works as expected.

I am running this using the standalone html spec runner and I believe that the functions if firing against the actual spec runner and not my fixture.

Is there a way of fixing this, any help would be appreciated.

Thanks

Tom

Rajan Agaskar

unread,
May 14, 2012, 3:11:50 PM5/14/12
to jasmi...@googlegroups.com
The way I've seen this solved is by setting up a custom jquery event (say, "initialize") and bind your UOJS initializers to that; then have that custom jquery event triggered using the jquery page load callback. This allows you to arbitrarily trigger initialization in tests. IE: 

$(function() {
  $(document).trigger("initialize");
});

$(document).bind("initialize", function() {
    jQuery('#myButton').bind('click', function (event) {
        event.preventDefault();
        doSomething();
    });
});

Then in your test, you can simply $(document).trigger('initialize') whenever your DOM is appropriately configured. 

Thanks!

Rajan


Tom

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/q8rVvFoKsXIJ.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.

Reply all
Reply to author
Forward
0 new messages