From fbc3fc6a510d676a78c560dea642ca5372c90171 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 22 Mar 2019 09:18:31 +0100 Subject: [PATCH] Test spree/admin/variants_controller #destroy --- .../admin/variants_controller_decorator.rb | 2 +- .../spree/admin/variants_controller_spec.rb | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/controllers/spree/admin/variants_controller_decorator.rb b/app/controllers/spree/admin/variants_controller_decorator.rb index 999ca0b22c..f93c6fcafc 100644 --- a/app/controllers/spree/admin/variants_controller_decorator.rb +++ b/app/controllers/spree/admin/variants_controller_decorator.rb @@ -15,7 +15,7 @@ Spree::Admin::VariantsController.class_eval do respond_with(@variant) do |format| format.html { redirect_to admin_product_variants_url(params[:product_id]) } - format.js { render_js_for_destroy } + format.js { render_js_for_destroy } end end diff --git a/spec/controllers/spree/admin/variants_controller_spec.rb b/spec/controllers/spree/admin/variants_controller_spec.rb index 36fd0463a2..d34444026d 100644 --- a/spec/controllers/spree/admin/variants_controller_spec.rb +++ b/spec/controllers/spree/admin/variants_controller_spec.rb @@ -35,6 +35,58 @@ module Spree assigns(:variants).should match_array [v1, v2] end end + + describe '#destroy' do + let(:variant) { create(:variant) } + + context 'when requesting with js' do + before do + allow(Spree::Variant).to receive(:find).with(variant.id.to_s) { variant } + allow(variant).to receive(:delete) + end + + it 'deletes the variant' do + spree_delete :destroy, id: variant.id, product_id: variant.product.permalink, format: 'js' + expect(variant).to have_received(:delete) + end + + it 'shows a success flash message' do + spree_delete :destroy, id: variant.id, product_id: variant.product.permalink, format: 'js' + expect(flash[:success]).to eq(I18n.t('notice_messages.variant_deleted')) + end + + it 'renders spree/admin/shared/destroy' do + spree_delete :destroy, id: variant.id, product_id: variant.product.permalink, format: 'js' + expect(response).to render_template('spree/admin/shared/_destroy') + end + end + + context 'when requesting with html' do + before do + allow(Spree::Variant).to receive(:find).with(variant.id.to_s) { variant } + allow(variant).to receive(:delete) + end + + it 'deletes the variant' do + spree_delete :destroy, id: variant.id, product_id: variant.product.permalink, format: 'html' + expect(variant).to have_received(:delete) + end + + it 'shows a success flash message' do + spree_delete :destroy, id: variant.id, product_id: variant.product.permalink, format: 'html' + expect(flash[:success]).to eq(I18n.t('notice_messages.variant_deleted')) + end + + it 'redirects to admin_product_variants_url' do + spree_delete :destroy, id: variant.id, product_id: variant.product.permalink, format: 'html' + expect(response).to redirect_to( + controller: 'spree/admin/variants', + action: :index, + product_id: variant.product.permalink + ) + end + end + end end end end