forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
minimum_quantity_spec.rb
110 lines (82 loc) · 2.79 KB
/
minimum_quantity_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Spree::Promotion::Rules::MinimumQuantity do
subject(:quantity_rule) { described_class.new(preferred_minimum_quantity: 2) }
describe "#valid?" do
let(:promotion) { build(:promotion) }
before { promotion.rules << quantity_rule }
it { is_expected.to be_valid }
context "when minimum quantity is zero" do
subject(:quantity_rule) { described_class.new(preferred_minimum_quantity: 0) }
it { is_expected.not_to be_valid }
end
end
describe "#applicable?" do
subject { quantity_rule.applicable?(promotable) }
context "when promotable is an order" do
let(:promotable) { Spree::Order.new }
it { is_expected.to be true }
end
context "when promotable is a line item" do
let(:promotable) { Spree::LineItem.new }
it { is_expected.to be false }
end
end
describe "#eligible?" do
subject { quantity_rule.eligible?(order) }
let(:order) {
create(
:order_with_line_items,
line_items_count: line_items.length,
line_items_attributes: line_items
)
}
let(:promotion) { build(:promotion) }
before { promotion.rules << quantity_rule }
context "when only the quantity rule is applied" do
context "when the quantity is less than the minimum" do
let(:line_items) { [{ quantity: 1 }] }
it { is_expected.to be false }
end
context "when the quantity is equal to the minimum" do
let(:line_items) { [{ quantity: 2 }] }
it { is_expected.to be true }
end
context "when the quantity is greater than the minimum" do
let(:line_items) { [{ quantity: 4 }] }
it { is_expected.to be true }
end
end
context "when another rule limits the applicable items" do
let(:variant_1) { create(:variant) }
let(:variant_2) { create(:variant) }
let(:variant_3) { create(:variant) }
let(:product_rule) {
Spree::Promotion::Rules::Product.new(
products: [variant_1.product, variant_2.product],
preferred_match_policy: "any"
)
}
before { promotion.rules << product_rule }
context "when the applicable quantity is less than the minimum" do
let(:line_items) do
[
{ variant: variant_1, quantity: 1 },
{ variant: variant_3, quantity: 1 }
]
end
it { is_expected.to be false }
end
context "when the applicable quantity is greater than the minimum" do
let(:line_items) do
[
{ variant: variant_1, quantity: 1 },
{ variant: variant_2, quantity: 1 },
{ variant: variant_3, quantity: 1 }
]
end
it { is_expected.to be true }
end
end
end
end