Add test for trailing newline with `render_in` (#1207)

* Add test for trailing newline with render_in

* Update changelog

* Lint

* Add tests directly on ViewComponent::Base

* Lint

Co-authored-by: Joel Hawksley <joelhawksley@github.com>
This commit is contained in:
Simon Fish 2021-12-16 22:44:25 +00:00 коммит произвёл GitHub
Родитель 475fea68bc
Коммит aedcd86ed4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 33 добавлений и 2 удалений

Просмотреть файл

@ -7,6 +7,10 @@ title: Changelog
## main
* Add a test to ensure trailing newlines are stripped when rendering with `#render_in`.
*Simon Fish*
* Add WEBrick as a depenency to the docs application.
*Connor McQuillan*

Просмотреть файл

@ -2,4 +2,6 @@
class DisplayInlineComponentPreview < ViewComponent::Preview
def with_newline; end
def with_newline_render_in; end
end

Просмотреть файл

@ -0,0 +1 @@
<%= DisplayInlineComponent.new.render_in(controller.view_context) %><%= DisplayInlineComponent.new.render_in(controller.view_context) %>

Просмотреть файл

@ -93,14 +93,14 @@ def without_template_annotations
if ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames)
old_value = ActionView::Base.annotate_rendered_view_with_filenames
ActionView::Base.annotate_rendered_view_with_filenames = false
app.reloader.reload!
app.reloader.reload! if defined?(app)
with_new_cache do
yield
end
ActionView::Base.annotate_rendered_view_with_filenames = old_value
app.reloader.reload!
app.reloader.reload! if defined?(app)
else
yield
end

Просмотреть файл

@ -77,4 +77,17 @@ class ViewComponent::Base::UnitTest < Minitest::Test
TranslatableComponent._sidecar_files(["yml"])
)
end
def test_does_not_render_additional_newline_with_render_in
skip unless Rails::VERSION::MAJOR >= 7
without_template_annotations do
ActionView::Template::Handlers::ERB.strip_trailing_newlines = true
rendered_component = Array.new(2) {
DisplayInlineComponent.new.render_in(ActionController::Base.new.view_context)
}.join
assert_includes rendered_component, "<span>Hello, world!</span><span>Hello, world!</span>"
end
ensure
ActionView::Template::Handlers::ERB.strip_trailing_newlines = false if Rails::VERSION::MAJOR >= 7
end
end

Просмотреть файл

@ -477,6 +477,17 @@ class IntegrationTest < ActionDispatch::IntegrationTest
ActionView::Template::Handlers::ERB.strip_trailing_newlines = false if Rails::VERSION::MAJOR >= 7
end
def test_does_not_render_additional_newline_with_render_in
skip unless Rails::VERSION::MAJOR >= 7
without_template_annotations do
ActionView::Template::Handlers::ERB.strip_trailing_newlines = true
get "/rails/view_components/display_inline_component/with_newline_render_in"
assert_includes response.body, "<span>Hello, world!</span><span>Hello, world!</span>"
end
ensure
ActionView::Template::Handlers::ERB.strip_trailing_newlines = false if Rails::VERSION::MAJOR >= 7
end
def test_renders_the_preview_example_with_its_own_template_and_a_layout
get "/rails/view_components/my_component/inside_banner"
assert_includes response.body, "ViewComponent - Admin - Test"