Testing DOM Events Using jQuery and Jasmine 2.0 | HTML Goodies

Testing DOM Events Using jQuery and Jasmine 2.0

Written By
RG
Rob Gravelle
Jan 29, 2014
5 minute read

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
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.