HTML and CSS Reference
In-Depth Information
assertNoException(function () {
this.client.dispatch();
}.bind(this));
},
"test should not throw if event is null": function () {
assertNoException(function () {
this.client.dispatch({ myEvent: null });
}.bind(this));
}
});
Running the tests somewhat surprisingly reveals that only the last test fails.
The
tddjs.each
method that is used for looping was built to handle input not
suitable for looping, so
dispatch
can already handle
null
and a missing
data
argument. To pass the last test, we need to be a little more careful in the loop over
event objects, as seen in Listing 13.52.
Listing 13.52
Carefully looping event data
function dispatch(data) {
/* ... */
tddjs.each(data, function (topic, events) {
var length = events && events.length;
for(vari=0;i<length; i++) {
observers.notify(topic, events[i]);
}
});
}
In order to make the
dispatch
test case complete, we should add some tests
that make sure that
notify
is really called for all topics in
data
, and that all events
are passed to observers of a topic. I'll leave doing so as an exercise.
13.4.4 Adding Observers
With a functional
dispatch
we have what we need to test the
observe
method.
Listing 13.53 shows a simple test that expects that observers to be called when data
is available.