Re add test for invalid cloned products

There is no easy way to make the original product invalid, but we can
make sure the cloned product will be invalid. The cloned product add
"COPe OF " in front of the product's name, so by starting with a name
that's long enough, the cloned product will have a name longer that 255
char and will then be invalid.
This commit is contained in:
Gaetan Craig-Riou
2024-08-27 10:41:33 +10:00
parent 755a394704
commit b0433bd8f5
3 changed files with 37 additions and 0 deletions

View File

@@ -68,6 +68,21 @@ RSpec.describe Spree::Core::ProductDuplicator do
end
describe "errors" do
context "with invalid product" do
# Name has a max length of 255 char, when cloning a product the cloned product has a name
# starting with "COPY OF <product.name>". So we set a name with 254 char to make sure the
# cloned product will be invalid
let(:product) {
create(:product).tap{ |v| v.update_columns(name: "l" * 254) }
}
subject { Spree::Core::ProductDuplicator.new(product).duplicate }
it "raises RecordInvalid error" do
expect{ subject }.to raise_error(ActiveRecord::ActiveRecordError)
end
end
context "invalid variant" do
let(:variant) {
# tax_category is required when products_require_tax_category

View File

@@ -17,6 +17,13 @@ module Spree
expect(clone.sku).to eq ""
expect(clone.image).to eq product.image
end
it 'fails to duplicate invalid product' do
# cloned product will be invalid
product.update_columns(name: "l" * 254)
expect{ product.duplicate }.to raise_error(ActiveRecord::ActiveRecordError)
end
end
context "product has variants" do

View File

@@ -294,6 +294,21 @@ RSpec.describe 'As an enterprise user, I can manage my products' do
end
end
end
it "shows error message when cloning invalid record" do
# The cloned product will be invalid
product_a.update_columns(name: "L" * 254)
# The page has not been reloaded so the product's name is still "Apples"
click_product_clone "Apples"
expect(page).to have_content "Unable to clone the product"
within "table.products" do
# Products does not include the cloned product.
expect(all_input_values).not_to match /COPY OF #{('L' * 254)}/
end
end
end
describe "delete" do