Rajan,
Thanks for the info. In reality I don't think we'll be spying directly on jquery very often. But more often will be doing spyOn($, 'ajax') as you mention.
I think the main utility is in accessing code that is protected by closure.
So, if I have some jquery selection that is protected by closure from being injected with a spy directly, and is part of a large code execution block (like a 'DOM-ready' style event), spying on jquery can be useful to isolate a single assertion in the midst of a large action.
For instance, in this example a layout is applied to a portion of the window. But perhaps we want to inject a spy for one of the jquery selections in the executing code.
var layoutDiv = $('#layout-div');
layoutDivSpy = spyOn(layoutDiv, 'renderLayout').andCallFake(function() {});
$ = spyOn$().andCallFake(function(selector) {
switch (selector) {
case "#layout-div":
return layoutDivSpy;
break;
default:
return jQuery(selector);
}
});
initScreenLoad(); // SUT
expect(layoutDivSpy.renderLayout).toHaveBeenCalled();
This may be too tight a coupling between the test and the system code, but that's the type of operation I think could be useful.
Thanks,
Eric