forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoupon_code_spec.rb
224 lines (184 loc) · 7.42 KB
/
coupon_code_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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require 'spec_helper'
describe "Coupon code promotions", type: :feature, js: true do
let!(:store) { create(:store) }
let!(:country) { create(:country, name: "United States of America", states_required: true) }
let!(:state) { create(:state, name: "Alabama", country: country) }
let!(:zone) { create(:zone) }
let!(:shipping_method) { create(:shipping_method) }
let!(:payment_method) { create(:check_payment_method) }
let!(:product) { create(:product, name: "RoR Mug", price: 20) }
context "visitor makes checkout as guest without registration" do
def create_basic_coupon_promotion(code)
promotion = create(
:promotion,
name: code.titleize,
code: code,
starts_at: 1.day.ago,
expires_at: 1.day.from_now
)
calculator = Spree::Calculator::FlatRate.new
calculator.preferred_amount = 10
action = Spree::Promotion::Actions::CreateItemAdjustments.new
action.calculator = calculator
action.promotion = promotion
action.save
promotion.reload # so that promotion.actions is available
end
let!(:promotion) { create_basic_coupon_promotion("onetwo") }
# OrdersController
context "on the payment page" do
before do
visit spree.root_path
click_link "RoR Mug"
click_button "add-to-cart-button"
click_button "Checkout"
fill_in "order_email", with: "spree@example.com"
fill_in "First Name", with: "John"
fill_in "Last Name", with: "Smith"
fill_in "Street Address", with: "1 John Street"
fill_in "City", with: "City of John"
fill_in "Zip", with: "01337"
select country.name, from: "Country"
select state.name, from: "order[bill_address_attributes][state_id]"
fill_in "Phone", with: "555-555-5555"
# To shipping method screen
click_button "Save and Continue"
# To payment screen
click_button "Save and Continue"
end
it "informs about an invalid coupon code" do
fill_in "order_coupon_code", with: "coupon_codes_rule_man"
click_button "Save and Continue"
expect(page).to have_content(I18n.t('spree.coupon_code_not_found'))
end
it "can enter an invalid coupon code, then a real one" do
fill_in "order_coupon_code", with: "coupon_codes_rule_man"
click_button "Save and Continue"
expect(page).to have_content(I18n.t('spree.coupon_code_not_found'))
fill_in "order_coupon_code", with: "onetwo"
click_button "Save and Continue"
expect(page).to have_content("Promotion (Onetwo) -$10.00")
end
context "with a promotion" do
it "applies a promotion to an order" do
fill_in "order_coupon_code", with: "onetwo"
click_button "Save and Continue"
expect(page).to have_content("Promotion (Onetwo) -$10.00")
end
end
end
# CheckoutController
context "on the cart page" do
before do
visit spree.root_path
click_link "RoR Mug"
click_button "add-to-cart-button"
end
it "can enter a coupon code and receives success notification" do
fill_in "order_coupon_code", with: "onetwo"
click_button "Update"
expect(page).to have_content(I18n.t('spree.coupon_code_applied'))
end
it "can enter a promotion code with both upper and lower case letters" do
fill_in "order_coupon_code", with: "ONETwO"
click_button "Update"
expect(page).to have_content(I18n.t('spree.coupon_code_applied'))
end
it "informs the user about a coupon code which has exceeded its usage" do
expect_any_instance_of(Spree::Promotion).to receive(:usage_limit_exceeded?).and_return(true)
fill_in "order_coupon_code", with: "onetwo"
click_button "Update"
expect(page).to have_content(I18n.t('spree.coupon_code_max_usage'))
end
context "informs the user if the coupon code is not eligible" do
before do
rule = Spree::Promotion::Rules::ItemTotal.new
rule.promotion = promotion
rule.preferred_amount = 100
rule.save
end
specify do
visit spree.cart_path
fill_in "order_coupon_code", with: "onetwo"
click_button "Update"
expect(page).to have_content(I18n.t(:item_total_less_than_or_equal, scope: [:spree, :eligibility_errors, :messages], amount: "$100.00"))
end
end
it "informs the user if the coupon is expired" do
promotion.expires_at = Date.today.beginning_of_week
promotion.starts_at = Date.today.beginning_of_week.advance(day: 3)
promotion.save!
fill_in "order_coupon_code", with: "onetwo"
click_button "Update"
expect(page).to have_content(I18n.t('spree.coupon_code_expired'))
end
context "calculates the correct amount of money saved with flat percent promotions" do
before do
calculator = Spree::Calculator::FlatPercentItemTotal.new
calculator.preferred_flat_percent = 20
promotion.actions.first.calculator = calculator
promotion.save
create(:product, name: "Spree Mug", price: 10)
end
specify do
visit spree.root_path
click_link "Spree Mug"
click_button "add-to-cart-button"
visit spree.cart_path
fill_in "order_coupon_code", with: "onetwo"
click_button "Update"
fill_in "order_line_items_attributes_0_quantity", with: 2
fill_in "order_line_items_attributes_1_quantity", with: 2
click_button "Update"
within '#cart_adjustments' do
# 20% of $40 = 8
# 20% of $20 = 4
# Therefore: promotion discount amount is $12.
expect(page).to have_content("Promotion (Onetwo) -$12.00")
end
within '.cart-total' do
expect(page).to have_content("$48.00")
end
end
end
context "calculates the correct amount of money saved with flat 100% promotions on the whole order" do
before do
calculator = Spree::Calculator::FlatPercentItemTotal.new
calculator.preferred_flat_percent = 100
promotion.promotion_actions.first.discard
Spree::Promotion::Actions::CreateAdjustment.create!(
calculator: calculator,
promotion: promotion
)
create(:product, name: "Spree Mug", price: 10)
end
specify do
visit spree.root_path
click_link "Spree Mug"
click_button "add-to-cart-button"
visit spree.cart_path
within '.cart-total' do
expect(page).to have_content("$30.00")
end
fill_in "order_coupon_code", with: "onetwo"
click_button "Update"
within '#cart_adjustments' do
expect(page).to have_content("Promotion (Onetwo) -$30.00")
end
within '.cart-total' do
expect(page).to have_content("$0.00")
end
fill_in "order_line_items_attributes_0_quantity", with: 2
fill_in "order_line_items_attributes_1_quantity", with: 2
click_button "Update"
within '#cart_adjustments' do
expect(page).to have_content("Promotion (Onetwo) -$60.00")
end
within '.cart-total' do
expect(page).to have_content("$0.00")
end
end
end
end
end
end