Skip to content

Commit

Permalink
Add specs for f.fields
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbait authored and tagliala committed Apr 16, 2021
1 parent 7e86f9f commit 2d869e4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
109 changes: 109 additions & 0 deletions test/action_view/cases/test_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,103 @@ def test_form_with_nested_fields_for_dont_overwrite_validation_with_inheritance
assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_inherit_validation_settings
form_with(model: @post, validate: true) do |f|
concat f.fields(:comment, @comment) { |c|
concat c.text_field(:title)
}
end

validators = { 'post[comment][title]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', id: 'post_comment_title', name: 'post[comment][title]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_inherit_validation_settings_when_record_object_is_a_hash
record_object = { defaults: nil }
post_with_category = Post.new
post_with_category.category = Category.new

form_with(model: post_with_category, validate: true) do |f|
concat f.fields(:category, record_object) { |c|
concat c.text_field(:title)
}
end

validators = { 'post[category_attributes][title]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', id: 'post_category_attributes_title', name: 'post[category_attributes][title]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_multiple_nested_fields_inherit_validation_settings
form_with(model: @post, validate: true) do |f|
concat f.fields(:comment, @comment) { |c|
concat c.text_field(:title)
}
concat f.fields(:comment, @comment) { |c|
concat c.text_field(:body)
}
end

validators = { 'post[comment][title]' => { presence: [{ message: "can't be blank" }] }, 'post[comment][body]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', id: 'post_comment_title', name: 'post[comment][title]', type: 'text') +
form_field('input', id: 'post_comment_body', name: 'post[comment][body]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_with_nested_attributes
form_with(model: @post, validate: true) do |f|
concat f.fields(:comments, [@comment]) { |c|
concat c.text_field(:title)
}
end

validators = { 'post[comments_attributes][][title]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', id: 'post_comments_attributes_0_title', name: 'post[comments_attributes][0][title]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_with_nested_attributes_with_child_index
form_with(model: @post, validate: true) do |f|
concat f.fields(:comments, [Comment.new], child_index: '__INDEX__') { |c|
concat c.text_field(:title)
}
end

validators = { 'post[comments_attributes][][title]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', id: 'post_comments_attributes___INDEX___title', name: 'post[comments_attributes][__INDEX__][title]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_dont_overwrite_validation_with_inheritance
form_with(model: @post, validate: true) do |f|
concat f.fields(:comment, @comment, validate: false) { |c|
concat c.text_field(:title)
}
end

expected = whole_form_with('/posts', validators: {}) do
form_field('input', id: 'post_comment_title', name: 'post[comment][title]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_with_custom_id_for_form
form_with(model: @post, validate: true, html: { id: 'some_form' }) do |f|
concat f.text_field(:cost)
Expand Down Expand Up @@ -540,6 +637,18 @@ def test_form_with_added_validators_defaulting_to_all
assert_dom_equal expected, output_buffer
end

def test_form_with_fields_added_validators_defaulting_to_all
form_with(model: @post, validate: true) do |f|
concat f.fields(:comment, @comment) { |c|
concat c.validate
}
end

validators = { 'post[comment][title]' => { presence: [{ message: "can't be blank" }] }, 'post[comment][body]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators)
assert_dom_equal expected, output_buffer
end

def test_form_with_added_validators_with_filters
form_with(model: @post, validate: true) do |f|
concat f.validate(:cost, :body, :title, length: false)
Expand Down
10 changes: 10 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 @@ -74,6 +74,16 @@ def test_fields_for
assert_dom_equal expected, result
end

def test_fields
result = fields(:comment) do |c|
c.text_field(:title)
end

expected = form_field('input', id: 'comment_title', name: 'comment[title]', type: 'text')

assert_dom_equal expected, result
end

def test_select
form_for(@post) do |f|
concat f.select(:cost, [])
Expand Down
10 changes: 10 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 @@ -75,6 +75,16 @@ def test_form_with_fields_for
assert_dom_equal expected, result
end

def test_form_with_fields
result = fields(:comment) do |c|
c.text_field(:title)
end

expected = form_field('input', id: 'comment_title', name: 'comment[title]', type: 'text')

assert_dom_equal expected, result
end

def test_form_with_select
form_with(model: @post) do |f|
concat f.select(:cost, [])
Expand Down

0 comments on commit 2d869e4

Please sign in to comment.