TAILIEUCHUNG - Test Driven JavaScript Development- P21

Test Driven JavaScript Development- P21:This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way. | The User Form 393 The next test shown in Listing expects setView to be a function. Listing Expecting setview to be a function test should have setView method function assertFunction Listing adds an empty method to pass the test. Listing Adding an empty setview method function function setView element chat .userFormController setview setview Adding a Class The first actual behavior we ll test for is that the js-chat class name is added to the DOM element as seen in Listing . Note that the test requires the implementation from Chapter 7 Objects and Prototypal Inheritance in lib to run smoothly across browsers. Listing Expecting the view to have its class name set TestCase UserFormControllerSetViewTest test should add js-chat class function var controller userController var element element assertClassName js-chat element The first thing that sticks out about this test is that it contains no DOM elements. It does however use the assertClassName assertion which checks if an element has the given class name. This assertion is generic and only checks that the object defines a string property className and that one of its space separated values matches the provided string. Please purchase PDF Split-Merge on to remove this watermark. From the Library of 394 TDD and DOM Manipulation The Chat Client The element object is a simple stub object. At this point there s no need to use a real DOM element because all we want to check is that some property was assigned. The test fails and Listing assigns the class name to pass it. Listing Adding the class function setView element js-chat At this point you might worry about a few things. Should we really be overriding the class name like that Should the class name not be configurable Remember You ain t gonna need it At this point we have no .