HTML and CSS Reference
In-Depth Information
var event = { preventDefault: stubFn() };
var input = this.element.getElementsByTagName("input")[0];
input.value = "cjno";
this.controller.setModel(model);
this.controller.setView(this.element);
this.controller.handleSubmit(event);
assertEquals("cjno", model.currentUser);
}
The test adds a stub model object with the so far non-existent
setModel
method. The fact that the method is missing causes the test to fail, so Listing 15.23
adds the method.
Listing 15.23
Adding
setModel
/* ... */
function setModel(model) {
this.model = model;
}
tddjs.namespace("chat").userFormController = {
setView: setView,
setModel: setModel,
handleSubmit: handleSubmit
};
One could argue that a simple setter such as this is superfluous, but providing
setView
and
setModel
methods makes the interface consistent and predictable.
When ECMAScript 5 becomes widely supported, we can do one better by using
native setters, which untangles the explicit method calls.
Next up, we need to make the
handleSubmit
method actually pick up the
current value of the input field. Listing 15.24 fills in the blanks.
Listing 15.24
Picking up the username
function handleSubmit(event) {
event.preventDefault();
var input = this.view.getElementsByTagName("input")[0];
this.model.currentUser = input.value;
}