Skip to content

Commit

Permalink
RED edit rspec to ensure entered user info is kept after donation upd…
Browse files Browse the repository at this point in the history
…ate error

* Page should still display original donation source pre-edit in the title
  and breadcrumbs
  • Loading branch information
jimmyli97 committed Jul 12, 2024
1 parent 0585857 commit 672e9b6
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions spec/requests/donations_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,15 @@
# Bug fix - Issue #4172
context "when donated items are distributed to less than donated amount " \
"and you edit the donation to less than distributed amount" do
it "shows a warning and displays original names and amounts" do
it "shows a warning and displays original names and amounts and info the user entered" do
item = create(:item, organization: organization, name: "Brightbloom Seed")
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 0, organization: organization)
extra_item = create(:item, organization: organization, name: "Extra Item")
TestInventory.create_inventory(organization, { storage_location.id => { item.id => 0, extra_item.id => 1 } })
original_quantity = 100
donation = create(:donation, :with_items, item: item, item_quantity: original_quantity, organization: organization, storage_location: storage_location)
original_source = Donation::SOURCES[:manufacturer]
original_date = DateTime.new(2024)
donation = create(:manufacturer_donation, :with_items, item: item, item_quantity: original_quantity, issued_at: original_date, organization: organization, storage_location: storage_location, source: original_source)
distribution = {
storage_location_id: storage_location.id,
partner_id: create(:partner).id,
Expand All @@ -192,26 +196,52 @@

post distributions_path(distribution: distribution, format: :turbo_stream)

edited_source = Donation::SOURCES[:product_drive]
edited_source_drive = create(:product_drive, organization: organization)
edited_source_drive_participant = create(:product_drive_participant, organization: organization)
edited_storage_location = create(:storage_location, name: "Test Storage", organization: organization)
edited_money = 10.0
edited_comment = "New test comment"
edited_date = "2019-01-01"
extra_quantity = 1
edited_quantity = 1

edited_donation = {
storage_location_id: storage_location.id,
source: edited_source,
product_drive_id: edited_source_drive.id,
product_drive_participant_id: edited_source_drive_participant.id,
storage_location_id: edited_storage_location.id,
money_raised_in_dollars: edited_money,
comment: edited_comment,
issued_at: edited_date,
line_items_attributes: {
"0": { item_id: item.id, quantity: 1 }
"0": { item_id: item.id, quantity: edited_quantity },
"1": { item_id: extra_item.id, quantity: extra_quantity }
}
}
put donation_path(id: donation.id, donation: edited_donation)

expect(response).to redirect_to(edit_donation_path(donation))

get response.headers['Location']
put donation_path(id: donation.id, donation: edited_donation)

if Event.read_events?(organization)
expect(flash[:alert]).to include("Error updating donation: Could not reduce quantity by 99 - current quantity is 10 for Brightbloom Seed")
else # TODO remove this branch when switching to events
expect(flash[:alert]).to include("Error updating donation: Requested items exceed the available inventory")
end

expect(response.body).to include(item.name)
expect(response.body).to include(original_quantity.to_s)
expect(response.body).to include("Edit - Donations - #{original_source}")
expect(response.body).to include("Editing Donation\n <small>from #{original_source}")
expect(response.body).to include("<li class=\"breadcrumb-item\">\n <a href=\"#\">Editing #{original_source}")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_source}\">#{edited_source}</option>")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_source_drive.id}\">#{edited_source_drive.name}</option>")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_source_drive_participant.id}\">#{edited_source_drive_participant.business_name}</option>")
expect(response.body).to include("<option selected=\"selected\" value=\"#{edited_storage_location.id}\">#{edited_storage_location.name}</option>")
expect(response.body).to include(edited_comment)
expect(response.body).to include("value=\"#{edited_money}\" type=\"text\" name=\"donation[money_raised_in_dollars]")
expect(response.body).to include(edited_date)
expect(response.body).to include("<option selected=\"selected\" value=\"1\">#{item.name}</option>")
expect(response.body).to include("value=\"#{edited_quantity}\" name=\"donation[line_items_attributes][0][quantity]")
expect(response.body).to include("<option selected=\"selected\" value=\"2\">#{extra_item.name}</option>")
expect(response.body).to include("value=\"#{extra_quantity}\" name=\"donation[line_items_attributes][1][quantity]")
end
end
end
Expand Down

0 comments on commit 672e9b6

Please sign in to comment.