Skip to content

Commit

Permalink
Add support for RSpec to previews as test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
thutterer committed Jun 28, 2022
1 parent 1a2e39d commit 7ed426c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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 RSpec to previews as test cases

*Thomas Hutterer*

* Include polymorphic slots in `ViewComponent::Base` by default.
* Add per-component config option for stripping newlines from templates before compilation.

Expand Down
10 changes: 8 additions & 2 deletions lib/view_component/render_preview_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ module RenderPreviewHelper
# ```
#
# Note: `#rendered_preview` expects a preview to be defined with the same class
# name as the calling test, but with `Test` replaced with `Preview`:
# name as the calling test, but with "Test" replaced with "Preview":
#
# MyComponentTest -> MyComponentPreview etc.
#
# With RSpec it uses `described_class` plus "Preview" as the class name.
#
# @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 self.respond_to?(:described_class)
"#{self.described_class}Preview"
else
self.class.name.gsub("Test", "Preview")
end
preview_klass = preview_klass.constantize
rescue NameError
raise NameError.new(
Expand Down

0 comments on commit 7ed426c

Please sign in to comment.