-
Notifications
You must be signed in to change notification settings - Fork 440
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
Comments
@jdrosales17 I see you are using view_component 2.82.0, have you tried reproducing the bug on the latest version |
Hey @jdrosales17, thanks for bringing this up! I think you might be misunderstanding how Capybara's within('form#not-found') do |element|
expect(element).to have_button 'Save'
end Asserting on |
@reeganviljoen @camertron thanks for your responses! 🙂 @camertron I just tried what you said, adding the @reeganviljoen I haven't tried this on a newer 3.x version yet, I'll let you know once I do! Thank you both! 🙌 |
@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 😄 |
@jdrosales17 are you able to share any code to help us debug this issue yet ? |
Hi, I stumbled across this issue this morning, too. view_component: 3.10.0 Here's a small reproduction case. I generated the files using 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> |
👋 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 |
I tried to get closer to this repo with the reproduction, so here is an in-repo commit with a spec which fails |
Sorry if I'm not up-to-date 🙏. If I remember correctly, support for 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 |
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 thewithin
selector doesn't match an element: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
The text was updated successfully, but these errors were encountered: