Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ten Things You Should Know About Jasmine

Ten Things You Should Know About Jasmine

The movement toward building JavaScript rich web applications like Twitter and GMail brings with it some unique challenges. In this talk you'll learn how Jasmine, a BDD JavaScript testing framework will help you overcome these hurdles, and write better, more maintainable code.

Damian Nicholson

April 10, 2012
Tweet

More Decks by Damian Nicholson

Other Decks in Programming

Transcript

  1. $(‘a’).on(‘click’, function(e) { var activeClass = ‘active’; e.preventDefault(); $(this) .addClass(activeClass)

    .siblings() .removeClass(activeClass); ... }); Jasmine doesn’t really lend itself to testing code written like this
  2. $(‘a’).on(‘click’, function(e) { var activeClass = ‘active’; e.preventDefault(); $(this) .addClass(activeClass)

    .siblings() .removeClass(activeClass); ... }); Fine when building a website, but not a web app
  3. “If its hard to test its a good sign that

    you need to refactor your code”
  4. function Tabs(options) { var defaults = { activeClass: ‘active’ };

    this.options = $.extend({}, defaults, options); this.$tabs = $(‘a’); this.$tabs.on(‘click’, $.proxy(this.handleClick, this)); }; Tabs.prototype.handleClick = function(e) { e.preventDefault(); this.$tabs.removeClass(this.options.activeClass); $(e.target).addClass(this.options.activeClass); }; Far easier to test and maintain
  5. describe(“Tabs”, function() var instance; beforeEach(function() { instance = new Tabs();

    }); describe(“initialize”, function() { it(“should have three tabs”, function() { expect(instance.$tabs.length).toEqual(3); }); ... }); });
  6. describe(“Tabs”, function() { ... describe(“on click”, function() { beforeEach(function() {

    instance.$tabs.first().trigger(‘click’); }); it(“should have an active class”, function() { var activeClass = instance.options.activeClass; expect(instance.$tabs.first().hasClass(activeClass)).toBeTruthy(); }); });
  7. describe(“Tabs”, function() { template(‘tabs.html’); describe(“on click”, function() { beforeEach(function() {

    instance.$tabs.first().trigger(‘click’); }); it(“should have an active class”, function() { var activeClass = instance.options.activeClass; expect(instance.$tabs.first().hasClass(activeClass)).toBeTruthy(); }); });
  8. describe(“Tabs”, function() { ... describe(“on click”, function() { var event;

    beforeEach(function() { event = $.Event(‘click’); spyOn(event, ‘preventDefault’); instance.$tabs.first().trigger(event); }); it(“should have called preventDefault on the event”, function() { expect(event.preventDefault).toHaveBeenCalled(); }); });
  9. describe(“Tabs”, function() { ... describe(“load content in to tab”, function()

    { var request; beforeEach(function() { jasmine.Ajax.useMock(); tabs.loadContent(); // This function call contains an AJAX request request = mostRecentAjaxRequest(); request.response({ foo: ‘bar’ }); }); it(“should respond with foo”, function() { expect(request.foo).toEqual(‘bar’); }); ... });