Skip to content

Commit

Permalink
Allow setting method when using the test helper (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 6, 2023
1 parent 47191f0 commit b11ba1f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Expand Up @@ -26,6 +26,10 @@ nav_order: 5

*Nachiket Pusalkar*

* Allow setting method when using the `with_request_url` test helper.

*Andrew Duthie*

## 3.7.0

* Support Rails 7.1 in CI.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Expand Up @@ -211,6 +211,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/reeganviljoen?s=64" alt="reeganviljoen" width="32" />
<img src="https://avatars.githubusercontent.com/thomascchen?s=64" alt="thomascchen" width="32" />
<img src="https://avatars.githubusercontent.com/milk1000cc?s=64" alt="milk1000cc" width="32" />
<img src="https://avatars.githubusercontent.com/aduth?s=64" alt="aduth" width="32" />

## Who uses ViewComponent?

Expand Down
14 changes: 13 additions & 1 deletion lib/view_component/test_helpers.rb
Expand Up @@ -163,10 +163,20 @@ def with_controller_class(klass)
# end
# ```
#
# To specify a request method, pass the method param:
#
# ```ruby
# with_request_url("/users/42", method: "POST") do
# render_inline(MyComponent.new)
# end
# ```
#
# @param path [String] The path to set for the current request.
# @param host [String] The host to set for the current request.
def with_request_url(full_path, host: nil)
# @param method [String] The request method to set for the current request.
def with_request_url(full_path, host: nil, method: nil)
old_request_host = vc_test_request.host
old_request_method = vc_test_request.request_method
old_request_path_info = vc_test_request.path_info
old_request_path_parameters = vc_test_request.path_parameters
old_request_query_parameters = vc_test_request.query_parameters
Expand All @@ -177,6 +187,7 @@ def with_request_url(full_path, host: nil)
vc_test_request.instance_variable_set(:@fullpath, full_path)
vc_test_request.instance_variable_set(:@original_fullpath, full_path)
vc_test_request.host = host if host
vc_test_request.request_method = method if method
vc_test_request.path_info = path
vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {})
vc_test_request.set_header("action_dispatch.request.query_parameters",
Expand All @@ -185,6 +196,7 @@ def with_request_url(full_path, host: nil)
yield
ensure
vc_test_request.host = old_request_host
vc_test_request.request_method = old_request_method
vc_test_request.path_info = old_request_path_info
vc_test_request.path_parameters = old_request_path_parameters
vc_test_request.set_header("action_dispatch.request.query_parameters", old_request_query_parameters)
Expand Down
6 changes: 1 addition & 5 deletions test/sandbox/app/components/current_page_component.rb
Expand Up @@ -2,10 +2,6 @@

class CurrentPageComponent < ViewComponent::Base
def text
if current_page?("/slots")
"Inside /slots"
else
"Outside /slots"
end
"#{current_page?("/slots") ? "Inside" : "Outside"} /slots (#{request.method} #{request.path})"
end
end
1 change: 1 addition & 0 deletions test/sandbox/config/routes.rb
Expand Up @@ -29,6 +29,7 @@
get :cached_partial, to: "integration_examples#cached_partial"
get :inherited_sidecar, to: "integration_examples#inherited_sidecar"
get :inherited_from_uncompilable_component, to: "integration_examples#inherited_from_uncompilable_component"
post :create, to: "integration_examples#create"

constraints(lambda { |request| request.env["warden"].authenticate! }) do
get :constraints_with_env, to: "integration_examples#index"
Expand Down
12 changes: 10 additions & 2 deletions test/sandbox/test/test_helper_test.rb
Expand Up @@ -8,15 +8,23 @@ def test_with_request_url_inside_slots_path
render_inline(CurrentPageComponent.new)
end

assert_selector("div", text: "Inside /slots")
assert_selector("div", text: "Inside /slots (GET /slots)")
end

def test_with_request_url_outside_slots_path
with_request_url "/" do
render_inline(CurrentPageComponent.new)
end

assert_selector("div", text: "Outside /slots")
assert_selector("div", text: "Outside /slots (GET /)")
end

def test_with_request_url_specifying_method
with_request_url "/create", method: "POST" do
render_inline(CurrentPageComponent.new)
end

assert_selector("div", text: "Outside /slots (POST /create)")
end

def test_with_request_url_under_constraint
Expand Down

0 comments on commit b11ba1f

Please sign in to comment.