This repository was archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproduct_spec.rb
More file actions
208 lines (158 loc) · 6.75 KB
/
Copy pathproduct_spec.rb
File metadata and controls
208 lines (158 loc) · 6.75 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true
require "rails_helper"
RSpec.describe SolidusFriendlyPromotions::Conditions::Product, type: :model do
let(:condition_options) { {} }
let(:condition) { described_class.new(condition_options) }
it { is_expected.to have_many(:products) }
describe "#applicable?" do
let(:promotable) { Spree::Order.new }
subject { condition.applicable?(promotable) }
it { is_expected.to be true }
context "with a line item" do
let(:promotable) { Spree::LineItem.new }
it { is_expected.to be true }
context "with line item applicable set to false" do
let(:condition_options) { {preferred_line_item_applicable: false} }
it { is_expected.to be false }
end
end
context "with a shipment" do
let(:promotable) { Spree::Shipment.new }
it { is_expected.to be false }
end
end
describe "#eligible?(order)" do
let(:order) { create(:order) }
let(:product_one) { create(:product) }
let(:product_two) { create(:product) }
let(:product_three) { create(:product) }
let(:order_products) { [] }
let(:eligible_products) { [] }
before do
order_products.each do |product|
order.contents.add(product.master, 1)
end
condition.products = eligible_products
end
it "is eligible if there are no products" do
expect(condition).to be_eligible(order)
end
context "with 'any' match policy" do
let(:condition_options) { super().merge(preferred_match_policy: "any") }
let(:order_products) { [product_one, product_two] }
let(:eligible_products) { [product_two, product_three] }
it "is eligible if any of the products is in eligible products" do
expect(condition).to be_eligible(order)
end
context "when none of the products are eligible products" do
let(:order_products) { [product_one] }
it { expect(condition).not_to be_eligible(order) }
it "sets an error message" do
condition.eligible?(order)
expect(condition.eligibility_errors.full_messages.first)
.to eq "You need to add an applicable product before applying this coupon code."
end
it "sets an error code" do
condition.eligible?(order)
expect(condition.eligibility_errors.details[:base].first[:error_code])
.to eq :no_applicable_products
end
end
end
context "with 'all' match policy" do
let(:condition_options) { super().merge(preferred_match_policy: "all") }
let(:order_products) { [product_three, product_two, product_one] }
let(:eligible_products) { [product_two, product_three] }
it "is eligible if all of the eligible products are ordered" do
expect(condition).to be_eligible(order)
end
context "when any of the eligible products is not ordered" do
let(:order_products) { [product_one, product_two] }
let(:eligible_products) { [product_one, product_two, product_three] }
it { expect(condition).not_to be_eligible(order) }
it "sets an error message" do
condition.eligible?(order)
expect(condition.eligibility_errors.full_messages.first).to eq(
"This coupon code can't be applied because you don't have all of the necessary products in your cart."
)
end
it "sets an error code" do
condition.eligible?(order)
expect(condition.eligibility_errors.details[:base].first[:error_code])
.to eq :missing_product
end
end
end
context "with 'none' match policy" do
let(:condition_options) { super().merge(preferred_match_policy: "none") }
let(:order_products) { [product_one] }
let(:eligible_products) { [product_two, product_three] }
it "is eligible if none of the order's products are in eligible products" do
expect(condition).to be_eligible(order)
end
context "when any of the order's products are in eligible products" do
let(:order_products) { [product_one, product_two] }
let(:eligible_products) { [product_two, product_three] }
it { expect(condition).not_to be_eligible(order) }
it "sets an error message" do
condition.eligible?(order)
expect(condition.eligibility_errors.full_messages.first)
.to eq "Your cart contains a product that prevents this coupon code from being applied."
end
it "sets an error code" do
condition.eligible?(order)
expect(condition.eligibility_errors.details[:base].first[:error_code])
.to eq :has_excluded_product
end
end
end
context "with 'only' match policy" do
let(:condition_options) { super().merge(preferred_match_policy: "only") }
let(:order_products) { [product_one] }
let(:eligible_products) { [product_one] }
it "is eligible if all of the order's products are in eligible products" do
expect(condition).to be_eligible(order)
end
context "if none of the order's products are in eligible products" do
let(:eligible_products) { [product_two, product_three] }
it "is not eligible" do
expect(condition).not_to be_eligible(order)
end
end
context "when any of the order's products are in eligible products" do
let(:order_products) { [product_one, product_two] }
let(:eligible_products) { [product_two, product_three] }
it { expect(condition).not_to be_eligible(order) }
it "sets an error message" do
condition.eligible?(order)
expect(condition.eligibility_errors.full_messages.first)
.to eq "Your cart contains a product that prevents this coupon code from being applied."
end
it "sets an error code" do
condition.eligible?(order)
expect(condition.eligibility_errors.details[:base].first[:error_code])
.to eq :has_excluded_product
end
end
end
end
describe "#eligible?(line_item)" do
subject { condition.eligible?(line_item) }
let(:condition_line_item) { Spree::LineItem.new(product: condition_product) }
let(:other_line_item) { Spree::LineItem.new(product: other_product) }
let(:condition_options) { super().merge(products: [condition_product]) }
let(:condition_product) { mock_model(Spree::Product) }
let(:other_product) { mock_model(Spree::Product) }
it "is eligible if there are no products" do
expect(condition).to be_eligible(condition_line_item)
end
context "for product in condition" do
let(:line_item) { condition_line_item }
it { is_expected.to be_truthy }
end
context "for product not in condition" do
let(:line_item) { other_line_item }
it { is_expected.to be_falsey }
end
end
end