Skip to content

Commit

Permalink
Fix JavaScript tests
Browse files Browse the repository at this point in the history
Explicitly add a test for multiple css classes and fix a couple of tests
that were non running properly
  • Loading branch information
tagliala committed May 1, 2020
1 parent fbb7a5b commit a49dd74
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions test/javascript/public/test/validateElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ QUnit.module('Validate Element', {
dataCsv = {
html_settings: {
type: 'ActionView::Helpers::FormBuilder',
input_tag: '<div class="class_one class_two field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
input_tag: '<div class="field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag"></label></div>'
},
validators: {
Expand Down Expand Up @@ -318,7 +318,7 @@ QUnit.test('Validate when error message needs to change', function (assert) {

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

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

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

QUnit.test("Removes error messages when input tag has more than two css classes", function (assert) {
dataCsv = {
html_settings: {
type: 'ActionView::Helpers::FormBuilder',
input_tag: '<div class="class_one class_two field_with_errors"><span id="input_tag"></span><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag"></label></div>'
},
validators: { 'user_2[name]': { presence: [{ message: 'must be present' }] } }
}

$('#qunit-fixture')
.html('')
.append($('<form>', {
action: '/users',
'data-client-side-validations': JSON.stringify(dataCsv),
method: 'post',
id: 'new_user_2'
}))
.find('form')
.append($('<label for="user_2_name">name</label>'))
.append($('<input />', {
name: 'user_2[name]',
id: 'user_2_name',
type: 'text'
}))
$('form#new_user_2').validate()
var form = $('form#new_user_2')
var input = form.find('input#user_2_name')

input.val('')
input.trigger('focusout')

assert.ok(input.parent().hasClass('field_with_errors'))

input.val('123')
input.trigger('change')
input.trigger('focusout')

assert.notOk(input.parent().hasClass('field_with_errors'))
})

QUnit.test('ensure label is scoped to form', function (assert) {
var form = $('form#new_user')
var input = form.find('input#user_name')
Expand Down

0 comments on commit a49dd74

Please sign in to comment.