Skip to content

Commit

Permalink
Test new edge methods textarea and checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Aug 20, 2024
1 parent 494c6e4 commit 5284b7a
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/action_view/cases/test_form_for_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ def test_text_area
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:textarea)
def test_textarea
form_for(@post, validate: true) do |f|
concat f.textarea(:cost)
end

validators = { 'post[cost]' => { presence: [{ message: I18n.t('errors.messages.blank') }] } }
expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
form_field('textarea', id: 'post_cost', name: 'post[cost]', tag_content: "\n")
end

assert_dom_equal expected, output_buffer
end
end

def test_file_field
form_for(@post, validate: true) do |f|
concat f.file_field(:cost)
Expand Down Expand Up @@ -97,6 +112,22 @@ def test_check_box
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:checkbox)
def test_checkbox
form_for(@post, validate: true) do |f|
concat f.checkbox(:cost)
end

validators = { 'post[cost]' => { presence: [{ message: I18n.t('errors.messages.blank') }] } }
expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
form_field('input', name: 'post[cost]', type: 'hidden', value: '0') +
form_field('input', id: 'post_cost', name: 'post[cost]', type: 'checkbox', value: '1')
end

assert_dom_equal expected, output_buffer
end
end

def test_check_box_ensure_no_validate_attribute
form_for(@post, validate: true) do |f|
concat f.check_box(:cost, validate: true)
Expand Down Expand Up @@ -425,6 +456,21 @@ def test_collection_check_boxes
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:collection_checkboxes)
def test_collection_checkboxes
form_for(@post, validate: true) do |f|
concat f.collection_checkboxes(:cost, [], :id, :name)
end

validators = { 'post[cost]' => { presence: [{ message: I18n.t('errors.messages.blank') }] } }
expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
form_field('input', name: 'post[cost][]', type: 'hidden', value: '')
end

assert_dom_equal expected, output_buffer
end
end

def test_collection_check_boxes_with_validate_options
form_for(@post, validate: true) do |f|
concat f.collection_check_boxes(:cost, [], :id, :name, {}, validate: false)
Expand Down
46 changes: 46 additions & 0 deletions test/action_view/cases/test_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ def test_form_with_text_area
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:textarea)
def test_form_with_textarea
form_with(model: @post, validate: true) do |f|
concat f.textarea(:cost)
end

validators = { 'post[cost]' => { presence: [{ message: I18n.t('errors.messages.blank') }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('textarea', id: 'post_cost', name: 'post[cost]', tag_content: "\n")
end

assert_dom_equal expected, output_buffer
end
end

def test_form_with_file_field
form_with(model: @post, validate: true) do |f|
concat f.file_field(:cost)
Expand Down Expand Up @@ -122,6 +137,22 @@ def test_form_with_check_box
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:checkbox)
def test_form_with_checkbox
form_with(model: @post, validate: true) do |f|
concat f.checkbox(:cost)
end

validators = { 'post[cost]' => { presence: [{ message: I18n.t('errors.messages.blank') }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', name: 'post[cost]', type: 'hidden', value: '0') +
form_field('input', id: 'post_cost', name: 'post[cost]', type: 'checkbox', value: '1')
end

assert_dom_equal expected, output_buffer
end
end

def test_form_with_check_box_ensure_no_validate_attribute
form_with(model: @post, validate: true) do |f|
concat f.check_box(:cost, validate: true)
Expand Down Expand Up @@ -440,6 +471,21 @@ def test_form_with_collection_check_boxes
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:collection_checkboxes)
def test_form_with_collection_checkboxes
form_with(model: @post, validate: true) do |f|
concat f.collection_checkboxes(:cost, [], :id, :name)
end

validators = { 'post[cost]' => { presence: [{ message: I18n.t('errors.messages.blank') }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', name: 'post[cost][]', type: 'hidden', value: '')
end

assert_dom_equal expected, output_buffer
end
end

def test_form_with_collection_check_boxes_with_validate_options
form_with(model: @post, validate: true) do |f|
concat f.collection_check_boxes(:cost, [], :id, :name, {}, validate: false)
Expand Down
29 changes: 29 additions & 0 deletions test/action_view/cases/test_legacy_form_for_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ def test_text_area
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:textarea)
def test_textarea
form_for(@post) do |f|
concat f.textarea(:cost)
end

expected = whole_form_for('/posts', 'new_post', 'new_post') do
form_field('textarea', id: 'post_cost', name: 'post[cost]', tag_content: "\n")
end

assert_dom_equal expected, output_buffer
end
end

def test_file_field
form_for(@post) do |f|
concat f.file_field(:cost)
Expand All @@ -57,6 +71,21 @@ def test_check_box
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:checkbox)
def test_checkbox
form_for(@post) do |f|
concat f.checkbox(:cost)
end

expected = whole_form_for('/posts', 'new_post', 'new_post') do
hidden_input_for_checkbox('post[cost]') +
form_field('input', id: 'post_cost', name: 'post[cost]', type: 'checkbox', value: '1')
end

assert_dom_equal expected, output_buffer
end
end

def test_radio_button
form_for(@post) do |f|
concat f.radio_button(:cost, '10')
Expand Down
29 changes: 29 additions & 0 deletions test/action_view/cases/test_legacy_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def test_form_with_text_area
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:textarea)
def test_form_with_textarea
form_with(model: @post) do |f|
concat f.textarea(:cost)
end

expected = whole_form_with('/posts') do
form_field('textarea', name: 'post[cost]', id: 'post_cost', tag_content: "\n")
end

assert_dom_equal expected, output_buffer
end
end

def test_form_with_file_field
form_with(model: @post) do |f|
concat f.file_field(:cost)
Expand All @@ -58,6 +72,21 @@ def test_form_with_check_box
assert_dom_equal expected, output_buffer
end

if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:checkbox)
def test_form_with_checkbox
form_with(model: @post) do |f|
concat f.checkbox(:cost)
end

expected = whole_form_with('/posts') do
hidden_input_for_checkbox('post[cost]') +
form_field('input', name: 'post[cost]', id: 'post_cost', type: 'checkbox', value: '1')
end

assert_dom_equal expected, output_buffer
end
end

def test_form_with_radio_button
form_with(model: @post) do |f|
concat f.radio_button(:cost, '10')
Expand Down

0 comments on commit 5284b7a

Please sign in to comment.