Skip to content

Commit

Permalink
Fix variant system spec
Browse files Browse the repository at this point in the history
  • Loading branch information
rioug committed Aug 7, 2024
1 parent 258bd90 commit 0bbbf56
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 43 deletions.
13 changes: 6 additions & 7 deletions app/views/spree/admin/variants/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@
%input{ "type" => "text", "id" => "variant_unit_price", "name" => "variant[unit_price]", "class" => 'fullwidth', "disabled" => true}
%div{style: "color: black"}
= t("spree.admin.products.new.unit_price_legend")
%div{ 'set-on-demand' => '' }
%div
.field.checkbox
%label
= f.check_box :on_demand
= f.check_box :on_demand, data: { "action": "click->edit-variant#toggleOnHand" }
= t(:on_demand)
- #TODO tooltip is broken
%div{'ofn-with-tip' => t('admin.products.variants.to_order_tip')}
%a= t('admin.whats_this')

= render partial: "admin/shared/tooltip", locals: { tooltip_text: t('admin.products.variants.to_order_tip'), link_text: t('admin.whats_this'), placement: "right" }
.field
= f.label :on_hand, t(:on_hand)
.fullwidth
= f.text_field :on_hand
= f.text_field :on_hand, data: { "edit-variant-target": "onHand" }

.right.six.columns.omega.label-block
.field
Expand All @@ -98,7 +97,7 @@
= f.collection_select(:primary_taxon_id, Spree::Taxon.order(:name), :id, :name, { include_blank: true }, { class: "select2 fullwidth" })

