Skip to content

Instantly share code, notes, and snippets.

@eshiota
Created November 22, 2012 12:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eshiota/4130937 to your computer and use it in GitHub Desktop.
Save eshiota/4130937 to your computer and use it in GitHub Desktop.
Testing form submission with jQuery and Jasmine
// sizeSelectionModule and addToCartModule are local variables that
// act as shortcuts to their namespaces
describe("when add to cart form is submitted", function () {
var $form;
beforeEach(function () {
// We use div instead of form so we can just mock the submit behavior
$form = $("<div />");
});
it("validates the size selection", function () {
spyOn(sizeSelectionModule, "validate");
addToCartModule.init($form);
$form.trigger("submit");
expect(sizeSelectionModule.validate).toHaveBeenCalled();
});
it("prevents form submission if size selection is invalid", function () {
spyOnEvent($form, "submit");
spyOn(sizeSelectionModule, "validate").andReturn(false);
addToCartModule.init($form);
$form.trigger("submit");
expect("submit").toHaveBeenPreventedOn($form);
});
it("submits the form if size selection is valid", function () {
spyOnEvent($form, "submit");
spyOn(sizeSelectionModule, "validate").andReturn(true);
addToCartModule.init($form);
$form.trigger("submit");
expect("submit").not.toHaveBeenPreventedOn($form);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment