From 897596de5f5987e46bed55f2902115c9853310e6 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 24 May 2024 02:45:26 +0500 Subject: [PATCH] remove delete methods from products reflex --- app/reflexes/products_reflex.rb | 35 --------- spec/reflexes/products_reflex_spec.rb | 100 -------------------------- 2 files changed, 135 deletions(-) delete mode 100644 spec/reflexes/products_reflex_spec.rb diff --git a/app/reflexes/products_reflex.rb b/app/reflexes/products_reflex.rb index 4c8aabc0d2..7d107af4a4 100644 --- a/app/reflexes/products_reflex.rb +++ b/app/reflexes/products_reflex.rb @@ -21,34 +21,6 @@ class ProductsReflex < ApplicationReflex fetch_and_render_products_with_flash end - def delete_product - id = current_id_from_element(element) - product = product_finder(id).find_product - authorize! :delete, product - - if product.destroy - flash[:success] = I18n.t('admin.products_v3.delete_product.success') - else - flash[:error] = I18n.t('admin.products_v3.delete_product.error') - end - - fetch_and_render_products_with_flash - end - - def delete_variant - id = current_id_from_element(element) - variant = Spree::Variant.active.find(id) - authorize! :delete, variant - - if VariantDeleter.new.delete(variant) - flash[:success] = I18n.t('admin.products_v3.delete_variant.success') - else - flash[:error] = I18n.t('admin.products_v3.delete_variant.error') - end - - fetch_and_render_products_with_flash - end - private def init_filters_params @@ -203,11 +175,4 @@ class ProductsReflex < ApplicationReflex .to_h.with_indifferent_access end - def product_finder(id) - ProductScopeQuery.new(current_user, { id: }) - end - - def current_id_from_element(element) - element.dataset.current_id - end end diff --git a/spec/reflexes/products_reflex_spec.rb b/spec/reflexes/products_reflex_spec.rb deleted file mode 100644 index f0c74f2641..0000000000 --- a/spec/reflexes/products_reflex_spec.rb +++ /dev/null @@ -1,100 +0,0 @@ -# frozen_string_literal: true - -require "reflex_helper" - -RSpec.describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do - let(:current_user) { create(:admin_user) } # todo: set up an enterprise user to test permissions - let(:context) { - { url: admin_products_url, connection: { current_user: } } - } - let(:flash) { {} } - - before do - pending "fix spec" - # Mock flash, because stimulus_reflex_testing doesn't support sessions - allow_any_instance_of(described_class).to receive(:flash).and_return(flash) - end - - describe '#delete_product' do - let(:product) { create(:simple_product) } - let(:action_name) { :delete_product } - - subject { build_reflex(method_name: action_name, **context) } - - before { subject.element.dataset.current_id = product.id } - - context 'given that the current user is admin' do - let(:current_user) { create(:admin_user) } - - it 'should successfully delete the product' do - subject.run(action_name) - product.reload - expect(product.deleted_at).not_to be_nil - expect(flash[:success]).to eq('Successfully deleted the product') - end - - it 'should be failed to delete the product' do - # mock db query failure - allow_any_instance_of(Spree::Product).to receive(:destroy).and_return(false) - subject.run(action_name) - product.reload - expect(product.deleted_at).to be_nil - expect(flash[:error]).to eq('Unable to delete the product') - end - end - - context 'given that the current user is not admin' do - let(:current_user) { create(:user) } - - it 'should raise the access denied exception' do - expect { subject.run(action_name) }.to raise_exception(CanCan::AccessDenied) - end - end - end - - describe '#delete_variant' do - let(:variant) { create(:variant) } - let(:action_name) { :delete_variant } - - subject { build_reflex(method_name: action_name, **context) } - - before { subject.element.dataset.current_id = variant.id } - - context 'given that the current user is admin' do - let(:current_user) { create(:admin_user) } - - it 'should successfully delete the variant' do - subject.run(action_name) - variant.reload - expect(variant.deleted_at).not_to be_nil - expect(flash[:success]).to eq('Successfully deleted the variant') - end - - it 'should be failed to delete the product' do - # mock db query failure - allow_any_instance_of(Spree::Variant).to receive(:destroy).and_return(false) - subject.run(action_name) - variant.reload - expect(variant.deleted_at).to be_nil - expect(flash[:error]).to eq('Unable to delete the variant') - end - end - - context 'given that the current user is not admin' do - let(:current_user) { create(:user) } - - it 'should raise the access denied exception' do - expect { subject.run(action_name) }.to raise_exception(CanCan::AccessDenied) - end - end - end -end - -# Build and run a reflex using the context -# Parameters can be added with params: option -# For more options see https://github.com/podia/stimulus_reflex_testing#usage -def run_reflex(method_name, opts = {}) - build_reflex(method_name:, **context.merge(opts)).tap{ |reflex| - reflex.run(method_name) - } -end