2017-10-23 21:01:08 -04:00
|
|
|
# frozen_string_literal: true
|
2011-09-03 12:36:02 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class WrapperTest < ActionView::TestCase
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper does not have error class for attribute without errors' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @user, :active
|
|
|
|
assert_no_select 'div.field_with_errors'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper does not have error class when object is not present' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for :project, :name
|
|
|
|
assert_no_select 'div.field_with_errors'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper adds the attribute name class' do
|
2012-10-06 23:31:57 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'div.user_name'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper adds the attribute name class for nested forms' do
|
2012-10-07 21:09:57 -04:00
|
|
|
@user.company = Company.new(1, 'Empresa')
|
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat(f.simple_fields_for(:company) do |company_form|
|
|
|
|
concat(company_form.input :name)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select 'div.user_company_name'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper adds the association name class' do
|
2012-10-06 23:31:57 -04:00
|
|
|
with_form_for @user, :company
|
|
|
|
assert_select 'div.user_company'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper adds error class for attribute with errors' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'div.field_with_errors'
|
|
|
|
end
|
|
|
|
|
2018-03-02 16:29:54 -05:00
|
|
|
test 'wrapper adds error class to input for attribute with errors' do
|
|
|
|
with_form_for @user, :name, wrapper: custom_wrapper_with_input_error_class
|
|
|
|
assert_select 'div.field_with_errors'
|
|
|
|
assert_select 'input.is-invalid'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'wrapper does not add error class to input when the attribute is valid' do
|
|
|
|
with_form_for @user, :phone_number, wrapper: custom_wrapper_with_input_error_class
|
|
|
|
assert_no_select 'div.field_with_errors'
|
|
|
|
assert_no_select 'input.is-invalid'
|
|
|
|
end
|
|
|
|
|
2018-03-11 10:27:41 -04:00
|
|
|
test 'wrapper adds valid class for present attribute without errors' do
|
|
|
|
@user.instance_eval { undef errors }
|
|
|
|
with_form_for @user, :name, wrapper: custom_wrapper_with_input_valid_class
|
|
|
|
assert_select 'div.field_without_errors'
|
2018-03-22 17:21:29 -04:00
|
|
|
assert_select 'input.is-valid'
|
2018-03-15 07:32:20 -04:00
|
|
|
assert_no_select 'div.field_with_errors'
|
2018-03-22 17:21:29 -04:00
|
|
|
assert_no_select 'input.is-invalid'
|
2018-03-11 10:27:41 -04:00
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper adds hint class for attribute with a hint' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, hint: 'hint'
|
2012-01-09 14:34:37 -05:00
|
|
|
assert_select 'div.field_with_hint'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper does not have disabled class by default' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @user, :active
|
|
|
|
assert_no_select 'div.disabled'
|
|
|
|
end
|
2011-09-04 05:52:48 -04:00
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper has disabled class when input is disabled' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :active, disabled: true
|
2011-09-11 17:46:47 -04:00
|
|
|
assert_select 'div.disabled'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper supports no wrapping when wrapper is false' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper: false
|
2011-09-04 05:52:48 -04:00
|
|
|
assert_select 'form > label[for=user_name]'
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'form > input#user_name.string'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper supports no wrapping when wrapper tag is false' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper: custom_wrapper_without_top_level
|
2011-11-09 17:02:27 -05:00
|
|
|
assert_select 'form > label[for=user_name]'
|
|
|
|
assert_select 'form > input#user_name.string'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper wraps tag adds required/optional css classes' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'form div.input.required.string'
|
|
|
|
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :age, required: false
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'form div.input.optional.integer'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper allows custom options to be given' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper_html: { id: "super_cool", class: 'yay' }
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'form #super_cool.required.string.yay'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper allows tag to be given on demand' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper_tag: :b
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'form b.required.string'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper allows wrapper class to be given on demand' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper_class: :wrapper
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'form div.wrapper.required.string'
|
|
|
|
end
|
2011-09-03 13:04:40 -04:00
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper skips additional classes when configured' do
|
2018-01-07 12:55:19 -05:00
|
|
|
swap SimpleForm, generate_additional_classes_for: %i[input label] do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper_class: :wrapper
|
2012-02-16 19:36:34 -05:00
|
|
|
assert_select 'form div.wrapper'
|
|
|
|
assert_no_select 'div.required'
|
|
|
|
assert_no_select 'div.string'
|
2012-10-06 23:31:57 -04:00
|
|
|
assert_no_select 'div.user_name'
|
2012-02-16 19:36:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'wrapper does not generate empty css class' do
|
2018-01-07 12:55:19 -05:00
|
|
|
swap SimpleForm, generate_additional_classes_for: %i[input label] do
|
2012-06-29 15:13:15 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_without_class do
|
|
|
|
with_form_for @user, :name
|
|
|
|
assert_no_select 'div#custom_wrapper_without_class[class]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-03 13:04:40 -04:00
|
|
|
# Custom wrapper test
|
|
|
|
|
|
|
|
test 'custom wrappers works' do
|
2011-09-04 05:31:24 -04:00
|
|
|
swap_wrapper do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, hint: "cool"
|
2011-09-03 13:04:40 -04:00
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper span.omg_error"
|
|
|
|
assert_select "section.custom_wrapper div.error_wrapper span.omg_error"
|
2012-02-18 09:47:02 -05:00
|
|
|
assert_select "section.custom_wrapper > div.omg_hint", "cool"
|
2011-09-03 13:04:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'custom wrappers can be turned off' do
|
2011-09-04 05:31:24 -04:00
|
|
|
swap_wrapper do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, another: false
|
2011-09-03 13:04:40 -04:00
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
assert_select "section.custom_wrapper div.error_wrapper span.omg_error"
|
|
|
|
end
|
|
|
|
end
|
2011-09-04 05:52:48 -04:00
|
|
|
|
2014-03-11 19:27:10 -04:00
|
|
|
test 'custom wrappers can have additional attributes' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_additional_attributes do
|
2014-03-11 19:27:10 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
|
|
|
|
assert_select "div.custom_wrapper[title='some title'][data-wrapper='test']"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-03 11:08:29 -04:00
|
|
|
test 'custom wrappers can have full error message on attributes' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_full_error do
|
2014-04-03 11:08:29 -04:00
|
|
|
with_form_for @user, :name
|
2014-11-25 12:52:47 -05:00
|
|
|
assert_select 'span.error', "Name cannot be blank"
|
2014-04-03 11:08:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-04 05:52:48 -04:00
|
|
|
test 'custom wrappers on a form basis' do
|
|
|
|
swap_wrapper :another do
|
2012-04-21 08:57:27 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2011-09-04 05:52:48 -04:00
|
|
|
f.input :name
|
2012-04-21 08:57:27 -04:00
|
|
|
end
|
2011-09-04 05:52:48 -04:00
|
|
|
|
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
|
2013-01-28 16:02:59 -05:00
|
|
|
with_concat_form_for(@user, wrapper: :another) do |f|
|
2011-09-04 05:52:48 -04:00
|
|
|
f.input :name
|
2012-04-21 08:57:27 -04:00
|
|
|
end
|
2011-09-04 05:52:48 -04:00
|
|
|
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'custom wrappers on input basis' do
|
|
|
|
swap_wrapper :another do
|
|
|
|
with_form_for @user, :name
|
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_no_select "section.custom_wrapper div.another_wrapper input.string"
|
2011-09-04 06:02:27 -04:00
|
|
|
output_buffer.replace ""
|
2011-09-04 05:52:48 -04:00
|
|
|
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper: :another
|
2011-09-04 05:52:48 -04:00
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
2011-09-04 06:02:27 -04:00
|
|
|
output_buffer.replace ""
|
2011-09-04 05:52:48 -04:00
|
|
|
end
|
2011-09-04 06:02:27 -04:00
|
|
|
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper: custom_wrapper
|
2011-09-04 06:02:27 -04:00
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
2011-09-04 05:52:48 -04:00
|
|
|
end
|
2011-09-26 21:47:06 -04:00
|
|
|
|
2011-09-26 22:41:53 -04:00
|
|
|
test 'access wrappers with indifferent access' do
|
|
|
|
swap_wrapper :another do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper: "another"
|
2011-09-26 22:41:53 -04:00
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'does not duplicate label classes for different inputs' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_label_html_option do
|
2012-04-21 08:51:02 -04:00
|
|
|
with_concat_form_for(@user) do |f|
|
2013-01-28 16:02:59 -05:00
|
|
|
concat f.input :name, required: false
|
|
|
|
concat f.input :email, as: :email, required: true
|
2012-04-21 08:51:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "label.string.optional.extra-label-class[for='user_name']"
|
|
|
|
assert_select "label.email.required.extra-label-class[for='user_email']"
|
|
|
|
assert_no_select "label.string.optional.extra-label-class[for='user_email']"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-26 21:47:06 -04:00
|
|
|
test 'raise error when wrapper not found' do
|
|
|
|
assert_raise SimpleForm::WrapperNotFound do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, wrapper: :not_found
|
2011-09-26 21:47:06 -04:00
|
|
|
end
|
|
|
|
end
|
2012-08-11 08:52:10 -04:00
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'uses wrapper for specified in config mapping' do
|
2012-08-11 08:52:10 -04:00
|
|
|
swap_wrapper :another do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, wrapper_mappings: { string: :another } do
|
2012-08-11 08:52:10 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-12 17:59:30 -05:00
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'uses custom wrapper mapping per form basis' do
|
2013-11-12 17:59:30 -05:00
|
|
|
swap_wrapper :another do
|
|
|
|
with_concat_form_for @user, wrapper_mappings: { string: :another } do |f|
|
|
|
|
concat f.input :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
end
|
2014-03-10 17:39:31 -04:00
|
|
|
|
2014-11-19 16:51:18 -05:00
|
|
|
test 'simple_fields_form reuses custom wrapper mapping per form basis' do
|
|
|
|
@user.company = Company.new(1, 'Empresa')
|
|
|
|
|
|
|
|
swap_wrapper :another do
|
|
|
|
with_concat_form_for @user, wrapper_mappings: { string: :another } do |f|
|
|
|
|
concat(f.simple_fields_for(:company) do |company_form|
|
|
|
|
concat(company_form.input(:name))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
|
|
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
|
|
end
|
|
|
|
|
2015-08-31 19:14:09 -04:00
|
|
|
test "input attributes class will merge with wrapper_options' classes" do
|
|
|
|
swap_wrapper :default, custom_wrapper_with_input_class do
|
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name, input_html: { class: 'another-class' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "div.custom_wrapper input.string.inline-class.another-class"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "input with data attributes will merge with wrapper_options' data" do
|
|
|
|
swap_wrapper :default, custom_wrapper_with_input_data_modal do
|
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name, input_html: { data: { modal: 'another-data', target: 'merge-data' } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "input[data-wrapper='data-wrapper'][data-modal='another-data'][data-target='merge-data']"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "input with aria attributes will merge with wrapper_options' aria" do
|
|
|
|
swap_wrapper :default, custom_wrapper_with_input_aria_modal do
|
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name, input_html: { aria: { modal: 'another-aria', target: 'merge-aria' } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "input[aria-wrapper='aria-wrapper'][aria-modal='another-aria'][aria-target='merge-aria']"
|
|
|
|
end
|
|
|
|
|
2014-03-10 17:39:31 -04:00
|
|
|
test 'input accepts attributes in the DSL' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_input_class do
|
2014-03-10 17:39:31 -04:00
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "div.custom_wrapper input.string.inline-class"
|
|
|
|
end
|
2014-03-11 10:22:20 -04:00
|
|
|
|
|
|
|
test 'label accepts attributes in the DSL' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_label_class do
|
2014-03-11 10:22:20 -04:00
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "div.custom_wrapper label.string.inline-class"
|
|
|
|
end
|
2014-03-11 10:31:48 -04:00
|
|
|
|
|
|
|
test 'label_input accepts attributes in the DSL' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_label_input_class do
|
2014-03-11 10:31:48 -04:00
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "div.custom_wrapper label.string.inline-class"
|
|
|
|
assert_select "div.custom_wrapper input.string.inline-class"
|
|
|
|
end
|
2014-03-11 10:39:41 -04:00
|
|
|
|
|
|
|
test 'input accepts data attributes in the DSL' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_input_attributes do
|
2014-03-11 10:39:41 -04:00
|
|
|
with_concat_form_for @user do |f|
|
|
|
|
concat f.input :name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select "div.custom_wrapper input.string[data-modal=true]"
|
|
|
|
end
|
2014-06-23 15:10:00 -04:00
|
|
|
|
|
|
|
test 'inline wrapper displays when there is content' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_wrapped_optional_component do
|
2014-11-17 14:27:13 -05:00
|
|
|
with_form_for @user, :name, hint: "cannot be blank"
|
|
|
|
assert_select 'section.custom_wrapper div.no_output_wrapper p.omg_hint', "cannot be blank"
|
2014-06-23 15:10:00 -04:00
|
|
|
assert_select 'p.omg_hint'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'inline wrapper does not display when there is no content' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_wrapped_optional_component do
|
2014-06-23 15:10:00 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'section.custom_wrapper div.no_output_wrapper'
|
|
|
|
assert_no_select 'p.omg_hint'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'optional wrapper does not display when there is content' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_unless_blank do
|
2014-06-23 15:10:00 -04:00
|
|
|
with_form_for @user, :name, hint: "can't be blank"
|
|
|
|
assert_select 'section.custom_wrapper div.no_output_wrapper'
|
|
|
|
assert_select 'div.no_output_wrapper'
|
|
|
|
assert_select 'p.omg_hint'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'optional wrapper does not display when there is no content' do
|
2015-03-25 07:17:42 -04:00
|
|
|
swap_wrapper :default, custom_wrapper_with_unless_blank do
|
2014-06-23 15:10:00 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_no_select 'section.custom_wrapper div.no_output_wrapper'
|
|
|
|
assert_no_select 'div.no_output_wrapper'
|
|
|
|
assert_no_select 'p.omg_hint'
|
|
|
|
end
|
|
|
|
end
|
2011-09-03 12:36:02 -04:00
|
|
|
end
|