Add support for RSpec to previews as test cases (#1408)
* 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>
This commit is contained in:
Родитель
386279b272
Коммит
5b9dad7569
|
@ -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
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
--require spec_helper
|
1
Gemfile
1
Gemfile
|
@ -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
|
||||
|
|
19
Gemfile.lock
19
Gemfile.lock
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
6
Rakefile
6
Rakefile
|
@ -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"
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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: "../"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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: "../"
|
||||
|
|
|
@ -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: "../"
|
||||
|
|
|
@ -15,11 +15,21 @@ module ViewComponent
|
|||
#
|
||||
# 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(
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
../test/sandbox
|
|
@ -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
|
|
@ -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
|
Загрузка…
Ссылка в новой задаче