Skip to content

Commit a49dd74

Browse files
committed
Fix JavaScript tests
Explicitly add a test for multiple css classes and fix a couple of tests that were non running properly
1 parent fbb7a5b commit a49dd74

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

test/javascript/public/test/validateElement.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ QUnit.module('Validate Element', {
33
dataCsv = {
44
html_settings: {
55
type: 'ActionView::Helpers::FormBuilder',
6-
input_tag: '<div class="class_one class_two field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
6+
input_tag: '<div class="field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
77
label_tag: '<div class="field_with_errors"><label id="label_tag"></label></div>'
88
},
99
validators: {
@@ -318,7 +318,7 @@ QUnit.test('Validate when error message needs to change', function (assert) {
318318

319319
QUnit.test("Don't validate confirmation when not a validatable input", function (assert) {
320320
dataCsv = {
321-
html_options: {
321+
html_settings: {
322322
type: 'ActionView::Helpers::FormBuilder',
323323
input_tag: '<div class="field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
324324
label_tag: '<div class="field_with_errors"><label id="label_tag"></label></div>'
@@ -327,6 +327,7 @@ QUnit.test("Don't validate confirmation when not a validatable input", function
327327
}
328328

329329
$('#qunit-fixture')
330+
.html('')
330331
.append($('<form>', {
331332
action: '/users',
332333
'data-client-side-validations': JSON.stringify(dataCsv),
@@ -363,10 +364,11 @@ QUnit.test("Don't validate disabled inputs", function (assert) {
363364
input_tag: '<div class="field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
364365
label_tag: '<div class="field_with_errors"><label id="label_tag"></label></div>'
365366
},
366-
validators: { 'user_2[name]': { presence: { message: 'must be present' } } }
367+
validators: { 'user_2[name]': { presence: [{ message: 'must be present' }] } }
367368
}
368369

369370
$('#qunit-fixture')
371+
.html('')
370372
.append($('<form>', {
371373
action: '/users',
372374
'data-client-side-validations': JSON.stringify(dataCsv),
@@ -427,6 +429,47 @@ QUnit.test("Don't validate dynamically disabled inputs", function (assert) {
427429
assert.notOk(input.parent().hasClass('field_with_errors'))
428430
})
429431

432+
QUnit.test("Removes error messages when input tag has more than two css classes", function (assert) {
433+
dataCsv = {
434+
html_settings: {
435+
type: 'ActionView::Helpers::FormBuilder',
436+
input_tag: '<div class="class_one class_two field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
437+
label_tag: '<div class="field_with_errors"><label id="label_tag"></label></div>'
438+
},
439+
validators: { 'user_2[name]': { presence: [{ message: 'must be present' }] } }
440+
}
441+
442+
$('#qunit-fixture')
443+
.html('')
444+
.append($('<form>', {
445+
action: '/users',
446+
'data-client-side-validations': JSON.stringify(dataCsv),
447+
method: 'post',
448+
id: 'new_user_2'
449+
}))
450+
.find('form')
451+
.append($('<label for="user_2_name">name</label>'))
452+
.append($('<input />', {
453+
name: 'user_2[name]',
454+
id: 'user_2_name',
455+
type: 'text'
456+
}))
457+
$('form#new_user_2').validate()
458+
var form = $('form#new_user_2')
459+
var input = form.find('input#user_2_name')
460+
461+
input.val('')
462+
input.trigger('focusout')
463+
464+
assert.ok(input.parent().hasClass('field_with_errors'))
465+
466+
input.val('123')
467+
input.trigger('change')
468+
input.trigger('focusout')
469+
470+
assert.notOk(input.parent().hasClass('field_with_errors'))
471+
})
472+
430473
QUnit.test('ensure label is scoped to form', function (assert) {
431474
var form = $('form#new_user')
432475
var input = form.find('input#user_name')

0 commit comments

Comments
 (0)