Skip to content

Instantly share code, notes, and snippets.

@zspencer
Forked from mdoel/mapform.js
Created September 14, 2011 12:16
Show Gist options
  • Save zspencer/1216413 to your computer and use it in GitHub Desktop.
Save zspencer/1216413 to your computer and use it in GitHub Desktop.
How to unit test this
$.fn.inFieldLabels = function() {}
$.fn.map_control = function() {}
$(document).ready(function() {
new MapForm();
});
(function() {
window.MapForm = function() {
$(document).ready(this.initialize);
};
window.MapForm.prototype = {
initialize: function() {
$('#map-searchform .searchform-fields label').inFieldLabels();
$('#map-searchform').submit(this.handleFormSubmit);
},
handleFormSubmit: function(e) {
e.preventDefault();
$('#map').map_control().find($('#map-search-terms').val());
}
}
})();
describe('a map form', function() {
var subject;
describe('instantiation', function() {
beforeEach(function() {
spyOn($.fn, 'ready').andCallFake(function() {
expect(this).toEqual($(document))
});
subject = new MapForm();
});
it('registers a dom ready call for its initialize function', function() {
expect($.fn.ready).toHaveBeenCalledWith(subject.initialize);
});
});
describe('initializing', function() {
beforeEach(function() {
spyOn($.fn,'inFieldLabels').andCallFake(function() {
expect(this).toEqual($('#map-searchform .searchform-fields label'))
});
spyOn($.fn,'submit');
subject.initialize();
});
it('activates the inFieldLabels on the map search form', function() {
expect($.fn.inFieldLabels).toHaveBeenCalled();
});
it('binds the map form submit handler to the submit', function() {
expect($.fn.submit).toHaveBeenCalledWith(subject.handleFormSubmit);
});
});
describe('submitting the map form', function() {
var e;
var map_control = jasmine.createSpyObj('MapControl',['find']);
beforeEach(function() {
spyOn($.fn, 'val').andReturn(1);
spyOn($.fn, 'map_control').andReturn(map_control);
e = jasmine.createSpyObj('Event',['preventDefault']);
subject.handleFormSubmit(e)
});
it('prevents default', function() {
expect(e.preventDefault).toHaveBeenCalled();
});
it('finds based upon the map search terms', function() {
expect(map_control.find).toHaveBeenCalledWith(1);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment