Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/react/rails/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= react_component 'GreetingMessage', { :name => 'Name' }, { :id => 'component' } do %>
<div id="unique-nested-id">NestedContent</div>
<% end %>
22 changes: 22 additions & 0 deletions test/react/rails/view_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,12 +15,31 @@ class ViewHelperTest < ActionView::TestCase
assert_equal(expected_html, rendered_html)
end

test 'view helper accepts block usage' do
expected_html = %{<div data-react-class="Component" data-react-props="{&quot;a&quot;:&quot;b&quot;}">content</div>}
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 = <<HTML
<div data-react-class=\"GreetingMessage\" data-react-props=\"{&quot;name&quot;:&quot;Name&quot;}\" id=\"component\">
<div id=\"unique-nested-id\">NestedContent</div>
</div>
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,
Expand Down