From f54c69cbbab37caa0d3e448a01daa8981a7f7825 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 4 Oct 2018 18:21:53 +0200 Subject: [PATCH] Add first test case for ProductSet This covers creation and update of a product. --- spec/models/spree/product_set_spec.rb | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 spec/models/spree/product_set_spec.rb diff --git a/spec/models/spree/product_set_spec.rb b/spec/models/spree/product_set_spec.rb new file mode 100644 index 0000000000..543debb0de --- /dev/null +++ b/spec/models/spree/product_set_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' + +describe Spree::ProductSet do + describe '#save' do + context 'when passing :collection_attributes' do + let(:product_set) do + described_class.new(collection_attributes: collection_hash) + end + + context 'when the product does not exist yet' do + let(:collection_hash) do + { + 0 => { + product_id: 11, + name: 'a product', + price: 2.0, + supplier_id: create(:enterprise).id, + primary_taxon_id: create(:taxon).id, + unit_description: 'description', + variant_unit: 'items', + variant_unit_name: 'bunches' + } + } + end + + it 'creates it with the specified attributes' do + product_set.save + + expect(Spree::Product.last.attributes) + .to include('name' => 'a product') + end + end + + context 'when the product does exist' do + let!(:product) do + create( + :simple_product, + variant_unit: 'items', + variant_unit_scale: nil, + variant_unit_name: 'bunches' + ) + end + + let(:collection_hash) do + { + 0 => { + id: product.id, + variant_unit: 'weight', + variant_unit_scale: 1 + } + } + end + + it 'updates all the specified product attributes' do + product_set.save + + expect(product.reload.attributes).to include( + 'variant_unit' => 'weight', + 'variant_unit_scale' => 1 + ) + end + end + end + end +end