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

with_request_url test helper does not work with request.fullpath #1867

Closed
robbevp opened this issue Oct 9, 2023 · 3 comments
Closed

with_request_url test helper does not work with request.fullpath #1867

robbevp opened this issue Oct 9, 2023 · 3 comments

Comments

@robbevp
Copy link

robbevp commented Oct 9, 2023

When components use request.fullpath instead of request.path the same test using with_request_url will fail for one, but not for the other.

Steps to reproduce

Component:

# frozen_string_literal: true

class RequestComponent < ViewComponent::Base
  def call
    content_tag(:span, request.path, class: 'path') + content_tag(:span, request.fullpath, class: 'fullpath')
  end
end

Test file:

# frozen_string_literal: true

require "test_helper"

class RequestComponentTest < ViewComponent::TestCase
  test 'should print path and fullpath' do
    with_request_url '/admin/questions' do
      render_inline(RequestComponent.new)

      assert_selector '.path', text: '/admin/questions'
      assert_selector '.fullpath', text: '/admin/questions'
    end
    with_request_url '/admin/questions/1' do
      render_inline(RequestComponent.new)

      assert_selector '.path', text: '/admin/questions/1'
      assert_selector '.fullpath', text: '/admin/questions/1'
    end
    with_request_url '/admin/questions/1?foo=bar' do
      render_inline(RequestComponent.new)

      assert_selector '.path', text: '/admin/questions/1'
      assert_selector '.fullpath', text: '/admin/questions/1?foo=bar'
    end
  end
end

Expected behavior

The test above should pass.

Actual behavior

The request's fullpath property seems to stick from the earlier request. The test fails, since the fullpath still contains the path from the earlier request. This does not reflect the actual behaviour of the app (in a more complex component, of course).

It's easy to work around this issue, by splitting the test up in three different requests, but it might still be good to fix this - or document this issue somewhere.

Backtrace:

rails t test/components/request_component_test.rb
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 25637

# Running:

F

Failure:
RequestComponentTest#test_should_print_path_and_fullpath [/Users/robbevp/.../test/components/request_component_test.rb:17]:
expected to find visible css ".fullpath" with text "/admin/questions/1" but there were no matches. Also found "/admin/questions", which matched the selector but not all filters. 


rails test test/components/request_component_test.rb:6

System configuration

Rails version: 7.0.8

Ruby version: 3.2.2

Gem version: 3.6.0

@camertron
Copy link
Contributor

Hey @robbevp, thanks for bringing this to our attention. Could I ask you to submit a PR with a failing test? As it stands, I'm not sure if this is a bug with ViewComponent or Rails.

@nachiket87
Copy link
Contributor

Hey @camertron I've created a PR to fix this: #1893

@camertron
Copy link
Contributor

Should be fixed in #1893

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants