Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

mathieul commented May 11, 2011

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