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 accept_confirm if turbo is available so system tests don't fail. fixes #519 #520

Merged
merged 3 commits into from
Mar 19, 2025

Conversation

patriciomacadden
Copy link
Contributor

Generated tests starting failing after we added the data turbo confirm attribute to delete links.

This PR fixes that by monkey patching the test_unit/scaffold generator so after everything is generated it will wrap the click_on call with a accept_confirm call, only if turbo is defined.

will try to add test for this but wanted to confirm if the approach is something everyone would agree on.

Alternatives:

  • add source_paths << File.expand_path("templates", __dir__) and copy the system_test.rb.tt from rails and add a guard there to check if turbo is available instead of gsubing the file in the generator
  • remove data-turbo-confirm from the template.
  • leave as is (but generated tests will fail 😞)

Copy link
Member

@flavorjones flavorjones left a comment

Choose a reason for hiding this comment

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

I think this approach is the best option out of the ones you listed? I'd rather not have to maintain a copy of the system tests! I made one small suggestion to try to make the gsub_file less finicky (in case the upstream system tests change).

class ScaffoldGenerator < Base # :nodoc:
def fix_system_test
if turbo_defined?
gsub_file File.join("test/system", class_path, "#{file_name.pluralize}_test.rb"), "click_on \"Destroy this #{human_name.downcase}\", match: :first", "accept_confirm { click_on \"Destroy this #{human_name.downcase}\", match: :first }"
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this a bit less brittle (in the case where the exact text of the system test changes) by using a more permissive regular expression? Maybe something like

Suggested change
gsub_file File.join("test/system", class_path, "#{file_name.pluralize}_test.rb"), "click_on \"Destroy this #{human_name.downcase}\", match: :first", "accept_confirm { click_on \"Destroy this #{human_name.downcase}\", match: :first }"
gsub_file File.join("test/system", class_path, "#{file_name.pluralize}_test.rb"), /(click_on.*Destroy this.*)$/", "accept_confirm { \\1 }"

Copy link
Member

Choose a reason for hiding this comment

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

Actually I tested this change and it works, so I added a commit doing it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much nicer! thanks!

@patriciomacadden
Copy link
Contributor Author

Yes, I feel the same. I didn't want to maintain a copy of the system test just for this.

And looks much nicer with your version of the gsub!

@flavorjones
Copy link
Member

I also just added a very simple integration test to make sure the gsub applies cleanly. I don't think I'm ready to add a full Rails system test to CI. 😅

@flavorjones flavorjones merged commit 9a95ccd into rails:main Mar 19, 2025
17 checks passed
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.

None yet

2 participants