forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This integration spec comes from the experimental PR solidusio#4296. That PR will very probably not be merged, so I'm moving it here as a vantage point.
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
friendly_promotions/spec/models/promotion/integration_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe "Promotion System" do | ||
context "A promotion that creates line item adjustments" do | ||
let(:shirt) { create(:product) } | ||
let(:pants) { create(:product) } | ||
let(:promotion) { create(:promotion, name: "20% off Shirts", apply_automatically: true) } | ||
let(:order) { create(:order) } | ||
|
||
before do | ||
promotion.rules << rule | ||
promotion.actions << action | ||
order.contents.add(shirt.master, 1) | ||
order.contents.add(pants.master, 1) | ||
end | ||
|
||
context "with an order-level rule" do | ||
let(:rule) { Spree::Promotion::Rules::Product.new(products: [shirt]) } | ||
|
||
context "with an order level action" do | ||
let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 20) } | ||
let(:action) { Spree::Promotion::Actions::CreateAdjustment.new(calculator: calculator) } | ||
|
||
it "creates one order-level adjustment" do | ||
expect(order.adjustments.length).to eq(1) | ||
expect(order.total).to eq(31.98) | ||
expect(order.item_total).to eq(39.98) | ||
# This is wrong! But order level adjustments can't work any other way | ||
expect(order.item_total_before_tax).to eq(39.98) | ||
expect(order.line_items.flat_map(&:adjustments)).to be_empty | ||
end | ||
end | ||
|
||
context "with an line item level action" do | ||
let(:calculator) { Spree::Calculator::PercentOnLineItem.new(preferred_percent: 20) } | ||
let(:action) { Spree::Promotion::Actions::CreateItemAdjustments.new(calculator: calculator) } | ||
|
||
it "creates one order-level adjustment" do | ||
pending | ||
expect(order.adjustments).to be_empty | ||
expect(order.total).to eq(31.98) | ||
expect(order.item_total).to eq(39.98) | ||
expect(order.item_total_before_tax).to eq(31.98) | ||
expect(order.line_items.flat_map(&:adjustments).length).to eq(2) | ||
end | ||
end | ||
end | ||
|
||
context "with a line-item level rule" do | ||
let(:rule) { Spree::Promotion::Rules::LineItemProduct.new(products: [shirt]) } | ||
|
||
context "with an order level action" do | ||
let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 20) } | ||
let(:action) { Spree::Promotion::Actions::CreateAdjustment.new(calculator: calculator) } | ||
|
||
it "creates one order-level adjustment" do | ||
pending | ||
# Whoops - this works because line item level rules don't affect order-level actions :( | ||
expect(order.adjustments.length).to eq(1) | ||
expect(order.total).to eq(31.98) | ||
expect(order.item_total).to eq(39.98) | ||
# This is wrong! But order level adjustments can't work any other way | ||
expect(order.item_total_before_tax).to eq(39.98) | ||
expect(order.line_items.flat_map(&:adjustments)).to be_empty | ||
end | ||
end | ||
|
||
context "with an line item level action" do | ||
let(:calculator) { Spree::Calculator::PercentOnLineItem.new(preferred_percent: 20) } | ||
let(:action) { Spree::Promotion::Actions::CreateItemAdjustments.new(calculator: calculator) } | ||
|
||
it "creates one line item level adjustment" do | ||
pending | ||
expect(order.adjustments).to be_empty | ||
expect(order.total).to eq(35.98) | ||
expect(order.item_total).to eq(39.98) | ||
expect(order.item_total_before_tax).to eq(35.98) | ||
expect(order.line_items.flat_map(&:adjustments).length).to eq(1) | ||
end | ||
end | ||
end | ||
end | ||
end |