HTML and CSS Reference
In-Depth Information
Listing 15.16 Expecting the event's preventDefault method to be called
TestCase("UserFormControllerHandleSubmitTest", {
"test should prevent event default action": function () {
var controller = Object.create(userController);
var event = { preventDefault: stubFn() };
controller.handleSubmit(event);
assert(event.preventDefault.called);
}
});
Again we put our trust in a stubbed function. Passing this test requires a single
line of added code, as Listing 15.17 shows.
Listing 15.17 Preventing the default action
function handleSubmit(event) {
event.preventDefault();
}
Now that the test passes, we can start worrying about duplicating setup between
the two test cases. As usual, we'll simply extract the setup to a local function that
both test cases can share, as Listing 15.18 shows.
Listing 15.18 Sharing setup
function userFormControllerSetUp() {
this.controller = Object.create(userController);
this.element = {};
dom.addEventHandler = stubFn();
}
TestCase("UserFormControllerSetViewTest", {
setUp: userFormControllerSetUp,
/* ... */
});
TestCase("UserFormControllerHandleSubmitTest", {
setUp: userFormControllerSetUp,
"test should prevent event default action": function () {
var event = { preventDefault: stubFn() };
 
Search WWH ::




Custom Search