Fix mailer specs brought from spree_core

This commit is contained in:
Luis Ramos
2020-07-14 14:19:19 +01:00
parent 5162964936
commit e96d9c1f45
3 changed files with 54 additions and 123 deletions

View File

@@ -5,113 +5,75 @@ require 'spec_helper'
describe Spree::OrderMailer do
include OpenFoodNetwork::EmailHelper
contect "original spree specs" do
let(:order) do
order = stub_model(Spree::Order)
product = stub_model(Spree::Product, name: %{The "BEST" product})
variant = stub_model(Spree::Variant, product: product)
price = stub_model(Spree::Price, variant: variant, amount: 5.00)
line_item = stub_model(Spree::LineItem, variant: variant, order: order, quantity: 1, price: 4.99)
variant.stub(default_price: price)
order.stub(line_items: [line_item])
order
end
context "basic behaviour" do
let(:order) { build(:order_with_totals_and_distribution) }
context ":from not set explicitly" do
it "falls back to spree config" do
message = Spree::OrderMailer.confirm_email_for_customer(order)
message.from.should == [Spree::Config[:mails_from]]
expect(message.from).to eq [Spree::Config[:mails_from]]
end
end
it "doesn't aggressively escape double quotes in confirmation body" do
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order)
confirmation_email.body.should_not include(""")
expect(confirmation_email.body).to_not include(""")
end
it "confirm_email_for_customer accepts an order id as an alternative to an Order object" do
Spree::Order.should_receive(:find).with(order.id).and_return(order)
lambda {
expect {
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order.id)
}.should_not raise_error
}.to_not raise_error
end
it "cancel_email accepts an order id as an alternative to an Order object" do
Spree::Order.should_receive(:find).with(order.id).and_return(order)
lambda {
expect {
cancel_email = Spree::OrderMailer.cancel_email(order.id)
}.should_not raise_error
}.to_not raise_error
end
end
context "only shows eligible adjustments in emails" do
let(:order) { create(:order_with_totals_and_distribution) }
before do
order.adjustments.create(
label: "Eligible Adjustment",
amount: 10,
eligible: true
)
order.adjustments.create!(
label: "Ineligible Adjustment",
amount: 0,
)
end
context "only shows eligible adjustments in emails" do
before do
order.adjustments.create(
label: "Eligible Adjustment",
amount: 10,
eligible: true
)
let!(:confirmation_email) { Spree::OrderMailer.confirm_email_for_customer(order) }
let!(:cancel_email) { Spree::OrderMailer.cancel_email(order) }
order.adjustments.create!(
label: "Ineligible Adjustment",
amount: -10,
eligible: false
)
end
let!(:confirmation_email) { Spree::OrderMailer.confirm_email(order) }
let!(:cancel_email) { Spree::OrderMailer.cancel_email(order) }
specify do
confirmation_email.body.should_not include("Ineligible Adjustment")
end
specify do
cancel_email.body.should_not include("Ineligible Adjustment")
end
specify do
expect(confirmation_email.body).to_not include("Ineligible Adjustment")
end
context "displays unit costs from line item" do
specify do
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order)
confirmation_email.body.should include("4.99")
confirmation_email.body.should_not include("5.00")
end
specify do
expect(cancel_email.body).to_not include("Ineligible Adjustment")
end
end
specify do
cancel_email = Spree::OrderMailer.cancel_email(order)
cancel_email.body.should include("4.99")
cancel_email.body.should_not include("5.00")
end
context "displays line item price" do
let(:order) { create(:order_with_totals_and_distribution) }
specify do
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order)
expect(confirmation_email.body).to include("3.00")
end
context "emails must be translatable" do
context "pt-BR locale" do
before do
pt_br_confirm_mail = { spree: { order_mailer: { confirm_email: { dear_customer: 'Caro Cliente,' } } } }
pt_br_cancel_mail = { spree: { order_mailer: { cancel_email: { order_summary_canceled: 'Resumo da Pedido [CANCELADA]' } } } }
I18n.backend.store_translations :'pt-BR', pt_br_confirm_mail
I18n.backend.store_translations :'pt-BR', pt_br_cancel_mail
I18n.locale = :'pt-BR'
end
after do
I18n.locale = I18n.default_locale
end
context "confirm_email" do
specify do
confirmation_email = Spree::OrderMailer.confirm_email_for_customer(order)
confirmation_email.body.should include("Caro Cliente,")
end
end
context "cancel_email" do
specify do
cancel_email = Spree::OrderMailer.cancel_email(order)
cancel_email.body.should include("Resumo da Pedido [CANCELADA]")
end
end
end
specify do
cancel_email = Spree::OrderMailer.cancel_email(order)
expect(cancel_email.body).to include("3.00")
end
end

