Skip to content

Commit

Permalink
set request full path manually for tests with request url (ViewCompon…
Browse files Browse the repository at this point in the history
  • Loading branch information
nachiket87 authored and claudiob committed Dec 22, 2023
1 parent 6f30ab6 commit 087f33a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Fix bug where `#with_request_url`, set the incorrect `request.fullpath`.

*Nachiket Pusalkar*

## 3.7.0

* Support Rails 7.1 in CI.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ ViewComponents are Ruby objects, making it easy to follow (and enforce) code qua

ViewComponent is built by over a hundred members of the community, including:

<img src="https://avatars.githubusercontent.com/nachiket87?s=64" alt="nachiket87" width="32" />
<img src="https://avatars.githubusercontent.com/andrewjtait?s=64" alt="andrewjtait" width="32" />
<img src="https://avatars.githubusercontent.com/asgerb?s=64" alt="asgerb" width="32" />
<img src="https://avatars.githubusercontent.com/bbugh?s=64" alt="bbugh" width="32" />
Expand Down
6 changes: 4 additions & 2 deletions lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,17 @@ def with_controller_class(klass)
#
# @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(path, host: nil)
def with_request_url(full_path, host: nil)
old_request_host = vc_test_request.host
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
old_request_query_string = vc_test_request.query_string
old_controller = defined?(@vc_test_controller) && @vc_test_controller

path, query = path.split("?", 2)
path, query = full_path.split("?", 2)
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.path_info = path
vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {})
Expand Down
7 changes: 7 additions & 0 deletions test/sandbox/app/components/request_url_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RequestUrlComponent < ViewComponent::Base
def call
content_tag(:span, request.path, class: "path") + content_tag(:span, request.fullpath, class: "fullpath")
end
end
28 changes: 28 additions & 0 deletions test/sandbox/test/components/request_url_component_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "test_helper"

class RequestUrlComponentTest < ViewComponent::TestCase
test "should print path and fullpath" do
with_request_url "/products" do
render_inline(RequestUrlComponent.new)

assert_selector ".path", text: "/products"
assert_selector ".fullpath", text: "/products"
end

with_request_url "/slots?param=1&paramtwo=2" do
render_inline(RequestUrlComponent.new)

assert_selector ".path", text: "/slots"
assert_selector ".fullpath", text: "/slots?param=1&paramtwo=2"
end

with_request_url "/slots" do
render_inline(RequestUrlComponent.new)

assert_selector ".path", text: "/slots"
assert_selector ".fullpath", text: "/slots"
end
end
end

0 comments on commit 087f33a

Please sign in to comment.