@@ -478,4 +478,62 @@ def post_address
478478 post :update , params : { state : "payment" }
479479 } . to change { order . line_items . to_a . size } . from ( 1 ) . to ( 0 )
480480 end
481+
482+ context 'trying to apply a coupon code' do
483+ let ( :order ) { create ( :order_with_line_items , state : 'payment' , guest_token : 'a token' ) }
484+ let ( :coupon_code ) { "coupon_code" }
485+
486+ before { cookies . signed [ :guest_token ] = order . guest_token }
487+
488+ context "when coupon code is empty" do
489+ let ( :coupon_code ) { "" }
490+
491+ it 'does not try to apply coupon code' do
492+ expect ( Spree ::PromotionHandler ::Coupon ) . not_to receive :new
493+
494+ put :update , params : { state : order . state , order : { coupon_code : coupon_code } }
495+
496+ expect ( response ) . to redirect_to ( spree . checkout_state_path ( 'confirm' ) )
497+ end
498+ end
499+
500+ context "when coupon code is applied" do
501+ let ( :promotion_handler ) { instance_double ( 'Spree::PromotionHandler::Coupon' , error : nil , success : 'Coupon Applied!' ) }
502+
503+ it "continues checkout flow normally" do
504+ expect ( Spree ::PromotionHandler ::Coupon )
505+ . to receive_message_chain ( :new , :apply )
506+ . and_return ( promotion_handler )
507+
508+ put :update , params : { state : order . state , order : { coupon_code : coupon_code } }
509+
510+ expect ( response ) . to render_template :edit
511+ expect ( flash . now [ :success ] ) . to eq ( 'Coupon Applied!' )
512+ end
513+
514+ context "when coupon code is not applied" do
515+ let ( :promotion_handler ) { instance_double ( 'Spree::PromotionHandler::Coupon' , error : 'Some error' , success : false ) }
516+
517+ it "setups the current step correctly before rendering" do
518+ expect ( Spree ::PromotionHandler ::Coupon )
519+ . to receive_message_chain ( :new , :apply )
520+ . and_return ( promotion_handler )
521+ expect ( controller ) . to receive ( :setup_for_current_state )
522+
523+ put :update , params : { state : order . state , order : { coupon_code : coupon_code } }
524+ end
525+
526+ it "render cart with coupon error" do
527+ expect ( Spree ::PromotionHandler ::Coupon )
528+ . to receive_message_chain ( :new , :apply )
529+ . and_return ( promotion_handler )
530+
531+ put :update , params : { state : order . state , order : { coupon_code : coupon_code } }
532+
533+ expect ( response ) . to render_template :edit
534+ expect ( flash . now [ :error ] ) . to eq ( 'Some error' )
535+ end
536+ end
537+ end
538+ end
481539end
0 commit comments