-
-
Save zspencer/1216413 to your computer and use it in GitHub Desktop.
How to unit test this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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()); | |
} | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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