Skip to content

Introduce ActionView::TestCase.register_parser #49194

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

Merged
merged 1 commit into from
Sep 27, 2023

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Sep 8, 2023

Motivation / Background

Register a callable to decode rendered content for a given MIME type

Each registered parser will also define a #rendered.$MIME helper method, where $MIME corresponds to the value of the mime argument.

Detail

Arguments

mime - Symbol the MIME Type name for the rendered content callable - Callable to decode the String. Accepts the String
value as its only argument
block - Block serves as the parser when the
callable is omitted

By default, ActionView::TestCase defines a parser for:

  • :html - returns an instance of Nokogiri::XML::Node
  • :json - returns an instance of ActiveSupport::HashWithIndifferentAccess

Each pre-registered parser also defines a corresponding helper:

  • :html - defines rendered.html
  • :json - defines rendered.json

Examples

To parse the rendered content into RSS, register a call to RSS::Parser.parse:

register_parser :rss, -> rendered { RSS::Parser.parse(rendered) }

test "renders RSS" do
  article = Article.create!(title: "Hello, world")

  render formats: :rss, partial: article

  assert_equal "Hello, world", rendered.rss.items.last.title
end

To parse the rendered content into a Capybara::Simple::Node, re-register an :html parser with a call to
Capybara.string:

register_parser :html, -> rendered { Capybara.string(rendered) }

test "renders HTML" do
  article = Article.create!(title: "Hello, world")

  render partial: article

  rendered.html.assert_css "h1", text: "Hello, world"
end

For the sake of backwards compatibility, re-define existing support for document_root_element in terms of rendered.html.

Additional information

This proposal draws inspiration from ActionDispatch::Testing::RequestEncoder, and the response.parsed_body test method that it enables.

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.

@rails-bot rails-bot bot added the actionview label Sep 8, 2023
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch from 39ff775 to bffea94 Compare September 8, 2023 02:14
@rails-bot rails-bot bot added the docs label Sep 8, 2023
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch from bffea94 to c1f01c0 Compare September 8, 2023 02:15
@seanpdoyle
Copy link
Contributor Author

@rafaelfranca this proposal is in the same spirit of the work merged as part of #49003.

@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch from c1f01c0 to d21f400 Compare September 8, 2023 02:18
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch 6 times, most recently from b7442a8 to c439b22 Compare September 13, 2023 14:03
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch 2 times, most recently from a764d3f to 42a11cb Compare September 15, 2023 21:04
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch 3 times, most recently from c750252 to cad9fbf Compare September 22, 2023 16:28
@rails-bot rails-bot bot added the railties label Sep 22, 2023
@seanpdoyle seanpdoyle requested review from zzak and p8 September 22, 2023 16:28
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch from cad9fbf to f61c6ef Compare September 22, 2023 16:31
@seanpdoyle seanpdoyle changed the title Introduce ActionView::TestCase.register_decoder Introduce ActionView::TestCase.register_parser Sep 22, 2023
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch 2 times, most recently from fe85304 to 1756b97 Compare September 22, 2023 16:35
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch from 1756b97 to 3365450 Compare September 22, 2023 17:05
@zzak zzak added this to the 7.1.0 milestone Sep 23, 2023
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch 10 times, most recently from 956d7af to 35d963b Compare September 25, 2023 02:49
@rafaelfranca rafaelfranca force-pushed the action-view-test-case-encoder branch from 35d963b to 0de3edb Compare September 27, 2023 01:06
@seanpdoyle seanpdoyle force-pushed the action-view-test-case-encoder branch from 0de3edb to 5894f31 Compare September 27, 2023 01:42
@@ -108,6 +189,26 @@ def include_helper_modules!
end
end

included do
class_attribute :content_class, instance_accessor: false, default: Class.new(Content)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca is this class a Content descendant for troubleshooting and naming purposes? Could it be default: Class.new(SimpleDelegator) directly, cutting out the Content class entirely?

Copy link
Member

@rafaelfranca rafaelfranca Sep 27, 2023

Choose a reason for hiding this comment

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

It doesn't need to be a descendant. I just changed that. But I prefer to have a name for debug purpose, and documentation so people can search. I just used the Ruby 3.3 set_temporary_name if it is present.

@rafaelfranca rafaelfranca force-pushed the action-view-test-case-encoder branch from 5894f31 to 14dfdae Compare September 27, 2023 02:00
Register a callable to decode rendered content for a given MIME type

Each registered decoder will also define a `#rendered.$MIME` helper
method, where `$MIME` corresponds to the value of the `mime` argument.

=== Arguments

`mime` - Symbol the MIME Type name for the rendered content
`callable` - Callable to decode the String. Accepts the String
                    value as its only argument
`block` - Block serves as the decoder when the
                 `callable` is omitted

By default, ActionView::TestCase defines a decoder for:

* :html - returns an instance of Nokogiri::XML::Node
* :json - returns an instance of ActiveSupport::HashWithIndifferentAccess

Each pre-registered decoder also defines a corresponding helper:

* :html - defines `rendered.html`
* :json - defines `rendered.json`

=== Examples

To parse the rendered content into RSS, register a call to `RSS::Parser.parse`:

```ruby
register_decoder :rss, -> rendered { RSS::Parser.parse(rendered) }

test "renders RSS" do
  article = Article.create!(title: "Hello, world")

  render formats: :rss, partial: article

  assert_equal "Hello, world", rendered.rss.items.last.title
end
```

To parse the rendered content into a Capybara::Simple::Node,
re-register an `:html` decoder with a call to
`Capybara.string`:

```ruby
register_decoder :html, -> rendered { Capybara.string(rendered) }

test "renders HTML" do
  article = Article.create!(title: "Hello, world")

  render partial: article

  rendered.html.assert_css "h1", text: "Hello, world"
end
```
@rafaelfranca rafaelfranca force-pushed the action-view-test-case-encoder branch from 14dfdae to 7badc42 Compare September 27, 2023 02:03
@rafaelfranca rafaelfranca merged commit 7893e0a into rails:main Sep 27, 2023
@seanpdoyle seanpdoyle deleted the action-view-test-case-encoder branch September 27, 2023 02:32
seanpdoyle added a commit to seanpdoyle/rails that referenced this pull request Oct 3, 2023
Based on a [comment on rails#49194][], describe how to achieve backwards
compatibility with existing `ActionView::TestCase` definitions.

[comment on rails#49194]: rails#49194 (comment)
kaspth added a commit to bullet-train-co/showcase that referenced this pull request Oct 9, 2023
Probably stemming from `rendered` having been converted to a proxy object in rails/rails#49194
kaspth added a commit to bullet-train-co/showcase that referenced this pull request Oct 9, 2023
* Rails 7.1: call `to_s` on `rendered` proxy

Probably stemming from `rendered` having been converted to a proxy object in rails/rails#49194

* CI with Rails 7.1

* Don't eager_load on CI

* Needs to be moved up further
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.

4 participants