From 9186bcd4556b09fba50461d47093873a576e21e9 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 16 Aug 2019 19:35:08 +0100 Subject: [PATCH 1/5] Dry out some code in checkout_spec --- .../consumer/shopping/checkout_spec.rb | 93 ++++++++----------- 1 file changed, 37 insertions(+), 56 deletions(-) diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 5635316150..a6fc56379c 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -64,31 +64,6 @@ feature "As a consumer I want to check out my cart", js: true do context 'login in as user' do let(:user) { create(:user) } - def fill_out_form - choose sm1.name - choose pm1.name - - within "#details" do - fill_in "First Name", with: "Will" - fill_in "Last Name", with: "Marshall" - fill_in "Email", with: "test@test.com" - fill_in "Phone", with: "0468363090" - end - - check "Save as default billing address" - - within "#billing" do - fill_in "City", with: "Melbourne" - fill_in "Postcode", with: "3066" - fill_in "Address", with: "123 Your Head" - select "Australia", from: "Country" - select "Victoria", from: "State" - end - - check "Shipping address same as billing address?" - check "Save as default shipping address" - end - before do quick_login_as(user) end @@ -216,7 +191,7 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content distributor.name end - it 'does not show the save as defalut address checkbox' do + it 'does not show the save as default address checkbox' do page.should_not have_content "Save as default billing address" page.should_not have_content "Save as default shipping address" end @@ -315,20 +290,8 @@ feature "As a consumer I want to check out my cart", js: true do describe "purchasing" do it "takes us to the order confirmation page when we submit a complete form" do - within "#details" do - fill_in "First Name", with: "Will" - fill_in "Last Name", with: "Marshall" - fill_in "Email", with: "test@test.com" - fill_in "Phone", with: "0468363090" - end - - within "#billing" do - fill_in "Address", with: "123 Your Face" - select "Australia", from: "Country" - select "Victoria", from: "State" - fill_in "City", with: "Melbourne" - fill_in "Postcode", with: "3066" - end + fill_out_details + fill_out_billing_address within "#shipping" do choose sm2.name @@ -365,22 +328,8 @@ feature "As a consumer I want to check out my cart", js: true do before do choose sm1.name choose pm1.name - - within "#details" do - fill_in "First Name", with: "Will" - fill_in "Last Name", with: "Marshall" - fill_in "Email", with: "test@test.com" - fill_in "Phone", with: "0468363090" - end - - within "#billing" do - fill_in "City", with: "Melbourne" - fill_in "Postcode", with: "3066" - fill_in "Address", with: "123 Your Face" - select "Australia", from: "Country" - select "Victoria", from: "State" - end - + fill_out_details + fill_out_billing_address check "Shipping address same as billing address?" end @@ -475,4 +424,36 @@ feature "As a consumer I want to check out my cart", js: true do end end end + + def fill_out_details + within "#details" do + fill_in "First Name", with: "Will" + fill_in "Last Name", with: "Marshall" + fill_in "Email", with: "test@test.com" + fill_in "Phone", with: "0468363090" + end + end + + def fill_out_billing_address + within "#billing" do + fill_in "City", with: "Melbourne" + fill_in "Postcode", with: "3066" + fill_in "Address", with: "123 Your Head" + select "Australia", from: "Country" + select "Victoria", from: "State" + end + end + + def fill_out_form + choose sm1.name + choose pm1.name + + fill_out_details + check "Save as default billing address" + + fill_out_billing_address + + check "Shipping address same as billing address?" + check "Save as default shipping address" + end end From c2f302450f8bb8e3ff90746c58f4696400a6de82 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 16 Aug 2019 19:35:57 +0100 Subject: [PATCH 2/5] Add spec to checkout to validate product is removed from shopfront just after all available quantity is checked out --- .../consumer/shopping/checkout_spec.rb | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index a6fc56379c..0733b7df42 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -180,6 +180,30 @@ feature "As a consumer I want to check out my cart", js: true do end end + context "in the shopfront" do + it "does not show item after all stock of an item is checked out (tesging cache update on checkout)" do + variant.update_attributes on_hand: 5 + visit shop_path + + fill_in "variants[#{variant.id}]", with: '5' + wait_until { !cart_dirty } + + visit checkout_path + checkout_as_guest + + fill_out_details + fill_out_billing_address + choose sm1.name + choose pm1.name + + place_order + expect(page).to have_content "Your order has been processed successfully" + + visit shop_path + page.should_not have_content variant.product.name + end + end + context "on the checkout page" do before do visit checkout_path From 99ff7149130dbfbc8cf5cfee52f97c7bc132a910 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 16 Aug 2019 20:17:14 +0100 Subject: [PATCH 3/5] Enable cache on checkout spec to test cache after checkout --- .../consumer/shopping/checkout_spec.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 0733b7df42..a0561c062f 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -180,8 +180,19 @@ feature "As a consumer I want to check out my cart", js: true do end end - context "in the shopfront" do + context "in the shopfront with cache enabled" do + around do |example| + original_config = Spree::Config[:enable_products_cache?] + example.run + Spree::Config[:enable_products_cache?] = original_config + end + + let(:control_product) { create(:taxed_product, supplier: supplier, price: 110, zone: zone, tax_rate_amount: 0.1) } + let(:control_variant) { control_product.variants.first } + let!(:order_cycle) { create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], coordinator: create(:distributor_enterprise), variants: [variant, control_variant]) } + it "does not show item after all stock of an item is checked out (tesging cache update on checkout)" do + Spree::Config[:enable_products_cache?] = true variant.update_attributes on_hand: 5 visit shop_path @@ -200,7 +211,10 @@ feature "As a consumer I want to check out my cart", js: true do expect(page).to have_content "Your order has been processed successfully" visit shop_path - page.should_not have_content variant.product.name + # The presence of the control product ensures the list of products is fully loaded + # before we verify the sold product is not present + page.should have_content control_product.name + page.should_not have_content product.name end end From 3ecb5c0c75bbc358b2552e843ae150f701a456ea Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sun, 18 Aug 2019 18:16:28 +1000 Subject: [PATCH 4/5] Flush background jobs in failing spec --- spec/features/consumer/shopping/checkout_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index a0561c062f..5b1ecfc50f 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -194,6 +194,7 @@ feature "As a consumer I want to check out my cart", js: true do it "does not show item after all stock of an item is checked out (tesging cache update on checkout)" do Spree::Config[:enable_products_cache?] = true variant.update_attributes on_hand: 5 + flush_jobs visit shop_path fill_in "variants[#{variant.id}]", with: '5' @@ -210,6 +211,7 @@ feature "As a consumer I want to check out my cart", js: true do place_order expect(page).to have_content "Your order has been processed successfully" + flush_jobs visit shop_path # The presence of the control product ensures the list of products is fully loaded # before we verify the sold product is not present From 706168f2f0782c3935453929d62efbc82c9d899e Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sun, 18 Aug 2019 16:22:12 +0800 Subject: [PATCH 5/5] Refresh variant cache when changing stock movements --- app/models/spree/stock_movement_decorator.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 app/models/spree/stock_movement_decorator.rb diff --git a/app/models/spree/stock_movement_decorator.rb b/app/models/spree/stock_movement_decorator.rb new file mode 100644 index 0000000000..40aa049943 --- /dev/null +++ b/app/models/spree/stock_movement_decorator.rb @@ -0,0 +1,10 @@ +Spree::StockMovement.class_eval do + after_save :refresh_products_cache + + private + + def refresh_products_cache + return if stock_item.variant.blank? + OpenFoodNetwork::ProductsCache.variant_changed stock_item.variant + end +end