.field
= f.label :supplier, t(:spree_admin_supplier)
= f.label :supplier, raw(t(:spree_admin_supplier) + content_tag(:span, ' *', :class => 'required'))
= f.collection_select(:supplier_id, @producers, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"})

.clear
96 changes: 60 additions & 36 deletions spec/system/admin/variants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@

describe "new variant" do
it "creating a new variant" do
# Given a product with a unit-related option type
product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1")
# Given a product
product = create(:simple_product)

# When I create a variant on the product
login_as_admin
visit spree.admin_product_variants_path product
click_link 'New Variant'

fill_in 'unit_value_human', with: '1'
fill_in 'variant_unit_description', with: 'foo'
tomselect_select("Volume (L)", from: "Unit scale")
click_on "Unit" # activate popout
# Unit popout
fill_in "Unit value", with: "1"
fill_in 'Price', with: 2.5
select taxon.name, from: "variant_primary_taxon_id"
select2_select product.variants.first.supplier.name, from: "variant_supplier_id"

click_button 'Create'

# Then the variant should have been created
Expand All @@ -33,7 +37,7 @@

it "creating a new variant from product variant page with filter" do
# Given a product with a unit-related option type
product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1")
product = create(:simple_product)
filter = { producerFilter: 2 }

# When I create a variant on the product
Expand All @@ -54,32 +58,45 @@

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")
product = create(:simple_product)

# When I create a variant on the product
login_as_admin
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'
fill_in 'unit_value_human', with: 1
tomselect_select("Volume (L)", from: "Unit scale")
click_on "Unit" # activate popout
# Unit popout
fill_in "Unit value", with: "1"
fill_in 'Price', with: 2.5
select taxon.name, from: "variant_primary_taxon_id"
select2_select product.variants.first.supplier.name, from: "variant_supplier_id"

# 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

it "show validation errors if present" do
product = create(:simple_product, variant_unit: "volume", variant_unit_scale: "1")
product = create(:simple_product)
login_as_admin
visit spree.admin_product_variants_path product
click_link 'New Variant'

fill_in 'unit_value_human', with: 0
tomselect_select("Volume (L)", from: "Unit scale")
fill_in 'Price', with: 2.5
select taxon.name, from: "variant_primary_taxon_id"
select2_select product.variants.first.supplier.name, from: "variant_supplier_id"

click_on "Unit" # activate popout
# Unit popout
fill_in "Unit value", with: "0"
# fill_in 'unit_value_human', with: 0
click_button 'Create'

expect(page).to have_content "Unit value must be greater than 0"
Expand All @@ -89,7 +106,7 @@
describe "viewing product variant" do
it "when the product page has a product filter" do
# Given a product with a unit-related option type
product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1")
product = create(:simple_product)
filter = { producerFilter: 2 }

# When I create a variant on the product
Expand Down Expand Up @@ -123,7 +140,7 @@

describe "editing unit value and description for a variant" do
it "when the product variant page has product filter" do
product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1")
product = create(:simple_product)
filter = { producerFilter: 2 }

# When I create a variant on the product
Expand All @@ -140,9 +157,10 @@

it "when variant_unit is weight" do
# Given a product with unit-related option types, with a variant
product = create(:simple_product, variant_unit: "weight", variant_unit_scale: "1")
product = create(:simple_product)
variant = product.variants.first
variant.update( unit_value: 1, unit_description: 'foo' )
variant.update( unit_value: 1, unit_description: 'foo', variant_unit: "weight",
variant_unit_scale: "1")

# When I view the variant
login_as_admin
Expand All @@ -151,41 +169,47 @@
page.find('table.index .icon-edit').click

# And I should see unit value and description fields for the unit-related option value
expect(page).to have_field "unit_value_human", with: "1"
expect(page).to have_field "variant_unit_description", with: "foo"
click_on "Unit" # activate popout
expect(page).to have_field "Unit value", with: "1 foo"

# When I update the fields and save the variant
fill_in "unit_value_human", with: "123"
fill_in "variant_unit_description", with: "bar"
click_on "Unit" # activate popout
# Unit popout
fill_in "Unit value", with: "123 bar"

click_button 'Update'
expect(page).to have_content %(Variant "#{product.name}" has been successfully updated!)

# Then the unit value and description should have been saved
expect(variant.reload.unit_value).to eq(123)
variant.reload
expect(variant.unit_value).to eq(123)
expect(variant.unit_description).to eq('bar')
end

it "can update unit_description when variant_unit is items" do
product = create(:simple_product, variant_unit: "items", variant_unit_name: "bunches")
product = create(:simple_product)
variant = product.variants.first
variant.update(unit_description: 'foo')
variant.update(unit_description: 'foo', variant_unit: "items", variant_unit_name: "bunches")

login_as_admin
visit spree.edit_admin_product_variant_path(product, variant)

expect(page).not_to have_field "unit_value_human"
expect(page).to have_field "variant_weight"
expect(page).to have_field "variant_unit_description", with: "foo"
click_on "Unit" # activate popout
expect(page).to have_field "Unit value", with: "1 foo"

fill_in "variant_unit_description", with: "bar"
click_on "Unit" # activate popout
# Unit popout
fill_in "Unit value", with: "123 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')
end

context "with ES as a locale" do
let(:product) { create(:simple_product, variant_unit: "weight", variant_unit_scale: "1") }
let(:product) { create(:simple_product) }
let(:variant) { product.variants.first }

around do |example|
Expand Down Expand Up @@ -318,7 +342,7 @@
end

describe "editing variant attributes" do
let!(:variant) { create(:variant) }
let!(:variant) { create(:variant, variant_unit: "weight", variant_unit_scale: "1") }
let(:product) { variant.product }
let!(:tax_category) { create(:tax_category) }

Expand All @@ -330,34 +354,34 @@
it "editing display name for a variant" do
# It should allow the display name to be changed
expect(page).to have_field "variant_display_name"
click_on "Unit" # activate popout
expect(page).to have_field "variant_display_as"

# When I update the fields and save the variant
fill_in "variant_display_name", with: "Display Name"
click_on "Unit" # activate popout
fill_in "variant_display_as", with: "Display As This"

click_button 'Update'
expect(page).to have_content %(Variant "#{product.name}" has been successfully updated!)

# Then the displayed values should have been saved
expect(variant.reload.display_name).to eq("Display Name")
variant.reload
expect(variant.display_name).to eq("Display Name")
expect(variant.display_as).to eq("Display As This")
end

it "editing weight for a variant" do
# It should allow the weight to be changed
expect(page).to have_field "unit_value_human"
click_on "Unit" # activate popout
expect(page).to have_field "Unit value"

# 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"
fill_in "Unit value", with: "1.234"
click_button 'Update'
expect(page).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)
expect(variant.reload.unit_value).to eq(1.234)
end

context "editing variant tax category" do
Expand Down

0 comments on commit 0bbbf56

Please sign in to comment.