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

Capybara's within helper method not working #1910

Open
jdrosales17 opened this issue Nov 15, 2023 · 9 comments
Open

Capybara's within helper method not working #1910

jdrosales17 opened this issue Nov 15, 2023 · 9 comments

Comments

@jdrosales17
Copy link

Seems like Capybara's within helper is not working when testing a component using RSpec. Just to give an example, the following code passes in a test when the within selector doesn't match an element:

within('form#not-found') do
  expect(page).to have_button 'Save'
end

Expected behavior

The test should only pass when the within element is found.

Actual behavior

The test passes even if the within element is not found.

System configuration

Rails version: 6.1.7.6

Ruby version: 3.0.5

Gem version: 2.82.0

@reeganviljoen
Copy link
Collaborator

@jdrosales17 I see you are using view_component 2.82.0, have you tried reproducing the bug on the latest version

@camertron
Copy link
Contributor

camertron commented Nov 29, 2023

Hey @jdrosales17, thanks for bringing this up! I think you might be misunderstanding how Capybara's within helper works, but please correct me if I'm wrong. within yields a local to the block, which can be used to match child elements within the given selector. In other words, try this:

within('form#not-found') do |element|
  expect(element).to have_button 'Save'
end

Asserting on page will always search the entire page.

@jdrosales17
Copy link
Author

@reeganviljoen @camertron thanks for your responses! 🙂

@camertron I just tried what you said, adding the element block variable, but the test still passes even when the within selector has no match in the DOM.

@reeganviljoen I haven't tried this on a newer 3.x version yet, I'll let you know once I do! Thank you both! 🙌

@camertron
Copy link
Contributor

@jdrosales17 hmm there must be something else going on here then. Are you able to share your code so I can take a look? If not, then a minimal reproduction case would be the next best thing 😄

@reeganviljoen
Copy link
Collaborator

reeganviljoen commented Jan 8, 2024

@jdrosales17 are you able to share any code to help us debug this issue yet ?

@ruralocity
Copy link

Hi, I stumbled across this issue this morning, too.

view_component: 3.10.0
capybara: 3.39.2
rspec-rails: 5.1.2 (I can try updating to 6.x in case that makes a difference)
rails: 6.1.7.6
Ruby: 2.7.8

Here's a small reproduction case. I generated the files using rails g component demo, then augmented to show the behavior.

demo_component_spec.rb:

# frozen_string_literal: true

require "rails_helper"

RSpec.describe DemoComponent, type: :component do
  it "renders something useful" do
    render_inline(described_class.new)
    expect(page).to have_content "Hello, components!"
    expect(page).to_not have_content "Goodbye!"
    expect(page).to_not have_css "#demmmmo"
  end

  it "reproduces the bug when used with invalid content" do
    render_inline(described_class.new)

    # I'd expect this to fail, but passes
    within "#demo" do |element|
      expect(element).to have_content "Goodbye!"
    end
  end

  it "reproduces the bug when used with invalid DOM id" do
    render_inline(described_class.new)

    # I'd expect this to fail, but passes
    within "#demmmmo" do |element|
      expect(element).to have_content "Hello, components!"
    end
  end
end

demo_component.rb:

# frozen_string_literal: true

class DemoComponent < ViewComponent::Base

end

demo_component.html.erb:

<div>Add Demo template here</div>

<div id="#demo">
  Hello, components!
</div>

olleolleolle added a commit to olleolleolle/vc1910 that referenced this issue Feb 5, 2024
@olleolleolle
Copy link
Contributor

olleolleolle commented Feb 5, 2024

👋 Thanks for the reproduction script, I amended it, https://github.com/olleolleolle/vc1910/ and made it run "red if wrong".

With latest Capybara and a rails new with today's Rails and ruby 3.2.2, it still had the issue.

olleolleolle added a commit to barsoom/view_component that referenced this issue Feb 5, 2024
@olleolleolle
Copy link
Contributor

I tried to get closer to this repo with the reproduction, so here is an in-repo commit with a spec which fails

barsoom@5150235

@javierm
Copy link
Contributor

javierm commented Mar 8, 2024

Sorry if I'm not up-to-date 🙏. If I remember correctly, support for within was added in #1389 but then reverted in #1427. I haven't seen references to this issue in the changelog since then 🤔.

In case someone's interested, here's a workaround for RSpec I've been using for a few years:

module ViewComponent
  module TestHelpers
    def within(...)
      raise "`within` doesn't work in component tests. Use `page.find` instead."
    end
  end
end

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

No branches or pull requests

6 participants