Skip to content

Commit e1966ce

Browse files
authored
render_preview can pass parameters to preview (#1499)
* remove experimental tag * move render_preview docs down * update docs to reference more reliable `m` command * `render_preview` can pass parameters to preview * linters
1 parent 8ea73f3 commit e1966ce

File tree

8 files changed

+48
-16
lines changed

8 files changed

+48
-16
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* `render_preview` can pass parameters to preview.
14+
15+
*Joel Hawksley*
16+
1317
* Fix docs typos.
1418

1519
*Joel Hawksley*

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The codespace environment includes a minimal Rails app with ViewComponent instal
5656
Use [`m`](https://rubygems.org/gems/m):
5757

5858
```command
59-
m test/view_component/YOUR_COMPONENT_test.rb:line_number
59+
bundle exec m test/view_component/YOUR_COMPONENT_test.rb:line_number
6060
```
6161

6262
### Running tests for a specific version of Rails

docs/guide/previews.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@ Then access the resulting previews at:
3232

3333
_For a more interactive experience, consider using [Lookbook](https://github.com/allmarkedup/lookbook) or [ViewComponent::Storybook](https://github.com/jonspalmer/view_component_storybook)._
3434

35+
## Passing parameters
36+
37+
Set dynamic values from URL parameters by setting them as arguments:
38+
39+
```ruby
40+
# test/components/previews/example_component_preview.rb
41+
class ExampleComponentPreview < ViewComponent::Preview
42+
def with_dynamic_title(title: "Example component default")
43+
render(ExampleComponent.new(title: title))
44+
end
45+
end
46+
```
47+
48+
Then pass in a value: `/rails/view_components/example_component/with_dynamic_title?title=Custom+title`.
49+
3550
## Previews as test cases
3651

3752
Since 2.56.0
3853
{: .label }
3954

40-
Experimental
41-
{: .label .label-yellow }
42-
4355
Use `render_preview(name)` to render previews in ViewComponent unit tests:
4456

4557
```ruby
@@ -52,21 +64,18 @@ class ExampleComponentTest < ViewComponent::TestCase
5264
end
5365
```
5466

55-
## Passing parameters
56-
57-
Set dynamic values from URL parameters by setting them as arguments:
67+
Parameters can also be passed:
5868

5969
```ruby
60-
# test/components/previews/example_component_preview.rb
61-
class ExampleComponentPreview < ViewComponent::Preview
62-
def with_dynamic_title(title: "Example component default")
63-
render(ExampleComponent.new(title: title))
70+
class ExampleComponentTest < ViewComponent::TestCase
71+
def test_render_preview
72+
render_preview(:with_default_title, params: { message: "Hello, world!" })
73+
74+
assert_text("Hello, world!")
6475
end
6576
end
6677
```
6778

68-
Then pass in a value: `/rails/view_components/example_component/with_dynamic_title?title=Custom+title`.
69-
7079
## Helpers
7180

7281
The `ViewComponent::Preview` base class includes

docs/guide/slots.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ end
147147

148148
## Referencing slots
149149

150-
As the content passed to slots is registered after a component is initialized, it cannot referenced in an initializer. One way to reference slot content is using the `before_render` [lifecycle method](/guide/lifecycle):
150+
As the content passed to slots is registered after a component is initialized, it can't be referenced in an initializer. One way to reference slot content is using the `before_render` [lifecycle method](/guide/lifecycle):
151151

152152
```ruby
153153
# blog_component.rb

lib/view_component/test_helpers.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ def render_inline(component, **args, &block)
8080
#
8181
# In RSpec, `Preview` is appended to `described_class`.
8282
#
83-
# @param preview [String] The name of the preview to be rendered.
83+
# @param name [String] The name of the preview to be rendered.
84+
# @param params [Hash] Parameters to be passed to the preview.
8485
# @return [Nokogiri::HTML]
85-
def render_preview(name)
86+
def render_preview(name, params: {})
8687
begin
8788
preview_klass = if respond_to?(:described_class)
8889
raise "`render_preview` expected a described_class, but it is nil." if described_class.nil?
@@ -97,6 +98,13 @@ def render_preview(name)
9798
end
9899

99100
previews_controller = build_controller(Rails.application.config.view_component.preview_controller.constantize)
101+
102+
# From what I can tell, it's not possible to overwrite all request parameters
103+
# at once, so we set them individually here.
104+
params.each do |k, v|
105+
previews_controller.request.params[k] = v
106+
end
107+
100108
previews_controller.request.params[:path] = "#{preview_klass.preview_name}/#{name}"
101109
previews_controller.response = ActionDispatch::Response.new
102110
result = previews_controller.previews
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<div>hello,world!</div>
2+
<%= content %>

test/sandbox/test/components/my_component_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ def test_render_preview
1212

1313
assert_selector("div", text: "hello,world!")
1414
end
15+
16+
def test_render_preview_with_args
17+
render_preview(:with_content, params: {content: "foo"})
18+
19+
assert_text("foo")
20+
end
1521
end

test/sandbox/test/components/previews/my_component_preview.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def default
77
render(MyComponent.new)
88
end
99

10+
def with_content(content:)
11+
render(MyComponent.new.with_content(content))
12+
end
13+
1014
def inside_banner
1115
render_with_template
1216
end

0 commit comments

Comments
 (0)