Created
November 22, 2012 12:28
-
-
Save eshiota/4130937 to your computer and use it in GitHub Desktop.
Testing form submission with jQuery and Jasmine
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
// 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