Skip to content

Commit

Permalink
Add .from_variant to the products/stock component
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Oct 19, 2023
1 parent 0687e4d commit cbcbb90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ def self.from_product(product)
)
end

def self.from_variant(variant)
new(
on_hand: variant.total_on_hand,
variants_count: nil,
)
end

def initialize(on_hand:, variants_count:)
@on_hand = on_hand
@variants_count = variants_count
Expand All @@ -24,7 +31,7 @@ def call
tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-red-500'
end

variant_info = t('.for_variants', count: @variants_count)
variant_info = t('.for_variants', count: @variants_count) if @variants_count

tag.span safe_join([stock_info, variant_info], ' ')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,30 @@
it "renders the overview preview" do
render_preview(:overview)
end

describe ".from_variant" do
it "has an empty variants count" do
allow(described_class).to receive(:new)

described_class.from_variant(instance_double(Spree::Variant, total_on_hand: 123))

expect(described_class).to have_received(:new).with(
on_hand: 123,
variants_count: nil,
)
end
end

describe ".from_product" do
it "has an empty variants count" do
allow(described_class).to receive(:new)

described_class.from_product(instance_double(Spree::Product, total_on_hand: 123, variants: []))

expect(described_class).to have_received(:new).with(
on_hand: 123,
variants_count: 0,
)
end
end
end

0 comments on commit cbcbb90

Please sign in to comment.