View File

@@ -1,18 +1,14 @@
# frozen_string_literal: true
require 'spec_helper'
require 'email_spec'
describe Spree::ShipmentMailer do
include EmailSpec::Helpers
include EmailSpec::Matchers
let(:shipment) do
order = stub_model(Spree::Order)
product = stub_model(Spree::Product, name: %{The "BEST" product})
variant = stub_model(Spree::Variant, product: product)
line_item = stub_model(Spree::LineItem, variant: variant, order: order, quantity: 1, price: 5)
shipment = stub_model(Spree::Shipment)
order = build(:order)
product = build(:product, name: %{The "BEST" product})
variant = build(:variant, product: product)
line_item = build(:line_item, variant: variant, order: order, quantity: 1, price: 5)
shipment = build(:shipment)
shipment.stub(line_items: [line_item], order: order)
shipment.stub(tracking_url: "TRACK_ME")
shipment
@@ -21,43 +17,20 @@ describe Spree::ShipmentMailer do
context ":from not set explicitly" do
it "falls back to spree config" do
message = Spree::ShipmentMailer.shipped_email(shipment)
message.from.should == [Spree::Config[:mails_from]]
expect(message.from).to eq [Spree::Config[:mails_from]]
end
end
# Regression test for #2196
it "doesn't include out of stock in the email body" do
shipment_email = Spree::ShipmentMailer.shipped_email(shipment)
shipment_email.body.should_not include(%{Out of Stock})
expect(shipment_email.body).to_not include(%{Out of Stock})
end
it "shipment_email accepts an shipment id as an alternative to an Shipment object" do
Spree::Shipment.should_receive(:find).with(shipment.id).and_return(shipment)
lambda {
expect {
shipped_email = Spree::ShipmentMailer.shipped_email(shipment.id)
}.should_not raise_error
end
context "emails must be translatable" do
context "shipped_email" do
context "pt-BR locale" do
before do
pt_br_shipped_email = {
spree: { shipment_mailer: { shipped_email: { dear_customer: 'Caro Cliente,' } } }
}
I18n.backend.store_translations :'pt-BR', pt_br_shipped_email
I18n.locale = :'pt-BR'
end
after do
I18n.locale = I18n.default_locale
end
specify do
shipped_email = Spree::ShipmentMailer.shipped_email(shipment)
shipped_email.body.should include("Caro Cliente,")
end
end
end
}.to_not raise_error
end
end

View File

@@ -1,25 +1,21 @@
# frozen_string_literal: true
require 'spec_helper'
require 'email_spec'
describe Spree::TestMailer do
include EmailSpec::Helpers
include EmailSpec::Matchers
let(:user) { create(:user) }
context ":from not set explicitly" do
it "falls back to spree config" do
message = Spree::TestMailer.test_email(user)
message.from.should == [Spree::Config[:mails_from]]
expect(message.from).to eq [Spree::Config[:mails_from]]
end
end
it "confirm_email accepts a user id as an alternative to a User object" do
Spree.user_class.should_receive(:find).with(user.id).and_return(user)
lambda {
expect {
test_email = Spree::TestMailer.test_email(user.id)
}.should_not raise_error
}.to_not raise_error
end
end