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

Add cable_ready.integration_test_request_encoding initializer #300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tcannonfodder
Copy link

Type of PR

Enhancement

Description

Why should this be added

This makes it easier for folks to write tests for their CableReady actions, making the gem fit into the larger ecosystem

Checklist

  • My code follows the style guidelines of this project
  • Checks (StandardRB & Prettier-Standard) are passing

Copy link

netlify bot commented Aug 4, 2024

Deploy Preview for cableready ready!

Name Link
🔨 Latest commit 9a5ba14
🔍 Latest deploy log https://app.netlify.com/sites/cableready/deploys/67098a55c6132d0008b2029c
😎 Deploy Preview https://deploy-preview-300--cableready.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@kaspth
Copy link

kaspth commented Oct 11, 2024

@marcoroth heyo, anything I can do to help on this?

@marcoroth
Copy link
Member

marcoroth commented Oct 11, 2024

Hey @kaspth thanks for checking in! I couldn't apply the fix myself, but if @tcannonfodder could update the suggestion that would be great! Then I'm more than happy to merge this! Thanks! 🙌🏼

@tcannonfodder
Copy link
Author

Will do this weekend! Sorry, things have been a mess here

@marcoroth
Copy link
Member

Thanks @tcannonfodder, looks like standard is failing on your changes, if you could run the fix command and push up the changes that would be super appreciated! 🙌🏼

* This has been adapted from https://github.com/hotwired/turbo-rails/blob/9b17a3be3705786d72c3ae77fde5a9b3006555d7/lib/turbo/engine.rb#L119
* In order to streamline development, Rails integration tests should
	be able to support `as: :cable_ready` to test Cable Ready responses
	* This is done by setting up a new request encoding that
		ActionDispatch's integration tests can use. In this case, the
		encoder is barebones and is largely focused on streamlining how
		the request `Accept` headers are setup
@tcannonfodder tcannonfodder force-pushed the thomas/temp/2024-08-04-add-cable-ready-integration-test-request-encoding branch from e6c11f3 to 9a5ba14 Compare October 11, 2024 20:28
@tcannonfodder
Copy link
Author

I think I've got standardrb appeased (I am having issues with nio4r on my M1 Mac, so a bit blind here)

@kaspth
Copy link

kaspth commented Oct 11, 2024

Nice!

@kaspth
Copy link

kaspth commented Oct 12, 2024

I just looked at the latest RequestEncoder setup in Rails and I wonder if we should do something more akin to https://github.com/rails/rails/blob/20f5828cacf164f0f53d669b37ba1e4b0f0f8907/actionpack/lib/action_dispatch/testing/request_encoder.rb#L58 here.

Ref: rails/rails@0f4ab82

That way users can match on the operations JSON:

patch account_url(@account), params: { name: "Yo" }, as: :cable_ready

assert_pattern { response.parsed_body => {name:} } # Not quite sure what the test would look like, I'm not the most experienced with CableReady.

We could also do a custom response parser object, that would load the JSON, construct into objects that respond to deconstruct & deconstruct_keys and then parse the internal HTML via Rails::Dom::Testing.html_document.parse.

We could start with:

@encoders[:cable_ready] = CableReadyStreamEncoder.new \
  response_parser: ->(body) { JSON.parse(body, object_class: ActiveSupport::HashWithIndifferentAccess)

Also because of the way the request encoders are setup, ^ would only be called when the response acutally was a cable_ready response. An HTML fallback response would correctly use the HTML appropriate response parser.

@kaspth
Copy link

kaspth commented Oct 12, 2024

cc @seanpdoyle in case you have thoughts on extending the request encoder.

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

Successfully merging this pull request may close these issues.

3 participants