|
| 1 | +QUnit.module('Validate Horizontal wrapper SimpleForm Bootstrap 4', { |
| 2 | + before: function () { |
| 3 | + currentFormBuilder = window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] |
| 4 | + window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = BS4_FORM_BUILDER |
| 5 | + }, |
| 6 | + |
| 7 | + after: function () { |
| 8 | + window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = currentFormBuilder |
| 9 | + }, |
| 10 | + |
| 11 | + beforeEach: function () { |
| 12 | + dataCsv = { |
| 13 | + html_settings: { |
| 14 | + type: 'SimpleForm::FormBuilder', |
| 15 | + error_class: 'is-invalid', |
| 16 | + error_tag: 'div', |
| 17 | + wrapper_error_class: 'form-group-invalid', |
| 18 | + wrapper_tag: 'div', |
| 19 | + wrapper_class: 'form-group' |
| 20 | + }, |
| 21 | + validators: { |
| 22 | + 'user[name]': { presence: [{ message: 'must be present' }], format: [{ message: 'is invalid', 'with': { options: 'g', source: '\\d+' } }] }, |
| 23 | + 'user[username]': { presence: [{ message: 'must be present' }] } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + $('#qunit-fixture') |
| 28 | + .append( |
| 29 | + $('<form>', { |
| 30 | + action: '/users', |
| 31 | + 'data-client-side-validations': JSON.stringify(dataCsv), |
| 32 | + method: 'post', |
| 33 | + id: 'new_user' |
| 34 | + }) |
| 35 | + .append( |
| 36 | + $('<div>', { 'class': 'form-group row' }) |
| 37 | + .append( |
| 38 | + $('<label for="user_name" class="string col-sm-3 col-form-label">Name</label>')) |
| 39 | + .append( |
| 40 | + $('<div>', { 'class': 'col-sm-9' }) |
| 41 | + .append( |
| 42 | + $('<input />', { 'class': 'form-control', name: 'user[name]', id: 'user_name', type: 'text' })))) |
| 43 | + // there isn't horizontal :input_group wrapper in simple_form's bootstrap 4 configuration by default |
| 44 | + // but if somebody would do it it would look like this |
| 45 | + .append( |
| 46 | + $('<div>', { 'class': 'form-group row' }) |
| 47 | + .append( |
| 48 | + $('<label for="user_username" class="string col-sm-3 col-form-label">Username</label>')) |
| 49 | + .append( |
| 50 | + $('<div>', { 'class': 'col-sm-9' }) |
| 51 | + .append( |
| 52 | + $('<div>', { 'class': 'input-group' }) |
| 53 | + .append( |
| 54 | + $('<div>', { 'class': 'input-group-prepend' }) |
| 55 | + .append( |
| 56 | + $('<span>', { 'class': 'input-group-text', text: '@' }))) |
| 57 | + .append( |
| 58 | + $('<input />', { 'class': 'form-control', name: 'user[username]', id: 'user_username', type: 'text' })))))) |
| 59 | + |
| 60 | + $('form#new_user').validate() |
| 61 | + } |
| 62 | +}) |
| 63 | + |
| 64 | +var wrappers = ['horizontal_form' ] |
| 65 | + |
| 66 | +for (var i = 0; i < wrappers.length; i++) { |
| 67 | + var wrapper = wrappers[i] |
| 68 | + |
| 69 | + QUnit.test(wrapper + ' - Validate error attaching and detaching', function (assert) { |
| 70 | + var form = $('form#new_user') |
| 71 | + var input = form.find('input#user_name') |
| 72 | + var label = $('label[for="user_name"]') |
| 73 | + form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper |
| 74 | + |
| 75 | + input.trigger('focusout') |
| 76 | + assert.ok(input.closest('.form-group').hasClass('form-group-invalid')) |
| 77 | + assert.ok(label.parent().hasClass('form-group-invalid')) |
| 78 | + assert.ok(input.parent().find('div.invalid-feedback:contains("must be present")')[0]) |
| 79 | + |
| 80 | + input.val('abc') |
| 81 | + input.trigger('change') |
| 82 | + input.trigger('focusout') |
| 83 | + assert.ok(input.closest('.form-group').hasClass('form-group-invalid')) |
| 84 | + assert.ok(input.parent().find('div.invalid-feedback:contains("is invalid")')[0]) |
| 85 | + assert.ok(input.hasClass('is-invalid')) |
| 86 | + |
| 87 | + input.val('123') |
| 88 | + input.trigger('change') |
| 89 | + input.trigger('focusout') |
| 90 | + assert.notOk(input.closest('.form-group').parent().hasClass('form-group-invalid')) |
| 91 | + assert.notOk(input.parent().parent().find('div.invalid-feedback:contains("is invalid")')[0]) |
| 92 | + assert.notOk(input.hasClass('is-invalid')) |
| 93 | + }) |
| 94 | + |
| 95 | + QUnit.test(wrapper + ' - Validate pre-existing error blocks are re-used', function (assert) { |
| 96 | + var form = $('form#new_user'); var input = form.find('input#user_name') |
| 97 | + var label = $('label[for="user_name"]') |
| 98 | + form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper |
| 99 | + |
| 100 | + input.parent().append($('<div class="invalid-feedback">Error from Server</span>')) |
| 101 | + assert.ok(input.parent().find('div.invalid-feedback:contains("Error from Server")')[0]) |
| 102 | + input.val('abc') |
| 103 | + input.trigger('change') |
| 104 | + input.trigger('focusout') |
| 105 | + assert.ok(input.closest('.form-group').hasClass('form-group-invalid')) |
| 106 | + assert.ok(label.parent().hasClass('form-group-invalid')) |
| 107 | + assert.ok(input.parent().find('div.invalid-feedback:contains("is invalid")').length === 1) |
| 108 | + assert.ok(form.find('div.invalid-feedback').length === 1) |
| 109 | + }) |
| 110 | + |
| 111 | + QUnit.test(wrapper + ' - Validate input-group', function (assert) { |
| 112 | + var form = $('form#new_user'); var input = form.find('input#user_username') |
| 113 | + form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper |
| 114 | + |
| 115 | + input.trigger('change') |
| 116 | + input.trigger('focusout') |
| 117 | + assert.ok(input.closest('.input-group-prepend').find('div.invalid-feedback').length === 0) |
| 118 | + assert.ok(input.closest('.input-group').find('div.invalid-feedback').length === 1) |
| 119 | + |
| 120 | + input.val('abc') |
| 121 | + input.trigger('change') |
| 122 | + input.trigger('focusout') |
| 123 | + assert.ok(input.closest('.input-group').find('div.invalid-feedback').length === 0) |
| 124 | + }) |
| 125 | +} |
0 commit comments