HTML and CSS Reference
In-Depth Information
Listing 9.5
Test case covering the
create
method
TestCase("TabControllerCreateTest", {
setUp: setUp,
"test should fail without element": function () {
assertException(function () {
tabController.create();
}, "TypeError");
},
"test should fail without element class": function () {
assertException(function () {
tabController.create({});
}, "TypeError");
},
"should return object": function () {
var controller = tabController.create(this.tabs);
assertObject(controller);
},
"test should add js-tabs class name to element":
function () {
var tabs = tabController.create(this.tabs);
assertClassName("js-tab-controller", this.tabs);
},
// Test for event handlers, explained later
});
The implementation shown in Listing 9.6 is fairly straightforward. Staying out
of the global namespace, the
tabController
object is implemented inside the
existing
tddjs
namespace.
The method makes one possibly unsafe assumption: The DOM 0 event listener
(the
onclick
property). The assumption the script implicitly is making is that no
other script will hijack the
ol
element's
onclick
listener. This might seem like a
reasonable expectation, but using DOM 2 event listeners is a much safer choice.
As mentioned previously, we will defer their use to Chapter 15,
TDD and DOM
Manipulation: The Chat Client,
in which we'll also discuss how to test them.