HTML and CSS Reference
In-Depth Information
“nuke” the body of the
ajax.get method
, leaving it to delegate its work to
ajax.request
, forcing a GET request.
12.6.1.1 Extracting
ajax.request
Extracting the new method isn't magic; simply copy-paste
ajax.get
and rename
it, as seen in Listing 12.51.
Listing 12.51
Copy-pasting
ajax.get
to
ajax.request
function request(url, options) {
// Copy of original ajax.get function body
}
ajax.request = request;
Remember to run the tests after each step while refactoring. In this case, only a
copy-paste mistake resulting in a syntax error could possibly break the code because
the new method isn't being called yet.
12.6.1.2 Making the Method Configurable
Next up is to make the request method a configurable option on the
ajax.
request
method. This is new functionality and so requires a test, as seen in
Listing 12.52.
Listing 12.52
Request method should be configurable
function setUp() {
this.tddjsIsLocal = tddjs.isLocal;
this.ajaxCreate = ajax.create;
this.xhr = Object.create(fakeXMLHttpRequest);
ajax.create = stubFn(this.xhr);
}
function tearDown() {
tddjs.isLocal = this.tddjsIsLocal;
ajax.create = this.ajaxCreate;
}
TestCase("GetRequestTest", {
setUp: setUp,
tearDown: tearDown,
/* ... */
});