A lot of processes within a Web application are triggered by Document Object Model (DOM) events. These include button clicks, setting the focus on an element, document loading, unloading, and any other event resulting from a user action or the browser. That’s why any good Unit Testing Framework will provide methods to deal with DOM events. Jasmine is no exception. It makes it possible to test that a given event occurred and what the results of the event were. In today’s article, we’re going to create some tests for the button click event using Jasmine 2.0, jQuery, and an additional third-party library.
What You’ll Need
The add-on library that we’ll be using is built on Jasmine 2.0 and jQuery and is called jasmine-jquery. Here are the details on it and the other two libraries:
- Jasmine 2.0: In this new major release, the makers of Jasmine have dropped the runs() and waitsFor() methods in favor of the Mocha done() callback, which has been added to beforeEach(), it() and afterEach(). If you plan on performing any tests on asynchronous processes, then this version is a must.
- Jasmine-jQuery: There are a lot of third party libraries that enhance Jasmine’s basic functionality. The one we’ll be using here, jasmine-jquery, adds a set of custom matchers for jQuery as well as an API for creating and managing HTML, CSS, and JSON fixtures in your specs. All of the functionality is contained in the jasmine-jquery.js file so all you need to do is include it with your other JS files and reference it from your SpecRunner.html file using a