Skip to content

Commit 3073fe6

Browse files
author
Gabe Scholz
committed
Write specs for render_react_component in different test controller
1 parent adec41d commit 3073fe6

File tree

6 files changed

+62
-14
lines changed

6 files changed

+62
-14
lines changed

test/controller_helper_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'test_helper'
2+
require 'support/setup_capybara'
3+
4+
class ControllerHelperTest < ActionDispatch::IntegrationTest
5+
include Capybara::DSL
6+
7+
setup do
8+
@helper = ActionView::Base.new.extend(React::Rails::ViewHelper)
9+
Capybara.current_driver = Capybara.javascript_driver
10+
end
11+
12+
test 'uses a custom layout and status' do
13+
get 'helper/1'
14+
15+
assert response.status == 218
16+
assert response.body.include?('This is a different layout')
17+
end
18+
19+
test 'renders the React component' do
20+
get 'helper/1'
21+
22+
%w(data-react-class="Foo" data-react-props="{&quot;bar&quot;:&quot;value&quot;}").each do |segment|
23+
assert response.body.include?(segment)
24+
end
25+
end
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class HelperController < ApplicationController
2+
def show
3+
@todos = %w{todo1 todo2 todo3}
4+
render_react_component "Foo", {:bar => 'value'}, layout: 'custom', status: 218
5+
end
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dummy</title>
5+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7+
<%= csrf_meta_tags %>
8+
</head>
9+
<body>
10+
11+
This is a different layout
12+
<%= yield %>
13+
14+
</body>
15+
</html>

test/dummy/config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Dummy::Application.routes.draw do
22
resources :pages, :only => [:show]
33
resources :server, :only => [:show]
4+
resources :helper, :only => [:show]
45
end

test/support/setup_capybara.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'capybara/rails'
2+
require 'capybara/poltergeist'
3+
4+
Capybara.javascript_driver = :poltergeist
5+
Capybara.app = Rails.application
6+
7+
# Useful for debugging.
8+
# Just put page.driver.debug in your test and it will
9+
# pause and throw up a browser
10+
Capybara.register_driver :poltergeist_debug do |app|
11+
Capybara::Poltergeist::Driver.new(app, :inspector => true)
12+
end
13+
Capybara.javascript_driver = :poltergeist_debug

test/view_helper_test.rb

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
require 'test_helper'
2-
3-
require 'capybara/rails'
4-
require 'capybara/poltergeist'
5-
6-
Capybara.javascript_driver = :poltergeist
7-
Capybara.app = Rails.application
8-
9-
# Useful for debugging.
10-
# Just put page.driver.debug in your test and it will
11-
# pause and throw up a browser
12-
Capybara.register_driver :poltergeist_debug do |app|
13-
Capybara::Poltergeist::Driver.new(app, :inspector => true)
14-
end
15-
Capybara.javascript_driver = :poltergeist_debug
2+
require 'support/setup_capybara'
163

174
class ViewHelperTest < ActionDispatch::IntegrationTest
185
include Capybara::DSL

0 commit comments

Comments
 (0)