2016-02-12 14:58:39 +00:00
|
|
|
module EmailHelpers
|
2017-10-10 18:45:43 +00:00
|
|
|
def sent_to_user(user, recipients: email_recipients)
|
|
|
|
recipients.count { |to| to == user.notification_email }
|
2016-02-12 14:58:39 +00:00
|
|
|
end
|
|
|
|
|
2016-08-16 16:08:43 +00:00
|
|
|
def reset_delivered_emails!
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
end
|
|
|
|
|
2016-10-18 12:02:35 +00:00
|
|
|
def should_only_email(*users, kind: :to)
|
|
|
|
recipients = email_recipients(kind: kind)
|
2016-09-19 06:01:58 +00:00
|
|
|
|
2017-10-10 18:45:43 +00:00
|
|
|
users.each { |user| should_email(user, recipients: recipients) }
|
2016-09-19 06:01:58 +00:00
|
|
|
|
2016-08-16 16:08:43 +00:00
|
|
|
expect(recipients.count).to eq(users.count)
|
|
|
|
end
|
|
|
|
|
2017-10-10 18:45:43 +00:00
|
|
|
def should_email(user, times: 1, recipients: email_recipients)
|
|
|
|
expect(sent_to_user(user, recipients: recipients)).to eq(times)
|
2016-02-12 14:58:39 +00:00
|
|
|
end
|
|
|
|
|
2017-10-10 18:45:43 +00:00
|
|
|
def should_not_email(user, recipients: email_recipients)
|
|
|
|
should_email(user, times: 0)
|
2016-02-12 14:58:39 +00:00
|
|
|
end
|
2016-09-19 05:54:16 +00:00
|
|
|
|
2016-10-17 10:03:53 +00:00
|
|
|
def should_not_email_anyone
|
2016-09-19 05:54:16 +00:00
|
|
|
expect(ActionMailer::Base.deliveries).to be_empty
|
|
|
|
end
|
2016-09-28 07:39:54 +00:00
|
|
|
|
2016-10-18 12:02:35 +00:00
|
|
|
def email_recipients(kind: :to)
|
|
|
|
ActionMailer::Base.deliveries.flat_map(&kind)
|
2016-09-28 07:39:54 +00:00
|
|
|
end
|
2016-02-12 14:58:39 +00:00
|
|
|
end
|