Skip to content

Commit

Permalink
Add support for RSpec to previews as test cases (#1408)
Browse files Browse the repository at this point in the history
* Add rspec-rails gem

* Add support for render_previews in RSpec tests

* Enable RSpec tests on CI

* Update lib/view_component/render_preview_helper.rb

* Update lib/view_component/render_preview_helper.rb

* Update Gemfile

Co-authored-by: Joel Hawksley <joelhawksley@github.com>
  • Loading branch information
thutterer and joelhawksley authored Jul 18, 2022
1 parent 386279b commit 5b9dad7
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
run: |
bundle config path vendor/bundle
bundle update
bundle exec rake
bundle exec rake test spec
env:
RAISE_ON_WARNING: 1
MEASURE_COVERAGE: true
Expand Down
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rails_version = (ENV["RAILS_VERSION"] || "~> 7.0.0").to_s

gem "capybara", "~> 3"
gem "rails", rails_version == "main" ? {git: "https://github.com/rails/rails", ref: "main"} : rails_version
gem "rspec-rails", "~> 5"

if RUBY_VERSION >= "3.1"
gem "net-imap", require: false
Expand Down
19 changes: 19 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.1.10)
crass (1.0.6)
diff-lcs (1.5.0)
digest (3.1.0)
docile (1.4.0)
erb_lint (0.0.37)
Expand Down Expand Up @@ -193,6 +194,23 @@ GEM
rake (13.0.6)
regexp_parser (2.4.0)
rexml (3.2.5)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-rails (5.1.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
railties (>= 5.2)
rspec-core (~> 3.10)
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.11.0)
rubocop (1.13.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
Expand Down Expand Up @@ -268,6 +286,7 @@ DEPENDENCIES
pry (~> 0.13)
rails (~> 7.0.0)
rake (~> 13.0)
rspec-rails (~> 5)
simplecov (~> 0.18.0)
simplecov-console (~> 0.7.2)
slim (~> 4.0)
Expand Down
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Rake::TestTask.new(:test) do |t|
t.test_files = FileList["test/**/*_test.rb"]
end

begin
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
rescue LoadError
end

desc "Runs benchmarks against components"
task :partial_benchmark do
ruby "./performance/partial_benchmark.rb"
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ title: Changelog

## main

* Add support for `render_preview` in RSpec tests.

*Thomas Hutterer*

## 2.59.0

* Expose Capybara DSL methods directly inside tests.
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", "~> 6.0.0"
gem "rspec-rails", "~> 5.1"
gem "tailwindcss-rails", "~> 2.0"

gemspec path: "../"
1 change: 1 addition & 0 deletions gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", "~> 6.1.0"
gem "rspec-rails", "~> 5.1"
gem "tailwindcss-rails", "~> 2.0"
gem "net-smtp", require: false
gem "net-imap", require: false
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", "~> 7.0.0"
gem "rspec-rails", "~> 5.1"
gem "tailwindcss-rails", "~> 2.0"

gemspec path: "../"
1 change: 1 addition & 0 deletions gemfiles/rails_head.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "capybara", "~> 3"
gem "rails", github: "rails/rails", branch: "main"
gem "rspec-rails", "~> 5.1"
gem "tailwindcss-rails", "~> 2.0"

gemspec path: "../"
12 changes: 11 additions & 1 deletion lib/view_component/render_preview_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ module RenderPreviewHelper
#
# MyComponentTest -> MyComponentPreview etc.
#
# In RSpec, `Preview` is appended to `described_class`.
#
# @param preview [String] The name of the preview to be rendered.
# @return [Nokogiri::HTML]
def render_preview(name)
begin
preview_klass = self.class.name.gsub("Test", "Preview")
preview_klass = if respond_to?(:described_class)
if described_class.nil?
raise "`render_preview` expected a described_class, but it is nil."
end

"#{described_class}Preview"
else
self.class.name.gsub("Test", "Preview")
end
preview_klass = preview_klass.constantize
rescue NameError
raise NameError.new(
Expand Down
1 change: 1 addition & 0 deletions spec/sandbox
27 changes: 27 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "simplecov"
require "simplecov-console"

if ENV["MEASURE_COVERAGE"]
SimpleCov.start do
command_name "rails#{ENV["RAILS_VERSION"]}-ruby#{ENV["RUBY_VERSION"]}" if ENV["RUBY_VERSION"]

formatter SimpleCov::Formatter::Console
end
end

require "bundler/setup"

# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

require "view_component/deprecation"
ViewComponent::Deprecation.behavior = :silence

require File.expand_path("../sandbox/config/environment.rb", __FILE__)
require "rspec/rails"

RSpec.configure do |config|
config.include ViewComponent::TestHelpers
end
25 changes: 25 additions & 0 deletions test/sandbox/spec/components/previews/preview_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
describe PreviewComponent do
include ViewComponent::RenderPreviewHelper

before do
ViewComponent::Preview.load_previews
end

it "renders the preview" do
render_preview(:default)

expect(page).to have_css "h1", text: "Lorem Ipsum"
end
end

describe "PreviewComponent" do
include ViewComponent::RenderPreviewHelper

before do
ViewComponent::Preview.load_previews
end

it "raises an error" do
expect { render_preview(:default) }.to raise_error(/expected a described_class/)
end
end

0 comments on commit 5b9dad7

Please sign in to comment.