-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
render_preview
test helper (#1347)
* clean up previews docs * RSpec not rspec * always find preview for action * rename @rendered_component to @rendered_page * Introduce `render_preview` test helper * rename rendered_page to rendered_content * rename benchmark * copy edits * re-add lookbook note * Update docs/CHANGELOG.md Co-authored-by: Blake Williams <blakewilliams@github.com> * Update docs/CHANGELOG.md Co-authored-by: Blake Williams <blakewilliams@github.com> * update depa * add note to changelog * render_preview is opt-in * fix broken reference in benchmark build * always load descendants * add Preview.load_all/load_all! * how about this? * remove duplicate before_action * Ruby 3 changed error formatting! * don't assert with period * mdlint * rename ivar * always load all previews * try this * explicitly load previews * one more try * this one? * load in engine? * uncomment load_previews call * remove call in engine Co-authored-by: Blake Williams <blakewilliams@github.com>
- Loading branch information
1 parent
2273e01
commit 4bceecf
Showing
15 changed files
with
160 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
module RenderPreviewHelper | ||
# Render a preview inline. Internally sets `page` to be a `Capybara::Node::Simple`, | ||
# allowing for Capybara assertions to be used: | ||
# | ||
# ```ruby | ||
# render_preview(:default) | ||
# assert_text("Hello, World!") | ||
# ``` | ||
# | ||
# Note: `#rendered_preview` expects a preview to be defined with the same class | ||
# name as the calling test, but with `Test` replaced with `Preview`: | ||
# | ||
# MyComponentTest -> MyComponentPreview etc. | ||
# | ||
# @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 = preview_klass.constantize | ||
rescue NameError | ||
raise NameError.new( | ||
"`render_preview` expected to find #{preview_klass}, but it does not exist." | ||
) | ||
end | ||
|
||
previews_controller = build_controller(ViewComponent::Base.preview_controller.constantize) | ||
previews_controller.request.params[:path] = "#{preview_klass.preview_name}/#{name}" | ||
previews_controller.response = ActionDispatch::Response.new | ||
result = previews_controller.previews | ||
|
||
@rendered_content = result | ||
|
||
Nokogiri::HTML.fragment(@rendered_content) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class MyComponentTest < ViewComponent::TestCase | ||
include ViewComponent::RenderPreviewHelper | ||
|
||
def setup | ||
ViewComponent::Preview.load_previews | ||
end | ||
|
||
def test_render_preview | ||
render_preview(:default) | ||
|
||
assert_selector("div", text: "hello,world!") | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
test/sandbox/test/components/without_preview_component_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class WithoutPreviewComponentTest < ViewComponent::TestCase | ||
include ViewComponent::RenderPreviewHelper | ||
|
||
def test_render_preview | ||
error = assert_raises NameError do | ||
render_preview(:default) | ||
end | ||
|
||
assert_equal( | ||
error.message.split(".")[0], | ||
"`render_preview` expected to find WithoutPreviewComponentPreview, but it does not exist" | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters