From 260d6f112a0ffdbe03e6f5051504cb441c1e94cd Mon Sep 17 00:00:00 2001 From: npezza93 Date: Tue, 13 Jun 2017 10:54:35 -0400 Subject: Change `form_with` to generates ids by default When `form_with` was introduced we disabled the automatic generation of ids that was enabled in `form_for`. This usually is not an good idea since labels don't work when the input doesn't have an id and it made harder to test with Capybara. You can still disable the automatic generation of ids setting `config.action_view.form_with_generates_ids` to `false.` --- .../test/template/form_helper/form_with_test.rb | 408 +++++++++++---------- 1 file changed, 218 insertions(+), 190 deletions(-) (limited to 'actionview/test') diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb index c7d49070ce..ad23d31d41 100644 --- a/actionview/test/template/form_helper/form_with_test.rb +++ b/actionview/test/template/form_helper/form_with_test.rb @@ -314,17 +314,45 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts/123", "create-post", method: "patch") do "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" + end + + assert_dom_equal expected, output_buffer + end + + def test_form_with_not_outputting_ids + old_value = ActionView::Helpers::FormHelper.form_with_generates_ids + ActionView::Helpers::FormHelper.form_with_generates_ids = false + + form_with(model: @post, id: "create-post") do |f| + concat f.label(:title) { "The Title" } + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + concat f.select(:category, %w( animal economy sports )) + concat f.submit("Create post") + end + + expected = whole_form("/posts/123", "create-post", method: "patch") do + "" \ "" \ "" \ "" \ "" \ "" \ - "" \ - "" \ - "" + "" end assert_dom_equal expected, output_buffer + ensure + ActionView::Helpers::FormHelper.form_with_generates_ids = old_value end def test_form_with_only_url_on_create @@ -335,7 +363,7 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts") do '' \ - '' + '' end assert_dom_equal expected, output_buffer @@ -349,7 +377,7 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts/123") do '' \ - '' + '' end assert_dom_equal expected, output_buffer @@ -361,7 +389,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123") do - '' + '' end assert_dom_equal expected, output_buffer @@ -373,7 +401,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: :patch) do - '' + '' end assert_dom_equal expected, output_buffer @@ -391,8 +419,8 @@ class FormWithActsLikeFormForTest < FormWithTest end.new form_with(model: obj, scope: "other_name", url: "/", id: "edit-other-name") do |f| - assert_dom_equal '', f.hidden_field(:private_property) - assert_dom_equal '', f.hidden_field(:protected_property) + assert_dom_equal '', f.hidden_field(:private_property) + assert_dom_equal '', f.hidden_field(:protected_property) end end @@ -459,7 +487,7 @@ class FormWithActsLikeFormForTest < FormWithTest "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -557,7 +585,7 @@ class FormWithActsLikeFormForTest < FormWithTest "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -587,7 +615,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", "create-post", method: "patch", multipart: true) do - "" + "" end assert_dom_equal expected, output_buffer @@ -601,7 +629,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch", multipart: true) do - "" + "" end assert_dom_equal expected, output_buffer @@ -640,7 +668,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/44", method: "patch") do - "" \ + "" \ "" end @@ -658,10 +686,10 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts/123", "create-post", method: "patch") do "" \ - "" \ - "" \ + "" \ + "" \ "" \ - "" \ + "" \ "" end @@ -676,10 +704,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", "create-post", method: "delete") do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -693,10 +721,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", "create-post", method: "delete") do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -710,7 +738,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/search", "search-post", method: "get") do - "" + "" end assert_dom_equal expected, output_buffer @@ -724,10 +752,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", "create-post", method: "patch") do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -744,10 +772,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", "create-post", method: "patch", local: true) do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -761,7 +789,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", skip_enforcing_utf8: true) do - "" + "" end assert_dom_equal expected, output_buffer @@ -773,7 +801,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", skip_enforcing_utf8: false) do - "" + "" end assert_dom_equal expected, output_buffer @@ -787,10 +815,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/", "create-post") do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -806,10 +834,10 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts/123", method: "patch") do "" \ - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -823,10 +851,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -841,7 +869,7 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts/123", method: "patch") do "
" \ - "
" \ + "
" \ "" end @@ -859,7 +887,7 @@ class FormWithActsLikeFormForTest < FormWithTest expected = whole_form("/posts/123", method: "patch") do "
" \ - "
" \ + "
" \ "" end @@ -947,7 +975,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: :patch) do - '' + '' end assert_dom_equal expected, output_buffer @@ -967,7 +995,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form do - '' + '' end assert_dom_equal expected, output_buffer @@ -982,7 +1010,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1002,7 +1030,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form do - "" + "" end assert_dom_equal expected, output_buffer @@ -1017,8 +1045,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" \ - "" + "" \ + "" end assert_dom_equal expected, output_buffer @@ -1033,8 +1061,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" \ - "" + "" \ + "" end assert_dom_equal expected, output_buffer @@ -1048,7 +1076,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1062,7 +1090,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1076,7 +1104,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1090,7 +1118,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1104,7 +1132,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1124,9 +1152,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end + whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1143,8 +1171,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1170,9 +1198,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1189,9 +1217,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1208,8 +1236,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1226,8 +1254,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1244,9 +1272,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1264,9 +1292,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1285,11 +1313,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1312,11 +1340,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1339,10 +1367,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1365,11 +1393,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1388,11 +1416,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1412,11 +1440,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1435,9 +1463,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1456,10 +1484,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1474,7 +1502,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' + '' end assert_dom_equal expected, output_buffer @@ -1491,11 +1519,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1512,11 +1540,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1547,11 +1575,11 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1570,10 +1598,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1590,8 +1618,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1607,8 +1635,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1630,8 +1658,8 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1716,18 +1744,18 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' \ - '' \ - '' \ - '' \ - '' \ - '' \ - '' \ - '' \ - '' \ - '' \ - '' \ - '' + '' \ + '' \ + '' \ + '' \ + '' \ + '' \ + '' \ + '' \ + '' \ + '' \ + '' \ + '' end assert_dom_equal expected, output_buffer @@ -1743,7 +1771,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - '' + '' end assert_dom_equal expected, output_buffer @@ -1757,10 +1785,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" assert_dom_equal expected, output_buffer end @@ -1773,10 +1801,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" assert_dom_equal expected, output_buffer end @@ -1789,10 +1817,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" assert_dom_equal expected, output_buffer end @@ -1805,10 +1833,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" assert_dom_equal expected, output_buffer end @@ -1821,10 +1849,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" assert_dom_equal expected, output_buffer end @@ -1837,10 +1865,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" assert_dom_equal expected, output_buffer end @@ -1852,7 +1880,7 @@ class FormWithActsLikeFormForTest < FormWithTest end assert_dom_equal "" \ - "", + "", output_buffer end @@ -1863,7 +1891,7 @@ class FormWithActsLikeFormForTest < FormWithTest end assert_dom_equal "" \ - "", + "", output_buffer end @@ -1882,10 +1910,10 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", "create-post", method: "patch") do - "" \ - "" \ + "" \ + "" \ "" \ - "" + "" end assert_dom_equal expected, output_buffer @@ -1902,9 +1930,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", "create-post", method: "patch") do - "" \ - "" \ - "" + "" \ + "" \ + "" end assert_dom_equal expected, output_buffer @@ -1918,7 +1946,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "" + "" end assert_dom_equal expected, output_buffer @@ -1942,9 +1970,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "
" \ - "
" \ - "
" + "
" \ + "
" \ + "
" end assert_dom_equal expected, output_buffer @@ -1961,9 +1989,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "
" \ - "
" \ - "
" + "
" \ + "
" \ + "
" end assert_dom_equal expected, output_buffer @@ -1980,7 +2008,7 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = whole_form("/posts/123", method: "patch") do - "
" + "
" end assert_dom_equal expected, output_buffer @@ -1995,7 +2023,7 @@ class FormWithActsLikeFormForTest < FormWithTest concat f.text_field(:title) end - expected = "
" + expected = "
" assert_dom_equal expected, output_buffer end @@ -2007,7 +2035,7 @@ class FormWithActsLikeFormForTest < FormWithTest concat f.text_field(:title) end - expected = "
" + expected = "
" assert_dom_equal expected, output_buffer end @@ -2020,9 +2048,9 @@ class FormWithActsLikeFormForTest < FormWithTest end expected = - "
" \ - "
" \ - "
" + "
" \ + "
" \ + "
" assert_dom_equal expected, output_buffer end -- cgit v1.2.3 From d82b7061843994897db9339a301b01c51d37f9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sat, 25 Nov 2017 15:17:31 -0500 Subject: Add test to make sure form_for is not affected by form_with_generates_ids --- actionview/test/template/form_helper_test.rb | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'actionview/test') diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index ac64096908..4442465047 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -1560,6 +1560,38 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_is_not_affected_by_form_with_generates_ids + old_value = ActionView::Helpers::FormHelper.form_with_generates_ids + ActionView::Helpers::FormHelper.form_with_generates_ids = false + + form_for(@post, html: { id: "create-post" }) do |f| + concat f.label(:title) { "The Title" } + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + concat f.submit("Create post") + concat f.button("Create post") + concat f.button { + concat content_tag(:span, "Create post") + } + end + + expected = whole_form("/posts/123", "create-post", "edit_post", method: "patch") do + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" + end + + assert_dom_equal expected, output_buffer + ensure + ActionView::Helpers::FormHelper.form_with_generates_ids = old_value + end + def test_form_for_with_collection_radio_buttons post = Post.new def post.active; false; end -- cgit v1.2.3 From 7ce8a6af1b8fdbc9ee553bda3fd725bea169c2f3 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 27 Nov 2017 15:16:12 +0900 Subject: Enable `Style/DefWithParentheses` rubocop rule The def with blank `()` was newly added in #31176, but we have not used the blank `()` style in most part of our code base. So I've enabled `Style/DefWithParentheses` to prevent to newly added the code. --- actionview/test/template/form_helper/form_with_test.rb | 2 +- actionview/test/template/form_helper_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'actionview/test') diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb index ad23d31d41..4e268eaa16 100644 --- a/actionview/test/template/form_helper/form_with_test.rb +++ b/actionview/test/template/form_helper/form_with_test.rb @@ -218,7 +218,7 @@ class FormWithActsLikeFormForTest < FormWithTest @post = Post.new @comment = Comment.new - def @post.errors() + def @post.errors Class.new { def [](field); field == "author_name" ? ["can't be empty"] : [] end def empty?() false end diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 4442465047..e7b249cc9c 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -105,7 +105,7 @@ class FormHelperTest < ActionView::TestCase @post = Post.new @comment = Comment.new - def @post.errors() + def @post.errors Class.new { def [](field); field == "author_name" ? ["can't be empty"] : [] end def empty?() false end -- cgit v1.2.3 From 21cd5b3031b0c022439a88cb750c1e00cd07f1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 27 Nov 2017 11:52:39 -0500 Subject: Make form_with_generates_ids default value to be false This will keep the behavior of an application with the defaults of a 4.2 or 5.0 application behaving the same when upgrading to 5.2. --- actionview/test/template/form_helper/form_with_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'actionview/test') diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb index 4e268eaa16..0295ff627d 100644 --- a/actionview/test/template/form_helper/form_with_test.rb +++ b/actionview/test/template/form_helper/form_with_test.rb @@ -5,6 +5,15 @@ require "controller/fake_models" class FormWithTest < ActionView::TestCase include RenderERBUtils + + setup do + @old_value = ActionView::Helpers::FormHelper.form_with_generates_ids + ActionView::Helpers::FormHelper.form_with_generates_ids = true + end + + teardown do + ActionView::Helpers::FormHelper.form_with_generates_ids = @old_value + end end class FormWithActsLikeFormTagTest < FormWithTest -- cgit v1.2.3 From 61214632f4a5d21ef86803740b6e30627e96c475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 28 Nov 2017 14:56:47 -0500 Subject: Make sure the request exist befoe trying to send early hints Action Mailer context for example responds to request but don't have a a request object. Fixes #31265 --- actionview/test/template/asset_tag_helper_test.rb | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'actionview/test') diff --git a/actionview/test/template/asset_tag_helper_test.rb b/actionview/test/template/asset_tag_helper_test.rb index 7475f5cc3c..a8b66191bc 100644 --- a/actionview/test/template/asset_tag_helper_test.rb +++ b/actionview/test/template/asset_tag_helper_test.rb @@ -440,6 +440,14 @@ class AssetTagHelperTest < ActionView::TestCase } end + def test_stylesheet_link_tag_without_request + @request = nil + assert_dom_equal( + %(), + stylesheet_link_tag("foo.css") + ) + end + def test_stylesheet_link_tag_is_html_safe assert stylesheet_link_tag("dir/file").html_safe? assert stylesheet_link_tag("dir/other/file", "dir/file2").html_safe? @@ -464,6 +472,11 @@ class AssetTagHelperTest < ActionView::TestCase assert_dom_equal %(), stylesheet_link_tag("wellington") end + def test_javascript_include_tag_without_request + @request = nil + assert_dom_equal %(), javascript_include_tag("foo.js") + end + def test_image_path ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end @@ -778,6 +791,23 @@ class AssetTagHelperNonVhostTest < ActionView::TestCase end end +class AssetTagHelperWithoutRequestTest < ActionView::TestCase + tests ActionView::Helpers::AssetTagHelper + + undef :request + + def test_stylesheet_link_tag_without_request + assert_dom_equal( + %(), + stylesheet_link_tag("foo.css") + ) + end + + def test_javascript_include_tag_without_request + assert_dom_equal %(), javascript_include_tag("foo.js") + end +end + class AssetUrlHelperControllerTest < ActionView::TestCase tests ActionView::Helpers::AssetUrlHelper -- cgit v1.2.3 From eb90b8bc86e758045a707cae43d11dab538ca6db Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Tue, 28 Nov 2017 00:58:55 -0500 Subject: Add preload_link_tag helper. This helper creates a link tag with preload keyword that allows to browser to initiate early fetch of resources. Additionally this send Early Hints if supported. See https://github.com/rails/rails/pull/30744/commits/59a02fb7bcbe68f26e1e7fdcec45c00c66e4a065 for more details about Early Hints. Preload spec: https://w3c.github.io/preload/ --- actionview/test/template/asset_tag_helper_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'actionview/test') diff --git a/actionview/test/template/asset_tag_helper_test.rb b/actionview/test/template/asset_tag_helper_test.rb index a8b66191bc..284dacf2d4 100644 --- a/actionview/test/template/asset_tag_helper_test.rb +++ b/actionview/test/template/asset_tag_helper_test.rb @@ -214,6 +214,17 @@ class AssetTagHelperTest < ActionView::TestCase %(favicon_link_tag 'mb-icon.png', :rel => 'apple-touch-icon', :type => 'image/png') => %() } + PreloadLinkToTag = { + %(preload_link_tag '/styles/custom_theme.css') => %(), + %(preload_link_tag '/videos/video.webm') => %(), + %(preload_link_tag '/posts.json', as: 'fetch') => %(), + %(preload_link_tag '/users', as: 'fetch', type: 'application/json') => %(), + %(preload_link_tag '//example.com/map?callback=initMap', as: 'fetch', type: 'application/javascript') => %(), + %(preload_link_tag '//example.com/font.woff2') => %(), + %(preload_link_tag '//example.com/font.woff2', crossorigin: 'use-credentials') => %(), + %(preload_link_tag '/media/audio.ogg', nopush: true) => %() + } + VideoPathToTag = { %(video_path("xml")) => %(/videos/xml), %(video_path("xml.ogg")) => %(/videos/xml.ogg), @@ -535,6 +546,10 @@ class AssetTagHelperTest < ActionView::TestCase FaviconLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end + def test_preload_link_tag + PreloadLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } + end + def test_video_path VideoPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end -- cgit v1.2.3