Skip to content

Commit

Permalink
REFACTOR kit factory to use KitCreateService, update rspecs
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyli97 committed Aug 1, 2024
1 parent 76e7be6 commit 6405280
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -792,4 +792,4 @@ DEPENDENCIES
webmock (~> 3.23)

BUNDLED WITH
2.5.14
2.5.16
9 changes: 5 additions & 4 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,12 @@
end

it "should process a kit allocation event" do
kit = FactoryBot.create(:kit, :with_item, organization: organization)
line_items = []
line_items << build(:line_item, quantity: 10, item: item1, itemizable: nil)
line_items << build(:line_item, quantity: 3, item: item2, itemizable: nil)

kit = FactoryBot.create(:kit, :with_item, organization: organization, line_items: line_items)

kit.line_items = []
kit.line_items << build(:line_item, quantity: 10, item: item1, itemizable: kit)
kit.line_items << build(:line_item, quantity: 3, item: item2, itemizable: kit)
KitAllocateEvent.publish(kit, storage_location1.id, 2)

# 30 - (10*2) = 10, 10 - (3*2) = 4
Expand Down
16 changes: 7 additions & 9 deletions spec/factories/kits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
#
FactoryBot.define do
factory :kit do
sequence(:name) { |n| "Test Kit #{n}" }
organization

after(:build) do |instance, _|
if instance.line_items.blank?
instance.line_items << create(:line_item, item: create(:item, organization: instance.organization), itemizable: instance)
end
end
sequence(:name) { |n| "Default Kit Name #{n} - Don't Match" }
organization { Organization.try(:first) || create(:organization) }
line_items { [build(:line_item, item: create(:item, organization: instance.organization), quantity: 1, itemizable: nil)] }

trait :with_item do
skip_create
after(:create) do |instance, _|
create(:item, kit: instance, organization: instance.organization)
kit_service = KitCreateService.new(organization_id: instance.organization_id,
kit_params: instance.attributes.merge(line_items_attributes: instance.line_items.as_json)).call
instance.attributes = kit_service.kit.attributes
end
end
end
Expand Down

0 comments on commit 6405280

Please sign in to comment.