|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +# Reached only from `rake oneoff:send_promotion_rate_statistic`, which hands the output |
| 4 | +# of `PromotionRate.group_by_score_and_date` straight to the view -- string-keyed |
| 5 | +# hashes, not model instances. The examples use the same shape that task produces. |
| 6 | +RSpec.describe PromotionRate::StatisticMailer, type: :mailer do |
| 7 | + # ApplicationMailer captures the From address into default_params at class load, so |
| 8 | + # stubbing the config it reads would be too late. Override the stored default instead. |
| 9 | + before do |
| 10 | + allow(described_class).to receive(:default_params) |
| 11 | + .and_return(described_class.default_params.merge(from: "from@some.email")) |
| 12 | + end |
| 13 | + |
| 14 | + let(:start_date) { "17-09-2017" } |
| 15 | + let(:end_date) { "22-09-2017" } |
| 16 | + let(:objects) { [{"_id" => 3, "count" => 7}, {"_id" => 1, "count" => 2}] } |
| 17 | + let(:mail) { described_class.show("ops@flaredown.com", objects, start_date, end_date) } |
| 18 | + |
| 19 | + it "renders the headers" do |
| 20 | + expect(mail.subject).to eq I18n.t("promotion_rate.statistic.subject") |
| 21 | + expect(mail.to).to eq ["ops@flaredown.com"] |
| 22 | + expect(mail.from).to eq ["from@some.email"] |
| 23 | + end |
| 24 | + |
| 25 | + it "shows the date range and each score with its tally" do |
| 26 | + body = mail.body.encoded |
| 27 | + |
| 28 | + expect(body).to include "#{start_date} - #{end_date}" |
| 29 | + expect(body).to include "Amount:" |
| 30 | + expect(body).to match(/Score:\s*1/) |
| 31 | + expect(body).to match(/Score:\s*3/) |
| 32 | + end |
| 33 | + |
| 34 | + it "orders the scores ascending regardless of the order given" do |
| 35 | + scores = mail.body.encoded.scan(/Score:\s*(\d+)/).flatten |
| 36 | + |
| 37 | + expect(scores).to eq %w[1 3] |
| 38 | + end |
| 39 | +end |
0 commit comments