HTML and CSS Reference
In-Depth Information
Running the tests verifies that this works. Note that the way the test was written
will allow it to succeed for any interval smaller than 1,000 milliseconds. If we wanted
to ensure that the delay is exactly 1,000, not any value below it, we can write another
test that ticks the clock 999 milliseconds and asserts that the callback was not called.
Before we move on we need to inspect the code so far for duplication and other
possible refactorings. All the tests are going to need a poller object, and seeing as
there is more than one line involved in creating one, we will extract setting up the
object to the setUp method, as seen in Listing 13.18.
Listing 13.18 Extracting poller setup
setUp: function () {
/* ... */
this.poller = Object.create(ajax.poller);
this.poller.url = "/url";
Moving common setup to the right place enables us to write simpler tests while
still doing the same amount of work. This makes tests easier to read, better at
communicating their intent, and less prone to errors—so long as we don't extract
too much.
Listing 13.19 shows the test that makes sure we wait the full interval.
Listing 13.19 Making sure the full 1,000ms wait is required
"test should not make new request until 1000ms passed":
function () {
this.xhr.send = stubFn();
This test passes immediately, as we already implemented the setTimeout call
correctly. Configurable Intervals
The next step is to make the polling interval configurable. Listing 13.20 shows how
we expect the poller interface to accept interval configuration.
Search WWH ::

Custom Search