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 email helper methods to spec helper #1125

Open
stevepolitodesign opened this issue Apr 2, 2023 · 1 comment
Open

Add email helper methods to spec helper #1125

stevepolitodesign opened this issue Apr 2, 2023 · 1 comment
Labels
Enhancement Features we're considering adding

Comments

@stevepolitodesign
Copy link
Contributor

stevepolitodesign commented Apr 2, 2023

When writing integration and/or system tests, I want to be able to easily test mailer links so that my developer experience is improved.

Acceptance Criteria

  • Create a helper method that returns the latest email for an email address.
  • Create a helper method that makes it so we can click a link in an email.
@stevepolitodesign
Copy link
Contributor Author

I think it would be a nice addition if we added current_email and email_link helper methods for testing. Below is are the proposed helper methods inspired from a personal post. Similar to capybara-email

def open_latest_email_for(email_address)
  @current_email = ActionMailer::Base.deliveries.reverse.detect do |email|
    email.to.include?(email_address)
  end
end

def current_email
  @current_email ||= ActionMailer::Base.deliveries.last
end

def email_link(email, string)
  document = Capybara.string(email.body.to_s)
  link = document.find(:link, string)[:href]

  localize_link(link)
end

private

def localize_link(link)
  uri = URI.parse(link)

  if uri.query
    "#{uri.path}?#{uri.query}"
  else
    uri.path
  end
end

@stevepolitodesign stevepolitodesign added the Enhancement Features we're considering adding label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Features we're considering adding
Projects
None yet
Development

No branches or pull requests

1 participant