HTML and CSS Reference
In-Depth Information
12.3.1 The First Test
The very first test we will write is the one that expects an
XMLHttpRequest
object.
As outlined in Section 12.2.2,
Choosing the Interface Style,
the properties we rely
on are the
open
and
send
methods. The
onreadystatechange
handler needs
the
readyState
property to know when the request has finished. Last, but not
least, we will eventually need the
setRequestHeader
method in order to, well,
set request headers.
Listing 12.3 shows the test in full; save it in
test/ajax
_
test.js
.
Listing 12.3
Testing for an
XMLHttpRequest
object
TestCase("AjaxCreateTest", {
"test should return XMLHttpRequest object": function () {
var xhr = tddjs.ajax.create();
assertNumber(xhr.readyState);
assert(tddjs.isHostMethod(xhr, "open"));
assert(tddjs.isHostMethod(xhr, "send"));
assert(tddjs.isHostMethod(xhr, "setRequestHeader"));
}
});
This test fails as expected because there is no
tddjs.ajax
namespace.
Listing 12.4 shows the namespace declaration that goes in
src/ajax.js
. In order
for this to run, the
tddjs.namespace
method from Chapter 6,
Applied Functions
and Closures,
needs to be available in
lib/tdd.js
.
Listing 12.4
Creating the ajax namespace
tddjs.namespace("ajax");
With this in place the test fails in response to the missing
create
method. We
will need a little background before we can implement it.
12.3.2
XMLHttpRequest
Background
Microsoft invented
XMLHttpRequest
as an ActiveX object back in 1999. Com-
petitors followed suit shortly after, and today the object is available in just about
every current browser. It's even on its way to becoming a W3C standard, in Last
Call Working Draft at the time of writing. Listing 12.5 shows how the object is