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