Skip to content

Commit 8259cca

Browse files
Add assert_component_rendered test helper (#2066)
* add assert_component_rendered * fix linting * update docs * docs cleanup * fix docs typo --------- Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent 51f9d49 commit 8259cca

File tree

6 files changed

+13
-2
lines changed

6 files changed

+13
-2
lines changed

docs/CHANGELOG.md

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

2323
*Seth Herr*
2424

25+
* Add `assert_component_rendered` test helper.
26+
27+
*Reegan Viljoen*
28+
2529
* Add `prefix:` option to `use_helpers`.
2630

2731
*Reegan Viljoen*

docs/guide/conditional_rendering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ end
5151
<%= render(ConfirmEmailComponent.new(user: current_user)) %>
5252
```
5353

54-
_To assert that a component hasn't been rendered, use `refute_component_rendered` from `ViewComponent::TestHelpers`._
54+
_To assert whether a component has been rendered, use `assert_component_rendered` / `refute_component_rendered` from `ViewComponent::TestHelpers`._

docs/guide/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ end
126126
To render ViewComponents outside of the view context (such as in a background job, markdown processor, etc), instantiate a Rails controller:
127127

128128
```ruby
129-
ApplicationComponent.new.view_context.render(MyComponent.new)
129+
ApplicationController.new.view_context.render(MyComponent.new)
130130
```

docs/guide/testing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class ExampleComponentTest < ViewComponent::TestCase
1515
def test_render_component
1616
render_inline(ExampleComponent.new(title: "my title")) { "Hello, World!" }
1717

18+
assert_component_rendered
19+
1820
assert_selector("span[title='my title']", text: "Hello, World!")
1921
# or, to just assert against the text:
2022
assert_text("Hello, World!")

lib/view_component/test_helpers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def page
1414
def refute_component_rendered
1515
assert_no_selector("body")
1616
end
17+
18+
def assert_component_rendered
19+
assert_selector("body")
20+
end
1721
rescue LoadError
1822
# We don't have a test case for running an application without capybara installed.
1923
# It's probably fine to leave this without coverage.

test/sandbox/test/rendering_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def test_conditional_rendering_if_content_provided
400400
"Content"
401401
end
402402

403+
assert_component_rendered
403404
assert_text("Content")
404405
end
405406

0 commit comments

Comments
 (0)