-
Notifications
You must be signed in to change notification settings - Fork 443
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
add render_preview
test helper
#1347
Changes from all commits
36f5acd
c6a8578
2ae1f61
598f54c
11aa0cf
b425fa6
d425ba2
929016b
dbce872
ab62dbf
464d921
1dddf60
a639265
f913810
bfa77c0
8d93e6f
06e7bac
9bdadfe
7b8a3fe
5de1d4d
db91b42
1a3eb4c
09f42de
d16f1d9
86455ff
ebaea4a
3fbc56a
7cda925
20e5e4a
45daf39
8fa0fc0
338c3b7
f89176c
95b115f
ffc855f
d34254c
5ed25ff
359ea37
780b0ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "view_component/render_preview_helper" | ||
|
||
module ViewComponent | ||
module TestHelpers | ||
begin | ||
require "capybara/minitest" | ||
include Capybara::Minitest::Assertions | ||
|
||
def page | ||
Capybara::Node::Simple.new(@rendered_component) | ||
Capybara::Node::Simple.new(@rendered_content) | ||
end | ||
|
||
def refute_component_rendered | ||
|
@@ -41,14 +43,14 @@ def refute_component_rendered | |
# @param component [ViewComponent::Base, ViewComponent::Collection] The instance of the component to be rendered. | ||
# @return [Nokogiri::HTML] | ||
def render_inline(component, **args, &block) | ||
@rendered_component = | ||
@rendered_content = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super minor, but I wonder if this is being used directly by any libraries/apps and if renaming it would break anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do wonder that too. I'll add a note to the changelog. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This indeed is used by lots of people! #1372 and #1373 offer two fixes. I wonder if we should update the documentation to more explicitly direct people towards writing expectations that use expect(page).to have_link("Home page", href: root_path) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related issue: #1375 |
||
if Rails.version.to_f >= 6.1 | ||
controller.view_context.render(component, args, &block) | ||
else | ||
controller.view_context.render_component(component, &block) | ||
end | ||
|
||
Nokogiri::HTML.fragment(@rendered_component) | ||
Nokogiri::HTML.fragment(@rendered_content) | ||
end | ||
|
||
# Execute the given block in the view context. Internally sets `page` to be a | ||
|
@@ -62,8 +64,8 @@ def render_inline(component, **args, &block) | |
# assert_text("Hello, World!") | ||
# ``` | ||
def render_in_view_context(&block) | ||
@rendered_component = controller.view_context.instance_exec(&block) | ||
Nokogiri::HTML.fragment(@rendered_component) | ||
@rendered_content = controller.view_context.instance_exec(&block) | ||
Nokogiri::HTML.fragment(@rendered_content) | ||
end | ||
|
||
# @private | ||
|
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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, why was this change necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. Since we're calling
previews
directly fromrender_preview
,before_action
isn't called. In fact, I think we could inlinefind_preview
here!