Skip to content

Commit

Permalink
Added tests for :horizontal_form wrapper and made it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalRemis committed Apr 15, 2020
1 parent 923dfc6 commit 0cabb37
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 12 deletions.
7 changes: 4 additions & 3 deletions dist/simple-form.bootstrap4.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = {
wrappers: {
"default": {
add: function add(element, settings, message) {
var wrapperElement = element.parent();
var parentElement = element.parent();
var wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'));
var errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback');

if (!errorElement.length) {
errorElement = $('<' + settings.error_tag + '>', {
"class": 'invalid-feedback',
text: message
});
wrapperElement.append(errorElement);
parentElement.append(errorElement);
}

wrapperElement.addClass(settings.wrapper_error_class);
element.addClass('is-invalid');
errorElement.text(message);
},
remove: function remove(element, settings) {
var wrapperElement = element.parent();
var wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'));
var errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback');
wrapperElement.removeClass(settings.wrapper_error_class);
element.removeClass('is-invalid');
Expand Down
7 changes: 4 additions & 3 deletions dist/simple-form.bootstrap4.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@
wrappers: {
"default": {
add: function add(element, settings, message) {
var wrapperElement = element.parent();
var parentElement = element.parent();
var wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'));
var errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback');

if (!errorElement.length) {
errorElement = $('<' + settings.error_tag + '>', {
"class": 'invalid-feedback',
text: message
});
wrapperElement.append(errorElement);
parentElement.append(errorElement);
}

wrapperElement.addClass(settings.wrapper_error_class);
element.addClass('is-invalid');
errorElement.text(message);
},
remove: function remove(element, settings) {
var wrapperElement = element.parent();
var wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'));
var errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback');
wrapperElement.removeClass(settings.wrapper_error_class);
element.removeClass('is-invalid');
Expand Down
8 changes: 5 additions & 3 deletions src/main.bootstrap4.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = {
wrappers: {
default: {
add (element, settings, message) {
const wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/\ /g, '.'));
const parentElement = element.parent()
const wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'))

let errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback')

if (!errorElement.length) {
errorElement = $('<' + settings.error_tag + '>', { class: 'invalid-feedback', text: message })
wrapperElement.append(errorElement)
parentElement.append(errorElement)
}

wrapperElement.addClass(settings.wrapper_error_class)
Expand All @@ -29,7 +31,7 @@ ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = {
},

remove (element, settings) {
const wrapperElement = settings.wrapper_tag + "." + settings.wrapper_class.replace(/\ /g, ".")
const wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'))
const errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback')

wrapperElement.removeClass(settings.wrapper_error_class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
QUnit.module('Validate Horizontal wrapper SimpleForm Bootstrap 4', {
before: function () {
currentFormBuilder = window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder']
window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = BS4_FORM_BUILDER
},

after: function () {
window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = currentFormBuilder
},

beforeEach: function () {
dataCsv = {
html_settings: {
type: 'SimpleForm::FormBuilder',
error_class: 'is-invalid',
error_tag: 'div',
wrapper_error_class: 'form-group-invalid',
wrapper_tag: 'div',
wrapper_class: 'form-group'
},
validators: {
'user[name]': { presence: [{ message: 'must be present' }], format: [{ message: 'is invalid', 'with': { options: 'g', source: '\\d+' } }] },
'user[username]': { presence: [{ message: 'must be present' }] }
}
}

$('#qunit-fixture')
.append(
$('<form>', {
action: '/users',
'data-client-side-validations': JSON.stringify(dataCsv),
method: 'post',
id: 'new_user'
})
.append(
$('<div>', { 'class': 'form-group row' })
.append(
$('<label for="user_name" class="string col-sm-3 col-form-label">Name</label>'))
.append(
$('<div>', { 'class': 'col-sm-9' })
.append(
$('<input />', { 'class': 'form-control', name: 'user[name]', id: 'user_name', type: 'text' }))))
// there isn't horizontal :input_group wrapper in simple_form's bootstrap 4 configuration by default
// but if somebody would do it it would look like this
.append(
$('<div>', { 'class': 'form-group row' })
.append(
$('<label for="user_username" class="string col-sm-3 col-form-label">Username</label>'))
.append(
$('<div>', { 'class': 'col-sm-9' })
.append(
$('<div>', { 'class': 'input-group' })
.append(
$('<div>', { 'class': 'input-group-prepend' })
.append(
$('<span>', { 'class': 'input-group-text', text: '@' })))
.append(
$('<input />', { 'class': 'form-control', name: 'user[username]', id: 'user_username', type: 'text' }))))))

$('form#new_user').validate()
}
})

var wrappers = ['horizontal_form' ]

for (var i = 0; i < wrappers.length; i++) {
var wrapper = wrappers[i]

QUnit.test(wrapper + ' - Validate error attaching and detaching', function (assert) {
var form = $('form#new_user')
var input = form.find('input#user_name')
var label = $('label[for="user_name"]')
form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper

input.trigger('focusout')
assert.ok(input.closest('.form-group').hasClass('form-group-invalid'))
assert.ok(label.parent().hasClass('form-group-invalid'))
assert.ok(input.parent().find('div.invalid-feedback:contains("must be present")')[0])

input.val('abc')
input.trigger('change')
input.trigger('focusout')
assert.ok(input.closest('.form-group').hasClass('form-group-invalid'))
assert.ok(input.parent().find('div.invalid-feedback:contains("is invalid")')[0])
assert.ok(input.hasClass('is-invalid'))

input.val('123')
input.trigger('change')
input.trigger('focusout')
assert.notOk(input.closest('.form-group').parent().hasClass('form-group-invalid'))
assert.notOk(input.parent().parent().find('div.invalid-feedback:contains("is invalid")')[0])
assert.notOk(input.hasClass('is-invalid'))
})

QUnit.test(wrapper + ' - Validate pre-existing error blocks are re-used', function (assert) {
var form = $('form#new_user'); var input = form.find('input#user_name')
var label = $('label[for="user_name"]')
form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper

input.parent().append($('<div class="invalid-feedback">Error from Server</span>'))
assert.ok(input.parent().find('div.invalid-feedback:contains("Error from Server")')[0])
input.val('abc')
input.trigger('change')
input.trigger('focusout')
assert.ok(input.closest('.form-group').hasClass('form-group-invalid'))
assert.ok(label.parent().hasClass('form-group-invalid'))
assert.ok(input.parent().find('div.invalid-feedback:contains("is invalid")').length === 1)
assert.ok(form.find('div.invalid-feedback').length === 1)
})

QUnit.test(wrapper + ' - Validate input-group', function (assert) {
var form = $('form#new_user'); var input = form.find('input#user_username')
form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper

input.trigger('change')
input.trigger('focusout')
assert.ok(input.closest('.input-group-prepend').find('div.invalid-feedback').length === 0)
assert.ok(input.closest('.input-group').find('div.invalid-feedback').length === 1)

input.val('abc')
input.trigger('change')
input.trigger('focusout')
assert.ok(input.closest('.input-group').find('div.invalid-feedback').length === 0)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@
wrappers: {
"default": {
add: function add(element, settings, message) {
var wrapperElement = element.parent();
var parentElement = element.parent();
var wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'));
var errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback');

if (!errorElement.length) {
errorElement = $('<' + settings.error_tag + '>', {
"class": 'invalid-feedback',
text: message
});
wrapperElement.append(errorElement);
parentElement.append(errorElement);
}

wrapperElement.addClass(settings.wrapper_error_class);
element.addClass('is-invalid');
errorElement.text(message);
},
remove: function remove(element, settings) {
var wrapperElement = element.parent();
var wrapperElement = element.closest(settings.wrapper_tag + '.' + settings.wrapper_class.replace(/ /g, '.'));
var errorElement = wrapperElement.find(settings.error_tag + '.invalid-feedback');
wrapperElement.removeClass(settings.wrapper_error_class);
element.removeClass('is-invalid');
Expand Down

0 comments on commit 0cabb37

Please sign in to comment.