From 96975df061fae7d9aecbcdabebe8745e80ee9f7e Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 18 Mar 2024 16:00:29 +0100 Subject: [PATCH 1/2] Stock item factory: Allow setting on hand count We have to jump through all kinds of hoops to do this otherwise. Just give the count on hand to the factory. --- .../spree/testing_support/factories/stock_item_factory.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/lib/spree/testing_support/factories/stock_item_factory.rb b/core/lib/spree/testing_support/factories/stock_item_factory.rb index b4fe2a8b46f..36fb77aaff5 100644 --- a/core/lib/spree/testing_support/factories/stock_item_factory.rb +++ b/core/lib/spree/testing_support/factories/stock_item_factory.rb @@ -6,6 +6,10 @@ association :stock_location, factory: :stock_location_without_variant_propagation variant - after(:create) { |object| object.adjust_count_on_hand(10) } + transient do + on_hand { 10 } + end + + after(:create) { |object, evaluator| object.adjust_count_on_hand(evaluator.on_hand) } end end From 755f381409930178f6643dc0b5be7565e86f90b8 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 18 Mar 2024 16:01:04 +0100 Subject: [PATCH 2/2] Fix flaky stock item spec This spec was flaky, as it generated potentially multiple SKUs per variant, and it generated them implicitly. These changes makes everything very explicit, such that we get exactly one stock item per variant with an identifiable name. --- admin/spec/features/stock_items_spec.rb | 33 ++++++++++++------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/admin/spec/features/stock_items_spec.rb b/admin/spec/features/stock_items_spec.rb index ca4c430753f..6e07172fb62 100644 --- a/admin/spec/features/stock_items_spec.rb +++ b/admin/spec/features/stock_items_spec.rb @@ -5,21 +5,20 @@ describe "Stock Items", :js, type: :feature do before { sign_in create(:admin_user, email: 'admin@example.com') } - it "lists stock items and allows navigating through scopes" do - non_backorderable = create(:stock_item, backorderable: false) - non_backorderable.variant.update!(sku: 'MY-SKU-1234567890') - backorderable = create(:stock_item, backorderable: true) - out_of_stock = begin - item = create(:stock_item, backorderable: false) - item.reduce_count_on_hand_to_zero - item - end - low_stock = begin - item = create(:stock_item, backorderable: false) - item.set_count_on_hand(SolidusAdmin::Config[:low_stock_value] - 1) - item - end + # We don't want multiple stock items per variant, and stock locations are created implicitly otherwise, + # and their default is to create stock items for all variants, which in this spec we do manually. + let!(:stock_location) { create(:stock_location, name: 'default', propagate_all_variants: false) } + + let(:backorderable_variant) { create(:variant, sku: 'backorderable', stock_items: []) } + let(:non_backorderable_variant) { create(:variant, sku: 'non-backorderable', stock_items: []) } + let(:out_of_stock_variant) { create(:variant, sku: 'out-of-stock', stock_items: []) } + let(:low_stock_variant) { create(:variant, sku: 'low-stock', stock_items: []) } + let!(:backorderable) { create(:stock_item, variant: backorderable_variant, backorderable: true) } + let!(:non_backorderable) { create(:stock_item, variant: non_backorderable_variant, backorderable: false) } + let!(:out_of_stock) { create(:stock_item, variant: out_of_stock_variant, backorderable: false, on_hand: 0) } + let!(:low_stock) { create(:stock_item, variant: low_stock_variant, backorderable: false, on_hand: SolidusAdmin::Config.low_stock_value - 1) } + it "lists stock items and allows navigating through scopes" do visit "/admin/stock_items" # `All` default scope @@ -29,11 +28,11 @@ expect(page).to have_content(low_stock.variant.sku) # Edit stock item - find('td', text: 'MY-SKU-1234567890').click + find('td', text: 'non-backorderable').click fill_in :quantity_adjustment, with: 1 click_on "Save" - expect(find('tr', text: 'MY-SKU-1234567890')).to have_content('11') - expect(find('tr', text: 'MY-SKU-1234567890')).to have_content('1 stock movement') + expect(find('tr', text: 'non-backorderable')).to have_content('11') + expect(find('tr', text: 'non-backorderable')).to have_content('1 stock movement') click_on 'Back Orderable' expect(page).to have_css('[aria-current="true"]', text: 'Back Orderable')