Skip to content

fix for rspec #367

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format doc
6 changes: 6 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ end
appraise "rails-4.2" do
gem 'rails', '~> 4.2.1'
end

appraise "rails-4.2-with-rspec" do
gem 'rails', '~> 4.2.1'
gem 'rspec'
gem 'rspec-rails'
end
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ Rake::TestTask.new(:test) do |t|
t.verbose = ENV['TEST_VERBOSE'] == '1'
end

begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task test: :spec
rescue LoadError
end

task default: :test
9 changes: 9 additions & 0 deletions gemfiles/rails_4.2_with_rspec.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "rails", "~> 4.2.1"
gem "rspec"
gem "rspec-rails"

gemspec :path => "../"
1 change: 1 addition & 0 deletions lib/react/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
require 'react/rails/component_mount'
require 'react/rails/view_helper'
require 'react/rails/controller_renderer'
require 'react/rails/rspec_helper'
1 change: 1 addition & 0 deletions lib/react/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Railtie < ::Rails::Railtie
React::ServerRendering.reset_pool
# Reload renderers in dev when files change
ActionDispatch::Reloader.to_prepare { React::ServerRendering.reset_pool }
RSpec.configure { |c| c.include React::Rails::RspecHelper } if defined?(RSpec::Rails)
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/react/rails/rspec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module React
module Rails
module RspecHelper
extend ActiveSupport::Concern

included do |mod|
mod.class_eval do
before { setup_react_rails_rspec } if respond_to?(:before)
end

def setup_react_rails_rspec
helper = controller.setup_react_component_helper if respond_to?(:controller)
assign :__react_component_helper, helper if helper && respond_to?(:assign)
end
end
end
end
end
10 changes: 10 additions & 0 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe PagesController, type: :controller do
describe 'assigns' do
it do
get :show , id: 1
expect(assigns).to have_key :__react_component_helper
end
end
end
36 changes: 36 additions & 0 deletions spec/controllers/server_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rails_helper'

RSpec.describe ServerController, type: :controller do
describe 'console_example' do
it do
get :console_example
[/Console Logged/,
/console.log.apply\(console, \["got initial state"\]\)/,
/console.warn.apply\(console, \["mounted component"\]\)/,
/console.error.apply\(console, \["rendered!","foo"\]\)/].each do |regex|
expect(response.body).to match regex
end
end
end

describe 'console_example_suppressed' do
it do
get :console_example_suppressed
expect(response.body).to match(/Console Logged/)
[/console.log/, /console.warn/, /console.error/].each do |regex|
expect(response.body).not_to match regex
end
end
end

describe 'inline_component' do
it do
get :inline_component
[/<span data-react-class=\"TodoList\"/,
/Render this inline<\/span>/,
/<title>Dummy<\/title>/].each do |regex|
expect(response.body).to match regex
end
end
end
end
11 changes: 11 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../test/dummy/config/environment', __FILE__)
require 'rspec/rails'

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
config.render_views = true
end
96 changes: 96 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true

# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"

# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
config.disable_monkey_patching!

# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end

# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end
10 changes: 10 additions & 0 deletions spec/views/pages/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe 'pages/show.html.erb', type: :view do
describe 'assigns' do
it { expect(assigns).to have_key :__react_component_helper }
end
describe '#render' do
it { expect { render }.not_to raise_error }
end
end