Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created April 20, 2011 22:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboesch/933239 to your computer and use it in GitHub Desktop.
Save jboesch/933239 to your computer and use it in GitHub Desktop.
RequireJS, BackboneJS and testing with Jasmine. Whew.
describe("AjaxForm", function() {
// Local vars for manipulation
var $form = null,
_requirejs_loaded = false,
found_errors = false,
submitted_successfully = false,
AjaxFormInstance = null,
RequireJSViews = {
AjaxFormView: null
};
require(TF.Modules.AjaxForm.js, function(AjaxFormView){
require.ready(function()
{
RequireJSViews.AjaxFormView = AjaxFormView;
_requirejs_loaded = true;
});
});
// Don't do anything until RequireJS loads and does its thing
waitsFor(function(){
return _requirejs_loaded;
}, 'RequireJS loading modules');
runs(function(){
$form = $('#spec-AjaxForm form');
// Call our stored backbone view
var opts = {
el: $form,
ajaxSubmit_opts: {
success: function(data)
{
found_errors = false;
submitted_successfully = true;
}
},
validate_opts: {
invalidHandler: function(form, validator)
{
found_errors = true;
submitted_successfully = false;
}
}
};
AjaxFormInstance = new RequireJSViews.AjaxFormView(opts);
});
it('should fail the initial validation submission (username required & enter same pw again)', function(){
$form.find('#password').val('pw');
$form.submit();
expect(submitted_successfully).toBeFalsy();
var errors = AjaxFormInstance.el.data('validator').errorList.length;
expect(errors).toEqual(2);
});
it('should fail the password/confirm password check', function(){
$form.find('#username').val('jboesch');
$form.find('#password').val('pw');
$form.submit();
expect(found_errors).toBeTruthy();
var errors = AjaxFormInstance.el.data('validator').errorList.length;
expect(errors).toEqual(1);
});
it('should pass validation!', function(){
$form.find('#username').val('jboesch');
$form.find('#password').val('pw');
$form.find('#confirm_password').val('pw');
$form.submit();
waitsFor(function(){
//console.log(AjaxFormInstance.el.data('validator'));
return submitted_successfully;
}, 'Should eventually validate to true. We run this because the call that validates is async', 3000);
});
});
@mathieul
Copy link

This didn't work for me, so I wrote this file to be included before jasmine specs: https://gist.github.com/966776 - comments describe how to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment