Skip to content

Commit

Permalink
Re add test for invalid cloned products
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rioug committed Aug 27, 2024
1 parent 4ad89f6 commit 57eee15
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spec/lib/spree/core/product_duplicator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@
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
Expand Down
7 changes: 7 additions & 0 deletions spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/system/admin/products_v3/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ def expect_other_columns_visible
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
Expand Down

0 comments on commit 57eee15

Please sign in to comment.