Skip to content

Commit

Permalink
REFACTOR move kit creation in specs into helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyli97 committed Sep 25, 2024
1 parent 6b50518 commit 730371a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 45 deletions.
14 changes: 4 additions & 10 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,10 @@
end

it "should process a kit allocation event" do
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
kit = create_kit(organization: organization, line_items_attributes: [
{item_id: item1.id, quantity: 10},
{item_id: item2.id, quantity: 3}
]

kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
])

KitAllocateEvent.publish(kit, storage_location1.id, 2)

Expand Down Expand Up @@ -419,13 +416,10 @@
end

it "should process a kit deallocation event" do
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
kit = create_kit(organization: organization, line_items_attributes: [
{item_id: item1.id, quantity: 20},
{item_id: item2.id, quantity: 5}
]

kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
])

TestInventory.create_inventory(organization,
{
Expand Down
6 changes: 2 additions & 4 deletions spec/factories/kits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
end

# See #3652, changes to this factory are in progress
# For now, to create corresponding item and line item and persist to database call KitCreateService with:
# kit_params = attributes_for(:kit)
# kit_params[:line_items_attributes] = [{ item_id: _id_, quantity: _quantity_, ...}]
# KitCreateService.new(organization_id: organization.id, kit_params: kit_params).call.kit
# For now, to create corresponding item and line item and persist to database call create_kit
# from spec/support/kit_helper.rb
end
end
12 changes: 4 additions & 8 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,9 @@

context "in a kit" do
before do
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
create_kit(organization: organization, line_items_attributes: [
{item_id: item.id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call
])
end

it "should return false" do
Expand Down Expand Up @@ -275,11 +273,9 @@

context "in a kit" do
before do
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
create_kit(organization: organization, line_items_attributes: [
{item_id: item.id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call
])
end

it "should return false" do
Expand Down
6 changes: 1 addition & 5 deletions spec/models/kit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@
end

specify 'deactivate and reactivate' do
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: create(:item).id, quantity: 1}
]
kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
kit = create_kit(organization: organization)
expect(kit.active).to eq(true)
expect(kit.item.active).to eq(true)
kit.deactivate
Expand Down
12 changes: 2 additions & 10 deletions spec/requests/kit_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
let(:organization_admin) { create(:organization_admin, organization: organization) }

let!(:kit) {
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: create(:item).id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
create_kit(organization: organization)
}

describe "while signed in" do
Expand All @@ -19,11 +15,7 @@
describe "GET #index" do
before do
# this shouldn't be shown
params = FactoryBot.attributes_for(:kit, active: false, name: "DOOBIE KIT")
params[:line_items_attributes] = [
{item_id: create(:item).id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call
create_kit(organization: organization, active: false, name: "DOOBIE KIT")
end

it "should include deactivate" do
Expand Down
12 changes: 4 additions & 8 deletions spec/services/reports/acquisition_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@
disposable_kit_item = create(:item, name: "Adult Disposable Diapers", partner_key: "adult diapers")
another_disposable_kit_item = create(:item, name: "Infant Disposable Diapers", partner_key: "infant diapers")

params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
disposable_kit = create_kit(organization: organization, line_items_attributes: [
{item_id: disposable_kit_item.id, quantity: 5}
]
disposable_kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
])

params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
another_disposable_kit = create_kit(organization: organization, line_items_attributes: [
{item_id: another_disposable_kit_item.id, quantity: 5}
]
another_disposable_kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
])

disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time)
another_disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time)
Expand Down
10 changes: 10 additions & 0 deletions spec/support/kit_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def create_kit(name: nil, active: true, organization: create(:organization), line_items_attributes: nil)
params = FactoryBot.attributes_for(:kit, active: active)
params[:name] = name if name

params[:line_items_attributes] = (line_items_attributes || [
{item_id: create(:item, organization: organization).id, quantity: 1}
])

KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
end

0 comments on commit 730371a

Please sign in to comment.