Merge pull request #11415 from abdellani/snail-when-setting-unit-value-to-0

fix Snail when setting "0" in the unit value field
This commit is contained in:
Konrad
2023-08-24 10:04:26 +02:00
committed by GitHub
5 changed files with 63 additions and 8 deletions

View File

@@ -53,8 +53,10 @@ module Spree
validates :name, presence: true
validates :variant_unit, presence: true
validates :unit_value, presence:
{ if: ->(p) { %w(weight volume).include?(p.variant_unit) && new_record? } }
validates :unit_value, numericality: {
greater_than: 0,
if: ->(p) { p.variant_unit.in?(%w(weight volume)) && new_record? }
}
validates :variant_unit_scale,
presence: { if: ->(p) { %w(weight volume).include? p.variant_unit } }
validates :variant_unit_name,

View File

@@ -24,7 +24,7 @@ module Spree
new_product.created_at = nil
new_product.deleted_at = nil
new_product.updated_at = nil
new_product.unit_value = true
new_product.unit_value = %w(weight volume).include?(product.variant_unit) ? 1.0 : nil
new_product.product_properties = reset_properties
new_product.image = duplicate_image(product.image) if product.image
new_product.variants = duplicate_variants

View File

@@ -8,7 +8,8 @@ describe Spree::Core::ProductDuplicator do
name: "foo",
product_properties: [property],
variants: [variant],
image: image
image: image,
variant_unit: 'item'
end
let(:new_product) do
@@ -70,7 +71,7 @@ describe Spree::Core::ProductDuplicator do
expect(new_product).to receive(:sku=).with("")
expect(new_product).to receive(:product_properties=).with([new_property])
expect(new_product).to receive(:created_at=).with(nil)
expect(new_product).to receive(:unit_value=).with(true)
expect(new_product).to receive(:unit_value=).with(nil)
expect(new_product).to receive(:updated_at=).with(nil)
expect(new_product).to receive(:deleted_at=).with(nil)
expect(new_product).to receive(:variants=).with([new_variant])

View File

@@ -167,8 +167,25 @@ module Spree
expect(build(:simple_product, primary_taxon: nil)).not_to be_valid
end
it "requires a unit value" do
expect(build(:simple_product, unit_value: nil)).not_to be_valid
context "unit value" do
it "requires a unit value when variant unit is weight" do
expect(build(:simple_product, variant_unit: 'weight', variant_unit_name: 'name',
unit_value: nil)).not_to be_valid
expect(build(:simple_product, variant_unit: 'weight', variant_unit_name: 'name',
unit_value: 0)).not_to be_valid
end
it "requires a unit value when variant unit is volume" do
expect(build(:simple_product, variant_unit: 'volume', variant_unit_name: 'name',
unit_value: nil)).not_to be_valid
expect(build(:simple_product, variant_unit: 'volume', variant_unit_name: 'name',
unit_value: 0)).not_to be_valid
end
it "does not require a unit value when variant unit is items" do
expect(build(:simple_product, variant_unit: 'items', variant_unit_name: 'name',
unit_value: nil)).to be_valid
end
end
it "requires a supplier" do

View File

@@ -64,6 +64,41 @@ describe '
expect(page).to have_content "Name can't be blank"
end
it "display all attributes when submitting with error: Unit Value must be grater than 0" do
login_to_admin_section
visit spree.new_admin_product_path
select 'New supplier', from: 'product_supplier_id'
fill_in 'product_name', with: "new product name"
select "Weight (kg)", from: 'product_variant_unit_with_scale'
fill_in 'product_unit_value', with: "0.00 g"
assert_selector(:field, placeholder: "0g g")
fill_in 'product_display_as', with: "Big Box of Chocolates"
select taxon.name, from: "product_primary_taxon_id"
fill_in 'product_price', with: '19.99'
fill_in 'product_on_hand', with: 5
check 'product_on_demand'
select 'Test Tax Category', from: 'product_tax_category_id'
page.find("div[id^='taTextElement']").native.send_keys('A description...')
click_button 'Create'
expect(page).to have_field 'product_name', with: "new product name"
expect(page).to have_field 'product_supplier_id', with: @supplier.id
expect(page).to have_field 'product_unit_value', with: "0 g"
expect(page).to have_field 'product_display_as', with: "Big Box of Chocolates"
expect(page).to have_field 'product_primary_taxon_id', with: taxon.id
expect(page).to have_field 'product_price', with: '19.99'
expect(page).to have_field 'product_on_hand', with: 5
expect(page).to have_field 'product_on_demand', checked: true
expect(page).to have_field 'product_tax_category_id', with: tax_category.id
expect(page.find("div[id^='taTextElement']")).to have_content 'A description...'
expect(page.find("#product_variant_unit_field")).to have_content 'Weight (kg)'
expect(page).to have_content "Unit value must be greater than 0"
end
it "preserves 'Items' 'Unit Size' selection when submitting with error" do
login_to_admin_section
@@ -164,7 +199,7 @@ describe '
click_button 'Create'
expect(current_path).to eq spree.admin_products_path
expect(page).to have_content "Unit value can't be blank"
expect(page).to have_content "Unit value is not a number"
end
end