@@ -148,6 +148,79 @@ def compute_line_item(_line_item, _options) = 1
148148 end
149149 end
150150
151+ describe "inherited hook" do
152+ context "for a well-formed benefit" do
153+ subject ( :benefit ) do
154+ Class . new ( described_class ) do
155+ def discount_line_item ( _line_item , _options = { } )
156+ true
157+ end
158+ end
159+ end
160+
161+ it "does not emit a deprecation warning" do
162+ expect ( Spree . deprecator ) . not_to receive ( :warn )
163+ benefit
164+ end
165+ end
166+
167+ context "for a legacy benefit" do
168+ subject ( :benefit ) do
169+ Class . new ( described_class ) do
170+ def self . name
171+ "LegacyBenefit"
172+ end
173+
174+ def discount ( _line_item , _options = { } )
175+ true
176+ end
177+ end
178+ end
179+
180+ it "emits a deprecation warning" do
181+ expect ( Spree . deprecator ) . to receive ( :warn ) . with ( <<~MSG )
182+ Please refactor `LegacyBenefit`. You're defining `#discount`. Instead, define a method for each type of discountable
183+ that your benefit can discount. For example:
184+ ```
185+ class MyBenefit < SolidusPromotions::Benefit
186+ def can_discount?(discountable)
187+ discountable.is_a?(Spree::LineItem)
188+ end
189+
190+ def discount(order, _options = {})
191+ amount = compute_amount(line_item, ...)
192+ return if amount.zero?
193+
194+ ItemDiscount.new(
195+ item: line_item,
196+ label: adjustment_label(line_item),
197+ amount: amount,
198+ source: self
199+ )
200+ end
201+ ```
202+ can now become
203+ ```
204+ class MyBenefit < SolidusPromotions::Benefit
205+ def discount_line_item?(order, ...)
206+ amount = compute_amount(line_item, ...)
207+ return if amount.zero?
208+
209+ ItemDiscount.new(
210+ item: line_item,
211+ label: adjustment_label(line_item),
212+ amount: amount,
213+ source: self
214+ )
215+ end
216+ end
217+ ```
218+ MSG
219+ benefit
220+ end
221+ end
222+ end
223+
151224 describe ".original_promotion_action" do
152225 let ( :spree_promotion ) { create :promotion , :with_adjustable_action }
153226 let ( :spree_promotion_action ) { spree_promotion . actions . first }
0 commit comments