Skip to content

Add assert_react_component method for test react_component render result #957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2019
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ $ yarn add @types/react @types/react-dom

Doing this will allow React-Rails to support the .tsx extension.

### Test component

You can use `assert_react_component` to test component render:

app/views/welcome/index.html.erb

```erb
<%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
```

```rb
class WelcomeControllerTest < ActionDispatch::IntegrationTest
test 'assert_react_component' do
get "/welcome"
assert_equal 200, response.status

# assert rendered react component and check the props
assert_react_component "HelloWorld" do |props|
assert_equal "Hello from react-rails.", props[:greeting]
assert_equal "react-rails", props[:info][:name]
assert_select "[class=?]", "hello-world"
end

# or just assert component rendered
assert_react_component "HelloWorld"
end
end
```

## Use with Asset Pipeline

`react-rails` provides a pre-bundled React.js & a UJS driver to the Rails asset pipeline. Get started by installing:
Expand Down
6 changes: 6 additions & 0 deletions lib/react-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
require 'react/jsx'
require 'react/rails'
require 'react/server_rendering'

module React
module Rails
autoload :TestHelper, 'react/rails/test_helper'
end
end
1 change: 1 addition & 0 deletions lib/react/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Railtie < ::Rails::Railtie

ActiveSupport.on_load(:action_view) do
include ::React::Rails::ViewHelper
ActionDispatch::IntegrationTest.send(:include, React::Rails::TestHelper)
end
end

Expand Down
26 changes: 26 additions & 0 deletions lib/react/rails/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module React
module Rails
module TestHelper
extend ActiveSupport::Concern

# assert react_component render
#
# assert_react_component("HelloWorld") do |props|
# assert_equal "Hello world", props[:message]
# end
def assert_react_component(name)
assert_select "div[data-react-class]" do |dom|
assert_select "[data-react-class=?]", name

if block_given?
props = JSON.parse(dom.attr("data-react-props"))
props.deep_transform_keys! { |key| key.to_s.underscore }
props.deep_symbolize_keys!

yield(props)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/dummy_sprockets/app/views/pages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</ul>

<div id='component-parent'>
<%= react_component 'GreetingMessage', { name: @name }, { id: 'component', prerender: @prerender } %>
<%= react_component 'GreetingMessage', { name: @name, last_name: "Last #{@name}", info: { name: @name } }, { id: 'component', class: "greeting-message", prerender: @prerender } %>
<ul>
<%= react_component 'Todo', { todo: 'Another Component' }, { id: 'todo', prerender: @prerender } %>
</ul>
Expand Down
17 changes: 17 additions & 0 deletions test/react/rails/test_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'test_helper'

class TestHelperTest < ActionDispatch::IntegrationTest
test 'assert_react_component' do
get "/pages/1"
assert_equal 200, response.status
assert_react_component "GreetingMessage"
assert_react_component "GreetingMessage" do |props|
assert_equal "Bob", props[:name]
assert_equal "Last Bob", props[:last_name]
assert_equal "Bob", props[:info][:name]

assert_select "[id=?]", "component"
assert_select "[class=?]", "greeting-message"
end
end
end