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

Define aria: and data: Capybara expression filters #49665

Closed

Conversation

seanpdoyle
Copy link
Contributor

Motivation / Background

When Capybara is available to the environment, define aria: and data: expression filters to match Action View's aria: { ... } and data: { ... } nesting:

tag.div id: "one", aria: { labelledby: "two" }, data: { nested_key: "value" }
  # => <div id="one" aria-labelledby="two" data-nested-key="value"></div>

assert_selector :element, aria: { labelledby: "two" }, data: { nested_key: "value" }

Detail

This commit implements them as part of loading railties. The rationale for extending them from rails/test_help is that test cases other than ActionDispatch::SystemTestCase have the ability to leverage Capybara. For example:

To that end, this commit includes test coverage for ActionView::TestCase and ActionDispatch::IntegrationTest to exercise those possibilities.

Additional information

Originally, these filters were proposed upstream in teamcapybara/capybara#2709 and teamcapybara/capybara#2708.

Ultimately, the Capybara team felt they'd be better suited for Rails, since the aria: and data: nesting are unique to Action View.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

require "rails/testing/capybara_extensions"

Rails.configuration.after_initialize do
Capybara::Selector.all.each do |name, _|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this would be a configurable list that defaults to all selectors. Since this file is required from railties/lib/rails/test_help.rb, I wasn't sure how to defer this code to be late enough to read from a configuration value. I would appreciate any guidance on how to achieve that.

Comment on lines +19 to +21
if defined?(Capybara)
require "rails/testing/capybara"
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned in the PR description, this felt like the most global place to do this extension, since ActionDispatch::SystemTestCase, ActionDispatch::IntegrationTest, ActionView::TestCase, and maybe even ActionMailer::TestCase could benefit from defining Capybara extensions.

Having said that, it feels like there is a more appropriate place to do so, or an entirely different and more appropriate mechanism.

@seanpdoyle seanpdoyle force-pushed the capybara-nested-attributes-filter branch from 8e2fa66 to cac23e1 Compare October 17, 2023 00:22
@rails-bot rails-bot bot added the docs label Oct 17, 2023
@seanpdoyle seanpdoyle force-pushed the capybara-nested-attributes-filter branch 3 times, most recently from d489e9f to 95e9c31 Compare October 17, 2023 08:11
When `Capybara` is available to the environment, define `aria:` and
`data:` expression filters to match Action View's `aria: { ... }` and
`data: { ... }` nesting:

```ruby
tag.div id: "one", aria: { labelledby: "two" }, data: { nested_key: "value" }
  # => <div id="one" aria-labelledby="two" data-nested-key="value"></div>

assert_selector :element, aria: { labelledby: "two" }, data: { nested_key: "value" }
```

Originally, these filters were proposed upstream, but ultimately, the
Capybara team felt they'd be better suited for Rails, since the `aria:`
and `data:` nesting are unique to Action View.

This commit implements them as part of loading `railties`. The rationale
for extending them from `rails/test_help` is that test cases other than
`ActionDispatch::SystemTestCase` have the ability to leverage Capybara.
For example:

* by [extending ActionView::TestCase](https://edgeguides.rubyonrails.org/testing.html#testing-view-partials)
* by [extending ActionDispatch::TestCase](rails#43361)
* from third-party gems like [ViewComponent](https://viewcomponent.org/guide/testing.html)

To that end, this commit includes test coverage for
`ActionView::TestCase` and `ActionDispatch::IntegrationTest` to exercise
those possibilities.
@seanpdoyle seanpdoyle force-pushed the capybara-nested-attributes-filter branch from 95e9c31 to b9911f9 Compare October 17, 2023 13:37
@seanpdoyle
Copy link
Contributor Author

This style of extension is discouraged by Capybara, so I'm closing in favor of a gem.

If there's interested in adding this support directly, I'm happy to re-open.

@seanpdoyle seanpdoyle closed this Oct 18, 2023
@seanpdoyle seanpdoyle deleted the capybara-nested-attributes-filter branch October 18, 2023 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant