Permalink
Browse files

Add tests for NestedForm::FormtasticBuilder and NestedForm::SimpleBui…

…lder.
  • Loading branch information...
1 parent 47dc4d9 commit 1fb6b1c7767c3770a13cf1b5247b3f1e795cb361 @Godisemo committed Jul 26, 2011
@@ -0,0 +1,54 @@
+module('Validate Nested Formtastic', {
+ setup: function() {
+ window['new_user'] = {
+ type: 'NestedForm::FormtasticBuilder',
+ inline_error_class: 'inline-errors',
+ validators: {
+ "user[name]":{"presence":{"message": "must be present"}, "format":{"message":"is invalid","with":/\d+/}}
+ }
+ }
+
+ $('#qunit-fixture')
+ .append($('<form />', {
+ action: '/users',
+ 'data-validate': true,
+ method: 'post',
+ id: 'new_user'
+ }))
+ .find('form')
+ .append('<li />').find('li')
+ .append($('<input />', {
+ name: 'user[name]',
+ id: 'user_name',
+ 'data-validate': 'true',
+ type: 'text'
+ }))
+ .append($('<label for="user_name">Name</label>'));
+ $('form#new_user').validate();
+ }
+});
+
+test('Validate error attaching and detaching', function() {
+ var form = $('form#new_user'), input = form.find('input#user_name');
+ var label = $('label[for="user_name"]');
+
+ input.trigger('focusout')
+ ok(input.parent().hasClass('error'));
+ ok(label.parent().hasClass('error'));
+ ok(input.parent().find('p.inline-errors:contains("must be present")')[0]);
+
+ input.val('abc')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(input.parent().hasClass('error'));
+ ok(label.parent().hasClass('error'));
+ ok(input.parent().find('p.inline-errors:contains("is invalid")')[0]);
+
+ input.val('123')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(!input.parent().hasClass('error'));
+ ok(!label.parent().hasClass('error'));
+ ok(!input.parent().find('p.inline-errors')[0]);
+});
+
@@ -0,0 +1,57 @@
+module('Validate Nested SimpleForm', {
+ setup: function() {
+ window['new_user'] = {
+ type: 'NestedForm::SimpleBuilder',
+ error_class: 'error',
+ error_tag: 'span',
+ wrapper_error_class: 'field_with_errors',
+ wrapper_tag: 'div',
+ validators: {
+ "user[name]":{"presence":{"message": "must be present"}, "format":{"message":"is invalid","with":/\d+/}}
+ }
+ }
+
+ $('#qunit-fixture')
+ .append($('<form />', {
+ action: '/users',
+ 'data-validate': true,
+ method: 'post',
+ id: 'new_user'
+ }))
+ .find('form')
+ .append('<div />').find('div')
+ .append($('<input />', {
+ name: 'user[name]',
+ id: 'user_name',
+ 'data-validate': 'true',
+ type: 'text'
+ }))
+ .append($('<label for="user_name">Name</label>'));
+ $('form#new_user').validate();
+ }
+});
+
+test('Validate error attaching and detaching', function() {
+ var form = $('form#new_user'), input = form.find('input#user_name');
+ var label = $('label[for="user_name"]');
+
+ input.trigger('focusout');
+ ok(input.parent().hasClass('field_with_errors'));
+ ok(label.parent().hasClass('field_with_errors'));
+ ok(input.parent().find('span.error:contains("must be present")')[0]);
+
+ input.val('abc')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(input.parent().hasClass('field_with_errors'));
+ ok(label.parent().hasClass('field_with_errors'));
+ ok(input.parent().find('span.error:contains("is invalid")')[0]);
+
+ input.val('123')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(!input.parent().hasClass('field_with_errors'));
+ ok(!label.parent().hasClass('field_with_errors'));
+ ok(!input.parent().find('span.error')[0]);
+});
+

0 comments on commit 1fb6b1c

Please sign in to comment.