Reworked the test to use variant factory

Creating a variant actually create an extra one via the associated
product, as it will create a "standard variant".
As far as I can see there is no way around it, but it should be fixed
once the Product refactor is finished, and product becomes product
group.
Added a comment on the variant factory to explain the problem.

It's not ideal as it will slow down the test suite a little, but I think
it's better to write the code the way you would expect it, and it will
eventually get fixed.
This commit is contained in:
Gaetan Craig-Riou
2024-06-26 10:56:15 +10:00
parent f79691e4bf
commit f60a79437e
2 changed files with 17 additions and 16 deletions

View File

@@ -14,7 +14,13 @@ FactoryBot.define do
primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) }
supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) }
product { |p| p.association(:base_product) }
# createing a product here will end up creating an extra variant, as creating product will
# create a "standard variant" by default. We could try to pass the variant instance we
# are creating but it fails because then the variant instance gets saved and it fails because
# the product isn't associated yet. It's a chicken and egg problem.
# It will be fixed once we finish the product refactor, and we don't need the product to
# create a "standard variant"
product { association :base_product }
# ensure stock item will be created for this variant
before(:create) { create(:stock_location) if Spree::StockLocation.count.zero? }
@@ -25,7 +31,6 @@ FactoryBot.define do
on_hand { 5 }
end
product { |p| p.association(:product) }
unit_value { 1 }
unit_description { '' }

View File

@@ -267,12 +267,10 @@ module Reporting
let(:report) do
AllProducts.new user, { fields_to_hide: [] }
end
# Creating a variant directly would create a product with another variant with no supplier
# which breaks the test
let(:variant) { create(:product).variants.first }
let(:variant) { create(:variant, supplier:) }
let(:supplier) { create(:supplier_enterprise) }
it "Should return headers" do
it "returns headers" do
expect(report.table_headers).to eq([
"Supplier",
"Producer Suburb",
@@ -289,29 +287,27 @@ module Reporting
])
end
it "Should render 'On demand' when the product is available on demand" do
it "renders 'On demand' when the product is available on demand" do
variant.on_demand = true
variant.on_hand = 15
variant.supplier = supplier
variant.save!
first_row = report.table_rows.first
on_demand_column = first_row[-2]
on_hand_column = first_row[-1]
last_row = report.table_rows.last
on_demand_column = last_row[-2]
on_hand_column = last_row[-1]
expect(on_demand_column).to eq("Yes")
expect(on_hand_column).to eq("On demand")
end
it "Should render the on hand count when the product is not available on demand" do
it "renders the on hand count when the product is not available on demand" do
variant.on_demand = false
variant.on_hand = 22
variant.supplier = supplier
variant.save!
first_row = report.table_rows.first
on_demand_column = first_row[-2]
on_hand_column = first_row[-1]
last_row = report.table_rows.last
on_demand_column = last_row[-2]
on_hand_column = last_row[-1]
expect(on_demand_column).to eq("No")
expect(on_hand_column).to eq(22)