How do you setup correct scoping in a QUnit test environment for testing callback functions?
Code to test:
<script type="text/javascript">
APP = {};
APP.callBack = function() {
$(this).closest("input").val('foobar');
};
$(function() {
$("#button").click(APP.callBack);
});
</script>
<div>
<a id="button" href="#"></a>
<input id="id-for-testing-only" name="test" type="text" value="barfoo" />
</div>
Test code:
test("try callback with 'this' scope", function() {
APP.callBack();
equals($("#id-for-testing-only").val(), "foobar", "should set value to 'foobar'");
});
I think, you might want to use
.trigger()to trigger ‘click’ on button and then check the values, instead of directly calling your callback function, which would not be scoped to the button’sthiswhen called independently.