Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Created March 21, 2013 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoolihan/5209879 to your computer and use it in GitHub Desktop.
Save thoolihan/5209879 to your computer and use it in GitHub Desktop.
TDD Example of Jasmine testing a backbone view
describe("affirmation create view", function(){
var a_view, values, div, affs;
beforeEach(function(){
div = $('<div class="_surface"></div>');
$('body').append(div);
affs = new App.Collections.Affirmations();
values = new App.Collections.Values([new App.Models.Value({description: 'x'})]);
a_view = new App.Views.NewAffirmation({
el: '._surface',
model: affs,
values: values
});
// adding an affirmation calls the router, so mock this out
App.Router ||= {}
spyOn(App.Router, 'navigate');
});
afterEach(function(){ div.remove();});
it("should render a value in the list of associated values", function(){
var li = a_view.$('input[type="checkbox"].value');
expect(li.length).toBe(1);
});
it("should save a new affirmation", function(){
spyOn(affs, 'create');
a_view.add_affirmation();
expect(affs.create).toHaveBeenCalled();
});
it("should clear the form after saving", function(){
spyOn(affs, 'create');
a_view.$('input[type="checkbox"]').attr('checked', true);
a_view.$('#description').val('You are a good person');
a_view.add_affirmation();
expect(a_view.$('#description').val()).toEqual('');
expect(a_view.$('input[type="checkbox"]:checked').length).toBe(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment