HTML and CSS Reference
In-Depth Information
Listing 12.33 Testing ready state handler with successful request
TestCase("ReadyStateHandlerTest", {
setUp: function () {
this.ajaxCreate = ajax.create;
this.xhr = Object.create(fakeXMLHttpRequest);
ajax.create = stubFn(this.xhr);
},
tearDown: function () {
ajax.create = this.ajaxCreate;
},
"test should call success handler for status 200":
function () {
this.xhr.readyState = 4;
this.xhr.status = 200;
var success = stubFn();
ajax.get("/url", { success: success });
this.xhr.onreadystatechange();
assert(success.called);
}
});
Because we are going to need quite a few tests targeting the onreadystate-
change handler, we create a new test case. This way it's implicit that test names
describe expectations on this particular function, allowing us to skip prefixing every
test with “onreadystatechange handler should.” It also allows us to run these tests
alone should we run into trouble and need even tighter focus.
To pass this test we need to do a few things. First, ajax.get needs to accept
an object of options; currently the only supported option is a success callback. Then
we need to actually add a body to that ready state function we added in the previous
section. The implementation can be viewed in Listing 12.34.
Listing 12.34 Accepting and calling the success callback
(function () {
var ajax = tddjs.namespace("ajax");
function requestComplete(transport, options) {
if (transport.status == 200) {
 
Search WWH ::




Custom Search