HTML and CSS Reference
In-Depth Information
}.bind(this));
return promise;
};
With the event in place, we can build the waitForMessagesSince method.
14.5.2 Waiting for Messages
The waitForMessagesSince method will do one of two things; if messages are
available since the provided id, the returned promise will resolve immediately. If no
messages are currently available, the method will add a listener for the “message”
event, and the returned promise will resolve once a new message is added.
The test in Listing 14.59 expects that the promise is immediately resolved if
messages are available.
Listing 14.59 Expecting available messages to resolve immediately
/* ... */
var Promise = require("node-promise/promise").Promise;
var stub = require("stub");
/* ... */
testCase(exports, "chatRoom.waitForMessagesSince", {
setUp: chatRoomSetup,
"should yield existing messages": function (test) {
var promise = new Promise();
promise.resolve([{ id: 43 }]);
this.room.getMessagesSince = stub(promise);
this.room.waitForMessagesSince(42).then(function (m) {
test.same([{ id: 43 }], m);
test.done();
});
}
});
This test stubs the getMessagesSince method to verify that its results are
used if there are any. To pass this test we can simply return the promise returned
from getMessagesSince , as seen in Listing 14.60.
 
Search WWH ::




Custom Search