HTML and CSS Reference
In-Depth Information
To pass the test, we will call the success callback whenever the request is for a
local file and the status code is not set. Listing 12.45 shows the updated ready state
change handler.
Listing 12.45 Allow local requests to succeed
function requestComplete(transport, options) {
var status = transport.status;
if (status == 200 || (tddjs.isLocal() && !status)) {
if (typeof options.success == "function") {
options.success(transport);
}
}
}
The implementation passes the test. In order to have this working in a browser
as well, we need to implement the helper that determines if the script is running
locally, as seen in Listing 12.46. Add it to the lib/tdd.js file.
Listing 12.46 Checking current URL to decide if request is local
tddjs.isLocal = (function () {
function isLocal() {
return !!(window.location &&
window.location.protocol.indexOf("file:") === 0);
}
return isLocal;
}());
With this helper in place we can re-run the integration test locally, and observe
that it now loads the HTML fragment.
12.5.5 Testing Statuses
We finished another step—a test and a few lines of production code—it's time to
review and look for duplication. Even with the stub helpers we added previously,
the tests that verify behavior for different sets of readyState and status codes
look awfully similar. And still we haven't tested for other 2xx status codes, or any
error codes at all.
To reduce the duplication, we will add a method to the fakeXMLHttp-
Request object that allows us to fake its ready state changing. Listing 12.47 adds a
method that changes the ready state and calls the onreadystatechange handler.
 
Search WWH ::




Custom Search