Chapter 15. Legacy JavaScript

Michael Feathers defines legacy code as code without tests [WEwLC]. JavaScript and other dynamic languages benefit from tests by exercising a wider range of the dynamic behaviors that sometimes make them behave in unanticipated ways. Open-source projects benefit from tests so that multiple contributors can make changes without each having to be intimately familiar with all behaviors.

In contrast, open-source JavaScript projects without tests survive through simplicity or a small number of immersed maintainers, and often both. Increasingly, open-source JavaScript projects include tests, but some very useful and popular projects remain without them.

The jQuery Timepicker Addon1 by Trent Richardson is one such project. The Timepicker Addon injects time selection into the jQuery UI Datepicker.2 As you might expect, this allows the selection of times along with dates. It goes well beyond a simple time drop-down menu, however, supporting formatting, time zones, sliders, multiple fields, ranges, constraints, and more. Fortunately, the project includes very thorough documentation with examples, making the task of testing it easier. The project may be used under either GPL or MIT licenses. Trent has wanted to add testing to the project for some time and has eagerly supported this effort and incorporated it into the distribution.

1. The project page with documentation and examples can be found at http://trentrichardson.com/examples/timepicker/, with the source code at http://github.com/trentrichardson/jQuery-Timepicker-Addon.

2. http://jqueryui.com/datepicker/

You can find my fork of the original project at https://github.com/srvance/jQuery-Timepicker-Addon, which is the repository containing the hash references for this chapter. The commits referenced in the chapter are on the “dev” branch of the repository; this is where Trent requests work to be submitted. As with Chapter 14, I have omitted practices—such as a Grunt build, jslint or jshint, and code coverage—that I would use in a more production-oriented context. I used IntelliJ, with JavaScript intentions enabled, in order to leverage the refactoring support it provides. This also gives some level of static analysis to catch questionable usages. I left the Chrome browser running on a different monitor, using the LivePage3 plug-in to continuously reload the Jasmine4 spec runner for the project.

3. https://chrome.google.com/webstore/detail/livepage/pilnojpmdoofaelbinaeodfpjheijkbh?hl=en

4. http://pivotal.github.io/jasmine/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.229.44