Skip to content
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 test coverage for Partial Helpers #64

Closed
wants to merge 1 commit into from

Conversation

seanpdoyle
Copy link
Collaborator

Introduce renderer test coverage to exercise that Partials can declare their own isolated helper methods for use within templates.

Introduce renderer test coverage to exercise that Partials can declare
their own [isolated helper methods][] for use within templates.

[isolated helper methods]: https://github.com/bullet-train-co/nice_partials/tree/v0.1.5#defining-and-using-well-isolated-helper-methods
end
%>

<p><%= upcase yield %></p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we can call upcase without doing partial.upcase?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm not quite sure what's going on here, I'll try to look locally.

@seanpdoyle
Copy link
Collaborator Author

seanpdoyle commented Sep 22, 2022

@kaspth the outer |partial| block variable isn't available to inner block calls:

diff --git a/test/fixtures/_partial_with_helpers.html.erb b/test/fixtures/_partial_with_helpers.html.erb
index e4b57e3..3131172 100644
--- a/test/fixtures/_partial_with_helpers.html.erb
+++ b/test/fixtures/_partial_with_helpers.html.erb
@@ -11,3 +11,5 @@
 %>
 
 <p><%= upcase yield %></p>
+
+<%= partial.block %>
diff --git a/test/renderer_test.rb b/test/renderer_test.rb
index c5bcaf7..bde5a72 100644
--- a/test/renderer_test.rb
+++ b/test/renderer_test.rb
@@ -55,6 +55,14 @@ class RendererTest < NicePartials::Test
     assert_css "p", text: "TEXT WITH SPACES"
   end
 
+  test "render partial exposes helper methods within caller's blocks" do
+    render "partial_with_helpers" do |partial|
+      partial.block { partial.upcase("text in a block") }
+    end
+
+    assert_text "TEXT IN A BLOCK"
+  end
+
   test "accessing partial in outer context won't leak state to inner render" do
     render "partial_accessed_in_outer_context"

That makes the partial:

<%
  partial.helpers do
    def upcase(content)
      content.upcase
    end

    def squish(content)
      content.gsub(/\s+/, " ")
    end
  end
%>

<p><%= upcase yield %></p>

<%= partial.block %>

The test failure is something like:

Error:                                                                                                                                  
RendererTest#test_render_partial_exposes_helper_methods_within_caller's_blocks:                                                         
ActionView::Template::Error: undefined method `upcase' for nil:NilClass                                                                 
    test/renderer_test.rb:59:in `block in <class:RendererTest>'

kaspth pushed a commit that referenced this pull request Nov 7, 2022
Introduce renderer test coverage to exercise that Partials can declare
their own [isolated helper methods][] for use within templates.

Fixes #64

[isolated helper methods]: https://github.com/bullet-train-co/nice_partials/tree/v0.1.5#defining-and-using-well-isolated-helper-methods
kaspth pushed a commit that referenced this pull request Nov 7, 2022
Introduce renderer test coverage to exercise that Partials can declare
their own [isolated helper methods][] for use within templates.

Fixes #64

[isolated helper methods]: https://github.com/bullet-train-co/nice_partials/tree/v0.1.5#defining-and-using-well-isolated-helper-methods
@kaspth kaspth closed this in #65 Nov 7, 2022
kaspth added a commit that referenced this pull request Nov 7, 2022
Introduce renderer test coverage to exercise that Partials can declare
their own [isolated helper methods][] for use within templates.

Fixes #64

[isolated helper methods]: https://github.com/bullet-train-co/nice_partials/tree/v0.1.5#defining-and-using-well-isolated-helper-methods

Co-authored-by: Sean Doyle <sean.p.doyle24@gmail.com>
@kaspth
Copy link
Contributor

kaspth commented Nov 7, 2022

@kaspth the outer |partial| block variable isn't available to inner block calls:

interesting, I'm a little tired so I can't quite get it, do you want to double check now and open a new issue?

@seanpdoyle seanpdoyle deleted the partial-helpers branch November 7, 2022 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants