Skip to content

Commit dfaa0e7

Browse files
committed
Add to rspec to test if deleted items persist after validation error
1 parent 82a76fd commit dfaa0e7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

spec/requests/donations_requests_spec.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,22 @@
182182
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 0, organization: organization)
183183
extra_item_name = "Extra Item"
184184
extra_item = create(:item, organization: organization, name: extra_item_name)
185+
item_to_delete_name = "Item To Delete"
186+
item_to_delete = create(:item, organization: organization, name: item_to_delete_name)
185187
TestInventory.create_inventory(organization, { storage_location.id => { item.id => 0, extra_item.id => 1 } })
186188
original_quantity = 100
187189
original_source = Donation::SOURCES[:manufacturer]
188190
original_date = DateTime.new(2024)
189-
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)
191+
donation = create(:manufacturer_donation, issued_at: original_date, organization: organization, storage_location: storage_location, source: original_source)
192+
add_item_donation = donation.as_json.merge({
193+
line_items_attributes: {
194+
"0": { item_id: item.id, quantity: original_quantity },
195+
"1": { item_id: item_to_delete.id, quantity: 1 }
196+
}
197+
})
198+
199+
put donation_path(id: donation.id, donation: add_item_donation)
200+
190201
distribution = {
191202
storage_location_id: storage_location.id,
192203
partner_id: create(:partner).id,
@@ -211,6 +222,7 @@
211222
extra_quantity = 1
212223
edited_quantity = 1
213224

225+
# deleted item is removed from line_items_attributes entirely
214226
edited_donation = {
215227
source: edited_source,
216228
product_drive_id: edited_source_drive.id,
@@ -247,6 +259,7 @@
247259
expect(response.body).to include("value=\"#{edited_quantity}\" name=\"donation[line_items_attributes][0][quantity]")
248260
expect(response.body).to include("<option selected=\"selected\" value=\"#{extra_item.id}\">#{extra_item_name}</option>")
249261
expect(response.body).to include("value=\"#{extra_quantity}\" name=\"donation[line_items_attributes][1][quantity]")
262+
expect(response.body).not_to include("<option selected=\"selected\" value=\"#{item_to_delete.id}\">#{item_to_delete_name}</option>")
250263
end
251264
end
252265
end

0 commit comments

Comments
 (0)