Skip to content

Commit a3489eb

Browse files
authored
Merge pull request #957 from huacnlee/add-assert-method-for-test
Add assert_react_component method for test react_component render result
2 parents 18dcf3e + 71ced0c commit a3489eb

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ $ yarn add @types/react @types/react-dom
159159

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

162+
### Test component
163+
164+
You can use `assert_react_component` to test component render:
165+
166+
app/views/welcome/index.html.erb
167+
168+
```erb
169+
<%= react_component("HelloWorld", { greeting: "Hello from react-rails.", info: { name: "react-rails" } }, { class: "hello-world" }) %>
170+
```
171+
172+
```rb
173+
class WelcomeControllerTest < ActionDispatch::IntegrationTest
174+
test 'assert_react_component' do
175+
get "/welcome"
176+
assert_equal 200, response.status
177+
178+
# assert rendered react component and check the props
179+
assert_react_component "HelloWorld" do |props|
180+
assert_equal "Hello from react-rails.", props[:greeting]
181+
assert_equal "react-rails", props[:info][:name]
182+
assert_select "[class=?]", "hello-world"
183+
end
184+
185+
# or just assert component rendered
186+
assert_react_component "HelloWorld"
187+
end
188+
end
189+
```
190+
162191
## Use with Asset Pipeline
163192

164193
`react-rails` provides a pre-bundled React.js & a UJS driver to the Rails asset pipeline. Get started by adding the `react-rails` gem:

lib/react-rails.rb

+6
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22
require 'react/jsx'
33
require 'react/rails'
44
require 'react/server_rendering'
5+
6+
module React
7+
module Rails
8+
autoload :TestHelper, 'react/rails/test_helper'
9+
end
10+
end

lib/react/rails/railtie.rb

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Railtie < ::Rails::Railtie
5858

5959
ActiveSupport.on_load(:action_view) do
6060
include ::React::Rails::ViewHelper
61+
ActionDispatch::IntegrationTest.send(:include, React::Rails::TestHelper)
6162
end
6263
end
6364

lib/react/rails/test_helper.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module React
2+
module Rails
3+
module TestHelper
4+
extend ActiveSupport::Concern
5+
6+
# assert react_component render
7+
#
8+
# assert_react_component("HelloWorld") do |props|
9+
# assert_equal "Hello world", props[:message]
10+
# end
11+
def assert_react_component(name)
12+
assert_select "div[data-react-class]" do |dom|
13+
assert_select "[data-react-class=?]", name
14+
15+
if block_given?
16+
props = JSON.parse(dom.attr("data-react-props"))
17+
props.deep_transform_keys! { |key| key.to_s.underscore }
18+
props.deep_symbolize_keys!
19+
20+
yield(props)
21+
end
22+
end
23+
end
24+
end
25+
end
26+
end

test/dummy_sprockets/app/views/pages/show.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</ul>
55

66
<div id='component-parent'>
7-
<%= react_component 'GreetingMessage', { name: @name }, { id: 'component', prerender: @prerender } %>
7+
<%= react_component 'GreetingMessage', { name: @name, last_name: "Last #{@name}", info: { name: @name } }, { id: 'component', class: "greeting-message", prerender: @prerender } %>
88
<ul>
99
<%= react_component 'Todo', { todo: 'Another Component' }, { id: 'todo', prerender: @prerender } %>
1010
</ul>

test/react/rails/test_helper_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'test_helper'
2+
3+
class TestHelperTest < ActionDispatch::IntegrationTest
4+
test 'assert_react_component' do
5+
get "/pages/1"
6+
assert_equal 200, response.status
7+
assert_react_component "GreetingMessage"
8+
assert_react_component "GreetingMessage" do |props|
9+
assert_equal "Bob", props[:name]
10+
assert_equal "Last Bob", props[:last_name]
11+
assert_equal "Bob", props[:info][:name]
12+
13+
assert_select "[id=?]", "component"
14+
assert_select "[class=?]", "greeting-message"
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)