Add specs for creating variants

This already works, but we want to make sure it stays that way!
This commit is contained in:
David Cook
2024-02-13 14:43:25 +11:00
parent d812815913
commit ec0e8b80eb
2 changed files with 101 additions and 2 deletions

View File

@@ -36,8 +36,10 @@ describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do
describe '#bulk_update' do
let!(:variant_a1) {
create(:variant, product: product_a, display_name: "Medium box", sku: "APL-01", price: 5.25,
on_hand: 5, on_demand: false)
product_a.variants.first.tap{ |v|
v.update! display_name: "Medium box", sku: "APL-01", price: 5.25, on_hand: 5,
on_demand: false
}
}
let!(:product_c) { create(:simple_product, name: "Carrots", sku: "CAR-00") }
let!(:product_b) { create(:simple_product, name: "Bananas", sku: "BAN-00") }
@@ -101,6 +103,63 @@ describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do
expect(flash).to include success: "Changes saved"
end
it "creates new variants" do
# Form field names:
# '[products][0][id]' (hidden field)
# '[products][0][name]'
# '[products][0][variants_attributes][0][id]' (hidden field)
# '[products][0][variants_attributes][0][display_name]'
# '[products][0][variants_attributes][1][display_name]' (id is omitted for new record)
# '[products][0][variants_attributes][2][display_name]' (more than 1 new record is allowed)
params = {
"products" => {
"0" => {
"id" => product_a.id.to_s,
"name" => "Pommes",
"variants_attributes" => {
"0" => {
"id" => variant_a1.id.to_s,
"display_name" => "Large box",
},
"1" => {
"display_name" => "Small box",
"sku" => "POM-02",
"price" => "5.25",
"unit_value" => "0.5",
},
"2" => {
"sku" => "POM-03",
"price" => "15.25",
"unit_value" => "2",
},
},
},
},
}
expect{
run_reflex(:bulk_update, params:)
product_a.reload
variant_a1.reload
}.to change{ product_a.name }.to("Pommes")
.and change{ variant_a1.display_name }.to("Large box")
.and change{ product_a.variants.count }.by(2)
variant_a2 = product_a.variants[1]
expect(variant_a2.display_name).to eq "Small box"
expect(variant_a2.sku).to eq "POM-02"
expect(variant_a2.price).to eq 5.25
expect(variant_a2.unit_value).to eq 0.5
variant_a3 = product_a.variants[2]
expect(variant_a3.display_name).to be_nil
expect(variant_a3.sku).to eq "POM-03"
expect(variant_a3.price).to eq 15.25
expect(variant_a3.unit_value).to eq 2
expect(flash).to include success: "Changes saved"
end
describe "sorting" do
let(:params) {
{

View File

@@ -243,6 +243,46 @@ describe Sets::ProductSet do
end
end
end
context "new variant" do
let(:variants_attributes) {
[
{ id: product.variants.first.id.to_s }, # default variant unchanged
{ sku: "new sku", price: "5.00", unit_value: "5" }, # omit ID for new variant
]
}
it "creates new variant" do
expect {
product_set.save
expect(product_set.errors).to be_empty
}.to change { product.variants.count }.by(1)
expect(product.variants.last.sku).to eq "new sku"
expect(product.variants.last.price).to eq 5.00
expect(product.variants.last.unit_value).to eq 5
end
context "variant has error" do
let(:variants_attributes) {
[
{ id: product.variants.first.id.to_s }, # default variant unchanged
{ sku: "new sku", unit_value: "blah" }, # price missing, unit_value should be number
]
}
include_examples "nothing saved"
it "logs variant errors" do
product_set.save
expect(product_set.errors.full_messages).to include(
"Variant price is not a number",
"Variant price can't be blank",
"Variant unit value is not a number"
)
end
end
end
end
context 'when there are multiple products' do