Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add row for grand totals to storage locations index view #4768

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/views/storage_locations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,21 @@
<th>Address</th>
<th>Square Footage</th>
<th>Warehouse Type</th>
<th>Total Inventory</th>
<th>Fair Market Value</th>
<th class="text-right">Total Inventory</th>
<th class="text-right">Fair Market Value</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<%= render partial: "storage_location_row", collection: @storage_locations, as: :storage_location %>
<tr>
<td colspan="4"><strong>Total</strong></td>
<td class="text-right">
<%= @storage_locations.sum { |sl| @inventory.quantity_for(storage_location: sl.id) } %>
</td>
<td class="text-right"><%= number_to_currency(@storage_locations.sum(&:inventory_total_value_in_dollars)) %></td>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pass @inventory to that method so it isn't recalculating it each time.

<td></td>
</tr>
</tbody>
</table>
</div>
Expand Down
25 changes: 25 additions & 0 deletions spec/requests/storage_locations_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
address: "123 Donation Site Way",
warehouse_type: StorageLocation::WAREHOUSE_TYPES.first)
end
let!(:storage_location2) do
create(:storage_location,
name: "Test Storage Location 1",
address: "123 Donation Site Way",
warehouse_type: StorageLocation::WAREHOUSE_TYPES.first)
end

context "html" do
let(:response_format) { 'html' }
Expand All @@ -24,6 +30,25 @@
expect(response).to be_successful
end

it "displays grand total across storage locations" do
item1 = create(:item, name: "Item A", value_in_cents: 100)
item2 = create(:item, name: "Item B", value_in_cents: 200)

TestInventory.create_inventory(storage_location.organization,
{ storage_location.id => { item1.id => 2, item2.id => 1 } })
TestInventory.create_inventory(storage_location2.organization,
{ storage_location2.id => { item1.id => 3 } })
get storage_locations_path(format: response_format)
page = Nokogiri::HTML(response.body)

total_row = page.at("table tbody tr:last-child")
total_inventory = total_row.css("td.text-right")[0].text.strip
total_value = total_row.css("td.text-right")[1].text.strip

expect(total_inventory).to eq("6")
expect(total_value).to eq("$7.00")
end

context "with inactive locations" do
let!(:discarded_storage_location) { create(:storage_location, name: "Some Random Location", discarded_at: rand(10.years).seconds.ago) }

Expand Down
21 changes: 19 additions & 2 deletions spec/system/storage_location_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@
end
end

it "displays the correct grand totals across all storage locations" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a request spec, not a system spec since there's no interaction. Request specs are a lot faster and more lightweight.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed now that we have the request spec?

item = create(:item, name: "Needle", value_in_cents: 100)

location1 = create(:storage_location, name: "Location 1")
create(:donation, :with_items, item: item, item_quantity: 30, storage_location: location1)

location2 = create(:storage_location, name: "Location 2")
create(:donation, :with_items, item: item, item_quantity: 70, storage_location: location2)

visit subject

within "tbody tr:last-child" do
expect(page).to have_content(100) # The expected total inventory
expect(page).to have_content("$100.00") # The expected total fair market value
end
Comment on lines +106 to +109
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to test the totals show up as expected without writing a system test?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! You can look at some other request tests to see how it parses the rendered body and checks for elements.

end

it "User can filter the #index by those that contain certain items" do
item = create(:item, name: Faker::Lorem.unique.word)
create(:item, name: Faker::Lorem.unique.word)
Expand All @@ -103,15 +120,15 @@
select item.name, from: "filters[containing]"
click_button "Filter"

expect(page).to have_css("table tr", count: 2)
expect(page).to have_css("table tr", count: 3)
expect(page).to have_xpath("//table/tbody/tr/td", text: location1.name)
expect(page).not_to have_xpath("//table/tbody/tr/td", text: location2.name)
expect(page).not_to have_xpath("//table/tbody/tr/td", text: location3.name)

check "include_inactive_storage_locations"
click_button "Filter"

expect(page).to have_css("table tr", count: 3)
expect(page).to have_css("table tr", count: 4)
expect(page).to have_xpath("//table/tbody/tr/td", text: location3.name)
end

Expand Down
Loading