HTML and CSS Reference
In-Depth Information
In this test we no longer use the fake
XMLHttpRequest
object, because the
semantics of
ajax.poll
better describes the expected behavior. Asserting that
the method started polling in terms of
fakeXMLHttpRequest
would basically
mean duplicating
ajax.poll
's test case.
The test fails because
connect
is not a method. We will add it along with the
call to
ajax.poll
in one go, as seen in Listing 13.56.
Listing 13.56
Connecting by calling
ajax.poll
(function () {
/* ... */
function connect() {
ajax.poll(this.url);
}
ajax.cometClient = {
connect: connect,
dispatch: dispatch,
observe: observe
}
});
What happens if we call
connect
when the client is already connected? From
the looks of things, more polling. Listing 13.57 asserts that only one connection is
made.
Listing 13.57
Verifying that
ajax.poll
is only called once
"test should not connect if connected": function () {
this.client.url = "/my/url";
ajax.poll = stubFn({});
this.client.connect();
ajax.poll = stubFn({});
this.client.connect();
assertFalse(ajax.poll.called);
}
To pass this test we need to keep a reference to the poller, and only connect if
this reference does not exist, as Listing 13.58 shows.