HTML and CSS Reference
In-Depth Information
The test fails as dispatch was not called. To fix this we need to parse the
responseText as JSON and call the method from within the success callback of
the request. A very naive implementation can be seen in Listing 13.63.
Listing 13.63 Naive success callback to the poller
function connect() {
if (!this.url) {
throw new TypeError("Provide client URL");
if (!this.poller) {
this.poller = ajax.poll(this.url, {
success: function (xhr) {
At this point I am expecting this test to still fail in at least a few browsers. As
we discussed in Chapter 8, ECMAScript 5th Edition, EcmaScript5 specifies a JSON
object. However, it is not yet widely implemented, least of all in older browsers such
as Internet Explorer 6. Still, the tests pass. What's happening is that JsTestDriver is
already using Douglas Crockford's JSON parser internally, and because it does not
namespace its dependencies in the test runner, our test accidentally works because
the environment loads our dependencies for us. Hopefully, this issue with JsTest-
Driver will be worked out, but until then, we need to keep this in the back of our
heads. The proper solution is of course to add, e.g., json2.js from in
lib/ .
I mentioned that the above implementation was naive. A successful response
from the server does not imply valid JSON. What do you suppose happens when
the test in Listing 13.64 runs?
Listing 13.64 Expecting badly formed data not to be dispatched
"test should not dispatch badly formed data": function () {
this.client.url = "/my/url";
this.client.dispatch = stubFn();
Search WWH ::

Custom Search