From 19768e1398791795885fe1229ef33093d8bcc909 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Mon, 12 Apr 2021 20:49:39 +0100 Subject: [PATCH 1/2] adds coverage for product deletion and cloning --- spec/features/admin/products_spec.rb | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 9e4544f657..787d99649d 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -114,6 +114,82 @@ feature ' expect(page).to have_content "Unit value can't be blank" end end + + describe "soft-deleting" do + let!(:product1) { create(:simple_product, name: 'a product to keep', supplier: @supplier) } + + context 'a simple product', js: true do + let!(:product2) { create(:simple_product, name: 'a product to delete', supplier: @supplier) } + + before do + login_as_admin_and_visit spree.admin_products_path + + within "tr#p_#{product2.id}" do + accept_alert { page.find("[data-powertip=Remove]").click } + end + end + + it 'removes it from the product list' do + expect(page).not_to have_selector "tr#p_#{product2.id}" + expect(page).to have_selector "tr#p_#{product1.id}" + end + end + + context 'a shipped product', js: true do + let!(:order) { create(:shipped_order, line_items_count: 1) } + let!(:line_item) { order.reload.line_items.first } + + before do + login_as_admin_and_visit spree.admin_products_path + + within "tr#p_#{order.variants.first.product_id}" do + accept_alert { page.find("[data-powertip=Remove]").click } + end + end + + it 'removes it from the product list' do + expect(page).to have_selector "tr#p_#{product1.id}" + expect(page).not_to have_selector "tr#p_#{order.variants.first.product_id}" + expect(Spree::Product.count).to eq 1 + expect(order.line_items.count).to eq 1 + end + + it 'keeps the line item on the order (admin)' do + visit spree.admin_orders_path + find(".icon-edit").click + expect(page).to have_content(line_item.product.name.to_s) + end + end + end + + describe 'cloning' do + let!(:product1) { create(:simple_product, name: 'a weight product', supplier: @supplier, variant_unit: "weight") } + let!(:variant1) { create(:variant, product_id: product1.id, display_name: 'kg oranges') } + + context 'products', js: true do + before { login_as_admin_and_visit spree.admin_products_path } + + it 'creates a copy of the product' do + within "tr#p_#{product1.id}" do + page.find("[data-powertip=Clone]").click + end + expect(page).to have_selector "tr#p_#{variant1.product_id}" + + within "tr#p_#{product1.id + 1}" do + expect(page).to have_input "product_name", with: 'COPY OF a weight product' + end + + # bug #660 + # only the first variant is duplicated + expect(Spree::Product.second.variants.count).to eq 1 + # TODO expect(Spree::Product.second.variants.count).to eq Spree::Product.first.variants.count + + # the stock of the cloned product is always zero + expect(Spree::Product.second.on_hand).to eq 0 + # TODO expect(Spree::Product.second.on_hand).to eq Spree::Product.first.on_hand + end + end + end context "as an enterprise user" do let!(:tax_category) { create(:tax_category) } From a476416dc4ad8c693a235a7eb964c93e482b1ced Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Fri, 23 Apr 2021 14:38:59 +0100 Subject: [PATCH 2/2] Removes html tr tag; Clarifies it block --- .../api/v0/products_controller_spec.rb | 22 ++++++++++ spec/features/admin/products_spec.rb | 40 +++++++------------ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/spec/controllers/api/v0/products_controller_spec.rb b/spec/controllers/api/v0/products_controller_spec.rb index 8af0985e48..e55152cd19 100644 --- a/spec/controllers/api/v0/products_controller_spec.rb +++ b/spec/controllers/api/v0/products_controller_spec.rb @@ -164,6 +164,7 @@ describe Api::V0::ProductsController, type: :controller do context 'as an enterprise user' do let(:current_api_user) { supplier_enterprise_user(supplier) } + let!(:variant) { create(:variant, product_id: product.id) } it 'responds with a successful response' do spree_post :clone, product_id: product.id, format: :json @@ -183,6 +184,27 @@ describe Api::V0::ProductsController, type: :controller do expect(response.status).to eq(201) expect(json_response['name']).to eq("COPY OF #{product_with_image.name}") end + + # test cases related to bug #660: product duplication clones master variant + + # stock info - clone is set to zero + it '(does not) clone the stock info of the product' do + spree_post :clone, product_id: product.id, format: :json + expect(json_response['on_hand']).to eq(0) + end + + # variants: only the master variant of the product is cloned + it '(does not) clone variants from a product with several variants' do + spree_post :clone, product_id: product.id, format: :json + expect(Spree::Product.second.variants.count).not_to eq Spree::Product.first.variants.count + end + + #price info: it does not consider price changes; it considers the price set upon product creation + it '(does not) clone price which was updated' do + product.update_attribute(:price, 2.22) + spree_post :clone, product_id: product.id, format: :json + expect(json_response['price']).not_to eq(2.22) + end end context 'as an administrator' do diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 787d99649d..080fd74702 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -115,43 +115,42 @@ feature ' end end - describe "soft-deleting" do + describe "deleting", js: true do let!(:product1) { create(:simple_product, name: 'a product to keep', supplier: @supplier) } - context 'a simple product', js: true do + context 'a simple product' do let!(:product2) { create(:simple_product, name: 'a product to delete', supplier: @supplier) } before do login_as_admin_and_visit spree.admin_products_path - within "tr#p_#{product2.id}" do + within "#p_#{product2.id}" do accept_alert { page.find("[data-powertip=Remove]").click } end + visit current_path end it 'removes it from the product list' do - expect(page).not_to have_selector "tr#p_#{product2.id}" - expect(page).to have_selector "tr#p_#{product1.id}" + expect(page).not_to have_selector "#p_#{product2.id}" + expect(page).to have_selector "#p_#{product1.id}" end end - context 'a shipped product', js: true do + context 'a shipped product' do let!(:order) { create(:shipped_order, line_items_count: 1) } let!(:line_item) { order.reload.line_items.first } before do login_as_admin_and_visit spree.admin_products_path - within "tr#p_#{order.variants.first.product_id}" do + within "#p_#{order.variants.first.product_id}" do accept_alert { page.find("[data-powertip=Remove]").click } end + visit current_path end - it 'removes it from the product list' do - expect(page).to have_selector "tr#p_#{product1.id}" - expect(page).not_to have_selector "tr#p_#{order.variants.first.product_id}" - expect(Spree::Product.count).to eq 1 - expect(order.line_items.count).to eq 1 + expect(page).to have_selector "#p_#{product1.id}" + expect(page).not_to have_selector "#p_#{order.variants.first.product_id}" end it 'keeps the line item on the order (admin)' do @@ -164,29 +163,18 @@ feature ' describe 'cloning' do let!(:product1) { create(:simple_product, name: 'a weight product', supplier: @supplier, variant_unit: "weight") } - let!(:variant1) { create(:variant, product_id: product1.id, display_name: 'kg oranges') } context 'products', js: true do before { login_as_admin_and_visit spree.admin_products_path } it 'creates a copy of the product' do - within "tr#p_#{product1.id}" do + within "#p_#{product1.id}" do page.find("[data-powertip=Clone]").click end - expect(page).to have_selector "tr#p_#{variant1.product_id}" - - within "tr#p_#{product1.id + 1}" do + visit current_path + within "#p_#{product1.id + 1}" do expect(page).to have_input "product_name", with: 'COPY OF a weight product' end - - # bug #660 - # only the first variant is duplicated - expect(Spree::Product.second.variants.count).to eq 1 - # TODO expect(Spree::Product.second.variants.count).to eq Spree::Product.first.variants.count - - # the stock of the cloned product is always zero - expect(Spree::Product.second.on_hand).to eq 0 - # TODO expect(Spree::Product.second.on_hand).to eq Spree::Product.first.on_hand end end end