Skip to content

Commit 8c562af

Browse files
committed
5428 add automated tests confirming vendor purchase order change
1 parent 586a8e1 commit 8c562af

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

app/views/vendors/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<tbody>
7777
<% @vendor.purchases.each do |purchase| %>
7878
<tr>
79-
<td><%= purchase.issued_at.strftime("%m/%d/%Y") %></td>
79+
<td data-testid="purchase-date"><%= purchase.issued_at.strftime("%m/%d/%Y") %></td>
8080
<td><%= purchase.line_items.total %></td>
8181
<td><%= view_button_to purchase, { text: "View purchase details" } %></td>
8282
</tr>

spec/models/vendor_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@
5656
end
5757
end
5858

59+
context "Associations" do
60+
describe "purchases" do
61+
it "orders purchases by issued_at in descending order (newest first)" do
62+
vendor = create(:vendor)
63+
64+
# Create purchases with different issued_at dates
65+
old_purchase = create(:purchase, vendor: vendor, issued_at: 1.week.ago)
66+
new_purchase = create(:purchase, vendor: vendor, issued_at: 1.day.ago)
67+
middle_purchase = create(:purchase, vendor: vendor, issued_at: 3.days.ago)
68+
69+
# Verify they're ordered newest first
70+
expect(vendor.purchases.to_a).to eq([new_purchase, middle_purchase, old_purchase])
71+
end
72+
end
73+
end
74+
5975
describe "versioning" do
6076
it { is_expected.to be_versioned }
6177
end

spec/system/vendor_system_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,23 @@
130130
expect(page).to have_xpath("//table/tbody/tr", count: 3)
131131
expect(page).to have_xpath("//table/tbody/tr/td", text: "10")
132132
end
133+
134+
it "displays purchases in reverse chronological order by issued_at date" do
135+
# Create purchases with specific issued_at dates
136+
old_purchase = create(:purchase, vendor: @vendor, issued_at: 1.week.ago)
137+
new_purchase = create(:purchase, vendor: @vendor, issued_at: 1.day.ago)
138+
middle_purchase = create(:purchase, vendor: @vendor, issued_at: 3.days.ago)
139+
140+
visit vendor_path(@vendor)
141+
142+
# Get all date text using the correct data attribute
143+
dates = page.all("[data-testid='purchase-date']").map(&:text)
144+
145+
# Get all purchases for this vendor and sort them by issued_at desc
146+
all_purchases = @vendor.reload.purchases.order(issued_at: :desc)
147+
expected_dates = all_purchases.map { |p| p.issued_at.strftime("%m/%d/%Y") }
148+
149+
expect(dates).to eq(expected_dates)
150+
end
133151
end
134152
end

0 commit comments

Comments
 (0)