Fix admin product variants form to close issues #10579 & #430

This commit is contained in:
MadisonBowron
2023-04-10 18:53:01 -04:00
parent 8bbc545e39
commit 9d0a69cceb
2 changed files with 50 additions and 5 deletions

View File

@@ -11,8 +11,10 @@
.field
= label_tag :unit_value_human, "#{t('admin.'+@product.variant_unit)} ({{unitName(#{@product.variant_unit_scale}, '#{@product.variant_unit}')}})"
= hidden_field_tag 'product_variant_unit_scale', @product.variant_unit_scale
= text_field_tag :unit_value_human, nil, {class: "fullwidth", 'ng-model' => 'unit_value_human', 'ng-change' => 'updateValue()'}
= f.text_field :unit_value, {hidden: true, 'ng-value' => 'unit_value'}
-# CHANGED LINE
= number_field_tag :unit_value_human, nil, {class: "fullwidth", step: 0.01, 'ng-model' => 'unit_value_human', 'ng-change' => 'updateValue()'}
-# CHANGED LINE
= f.number_field :unit_value, {hidden: true, 'ng-value' => 'unit_value'}
.field
= f.label :unit_description, t(:spree_admin_unit_description)
@@ -65,8 +67,8 @@
- if @product.variant_unit != 'weight'
.field
= f.label 'weight', t(:weight)+' (kg)'
- value = number_with_precision(@variant.weight, precision: 2)
= f.number_field 'weight', value: value, class: 'fullwidth', step: 0.01
- value = number_with_precision(@variant.weight, precision: 3)
= f.number_field 'weight', value: value, class: 'fullwidth', step: 0.001
- [:height, :width, :depth].each do |field|
.field
@@ -74,4 +76,4 @@
- value = number_with_precision(@variant.send(field), precision: 2)
= f.number_field field, value: value, class: 'fullwidth', step: 0.01
.clear
.clear

View File

@@ -45,6 +45,22 @@ describe '
)
expect(page).to have_link('Cancel', href: expected_cancel_url)
end
it "creating a new variant with non-weight unit type" do
# Given a product with a unit-related option type
product = create(:simple_product, variant_unit: "volume", variant_unit_scale: "1")
# When I create a variant on the product
login_as_admin_and_visit spree.admin_product_variants_path product
click_link 'New Variant'
# Expect variant_weight to accept 3 decimal places
fill_in 'variant_weight', with: '1.234'
click_button 'Create'
# Then the variant should have been created
expect(page).to have_content "Variant \"#{product.name}\" has been successfully created!"
end
end
describe "viewing product variant" do
@@ -138,9 +154,11 @@ describe '
login_as_admin_and_visit spree.edit_admin_product_variant_path(product, variant)
expect(page).to_not have_field "unit_value_human"
expect(page).to have_field "variant_weight"
expect(page).to have_field "variant_unit_description", with: "foo"
fill_in "variant_unit_description", with: "bar"
fill_in "variant_weight", with: "1.234"
click_button 'Update'
expect(page).to have_content %(Variant "#{product.name}" has been successfully updated!)
expect(variant.reload.unit_description).to eq('bar')
@@ -226,4 +244,29 @@ describe '
expect(variant.reload.display_name).to eq("Display Name")
expect(variant.display_as).to eq("Display As This")
end
it "editing weight for a variant" do
product = create(:simple_product)
variant = product.variants.first
# When I view the variant
login_as_admin_and_visit spree.admin_product_variants_path product
page.find('table.index .icon-edit').click
# It should allow the weight to be changed
expect(page).to have_field "unit_value_human"
# When I update the fields and save the variant with invalid value
fill_in "unit_value_human", with: "1.234"
click_button 'Update'
expect(page).not_to have_content %(Variant "#{product.name}" has been successfully updated!)
fill_in "unit_value_human", with: "1.23"
click_button 'Update'
expect(page).not_to have_content %(Variant "#{product.name}" has been successfully updated!)
# Then the displayed values should have been saved
expect(variant.reload.unit_value).to eq(1.23)
end
end