diff --git a/lib/react/rails/view_helper.rb b/lib/react/rails/view_helper.rb index fecb21fbf..89bd6f238 100644 --- a/lib/react/rails/view_helper.rb +++ b/lib/react/rails/view_helper.rb @@ -18,7 +18,7 @@ module ViewHelper # Otherwise, make a new instance. def react_component(*args, &block) helper_obj = @__react_component_helper ||= helper_implementation_class.new - helper_obj.react_component(*args, &block) + helper_obj.react_component(*args) { capture &block if block_given? } end end end diff --git a/test/dummy/app/views/pages/_component_with_inner_html.html.erb b/test/dummy/app/views/pages/_component_with_inner_html.html.erb new file mode 100644 index 000000000..142e5d78f --- /dev/null +++ b/test/dummy/app/views/pages/_component_with_inner_html.html.erb @@ -0,0 +1,3 @@ +<%= react_component 'GreetingMessage', { :name => 'Name' }, { :id => 'component' } do %> +
NestedContent
+<% end %> diff --git a/test/react/rails/view_helper_test.rb b/test/react/rails/view_helper_test.rb index d5b8391c5..e8ff78e7a 100644 --- a/test/react/rails/view_helper_test.rb +++ b/test/react/rails/view_helper_test.rb @@ -2,7 +2,10 @@ # Provide direct access to the view helper methods class ViewHelperHelper + extend ActionView::Context + extend ActionView::Helpers::CaptureHelper extend React::Rails::ViewHelper + end class ViewHelperTest < ActionView::TestCase @@ -12,12 +15,31 @@ class ViewHelperTest < ActionView::TestCase assert_equal(expected_html, rendered_html) end + test 'view helper accepts block usage' do + expected_html = %{
content
} + rendered_html = ViewHelperHelper.react_component("Component", {a: "b"}) do + "content" + end + assert_equal(expected_html, rendered_html) + end + test "view helper can be used in stand-alone views" do @name = "React-Rails" render template: "pages/show" assert_includes(rendered, "React-Rails") end + test "view helper can accept block and render inner content only once" do + rendered_html = render partial: "pages/component_with_inner_html" + expected_html = < +
NestedContent
+ +HTML + assert_equal expected_html.strip, rendered_html + end + + test "view helper uses the implementation class set in the initializer" do assert_equal( React::Rails::ViewHelper.helper_implementation_class.to_s,