2024-02-23 21:34:33 -05:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class TransactionsControllerTest < ActionDispatch::IntegrationTest
|
2025-04-14 11:40:34 -04:00
|
|
|
include EntryableResourceInterfaceTest, EntriesTestHelper
|
2024-07-10 11:22:59 -04:00
|
|
|
|
2024-02-23 21:34:33 -05:00
|
|
|
setup do
|
2024-02-27 12:43:49 -05:00
|
|
|
sign_in @user = users(:family_admin)
|
2025-04-14 11:40:34 -04:00
|
|
|
@entry = entries(:transaction)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "creates with transaction details" do
|
|
|
|
assert_difference [ "Entry.count", "Transaction.count" ], 1 do
|
|
|
|
post transactions_url, params: {
|
|
|
|
entry: {
|
|
|
|
account_id: @entry.account_id,
|
|
|
|
name: "New transaction",
|
|
|
|
date: Date.current,
|
|
|
|
currency: "USD",
|
|
|
|
amount: 100,
|
|
|
|
nature: "inflow",
|
|
|
|
entryable_type: @entry.entryable_type,
|
|
|
|
entryable_attributes: {
|
|
|
|
tag_ids: [ Tag.first.id, Tag.second.id ],
|
|
|
|
category_id: Category.first.id,
|
|
|
|
merchant_id: Merchant.first.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
created_entry = Entry.order(:created_at).last
|
|
|
|
|
|
|
|
assert_redirected_to account_url(created_entry.account)
|
|
|
|
assert_equal "Transaction created", flash[:notice]
|
|
|
|
assert_enqueued_with(job: SyncJob)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updates with transaction details" do
|
|
|
|
assert_no_difference [ "Entry.count", "Transaction.count" ] do
|
|
|
|
patch transaction_url(@entry), params: {
|
|
|
|
entry: {
|
|
|
|
name: "Updated name",
|
|
|
|
date: Date.current,
|
|
|
|
currency: "USD",
|
|
|
|
amount: 100,
|
|
|
|
nature: "inflow",
|
|
|
|
entryable_type: @entry.entryable_type,
|
|
|
|
notes: "test notes",
|
|
|
|
excluded: false,
|
|
|
|
entryable_attributes: {
|
|
|
|
id: @entry.entryable_id,
|
|
|
|
tag_ids: [ Tag.first.id, Tag.second.id ],
|
|
|
|
category_id: Category.first.id,
|
|
|
|
merchant_id: Merchant.first.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@entry.reload
|
|
|
|
|
|
|
|
assert_equal "Updated name", @entry.name
|
|
|
|
assert_equal Date.current, @entry.date
|
|
|
|
assert_equal "USD", @entry.currency
|
|
|
|
assert_equal -100, @entry.amount
|
|
|
|
assert_equal [ Tag.first.id, Tag.second.id ], @entry.entryable.tag_ids.sort
|
|
|
|
assert_equal Category.first.id, @entry.entryable.category_id
|
|
|
|
assert_equal Merchant.first.id, @entry.entryable.merchant_id
|
|
|
|
assert_equal "test notes", @entry.notes
|
|
|
|
assert_equal false, @entry.excluded
|
|
|
|
|
|
|
|
assert_equal "Transaction updated", flash[:notice]
|
|
|
|
assert_redirected_to account_url(@entry.account)
|
|
|
|
assert_enqueued_with(job: SyncJob)
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
test "transaction count represents filtered total" do
|
|
|
|
family = families(:empty)
|
2025-01-31 11:29:49 -06:00
|
|
|
sign_in users(:empty)
|
2024-07-17 14:18:12 -04:00
|
|
|
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
3.times do
|
|
|
|
create_transaction(account: account)
|
2024-06-24 11:58:39 -04:00
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
get transactions_url(per_page: 10)
|
2024-04-24 21:55:52 +02:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
assert_dom "#total-transactions", count: 1, text: family.entries.transactions.size.to_s
|
2024-07-10 11:22:59 -04:00
|
|
|
|
|
|
|
searchable_transaction = create_transaction(account: account, name: "Unique test name")
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
get transactions_url(q: { search: searchable_transaction.name })
|
2024-06-24 11:58:39 -04:00
|
|
|
|
|
|
|
# Only finds 1 transaction that matches filter
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_dom "#" + dom_id(searchable_transaction), count: 1
|
2024-06-24 11:58:39 -04:00
|
|
|
assert_dom "#total-transactions", count: 1, text: "1"
|
2024-04-24 21:55:52 +02:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
test "can paginate" do
|
|
|
|
family = families(:empty)
|
2025-01-31 11:29:49 -06:00
|
|
|
sign_in users(:empty)
|
2024-07-17 14:18:12 -04:00
|
|
|
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
|
2024-07-10 11:22:59 -04:00
|
|
|
|
|
|
|
11.times do
|
|
|
|
create_transaction(account: account)
|
|
|
|
end
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
sorted_transactions = family.entries.transactions.reverse_chronological.to_a
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_equal 11, sorted_transactions.count
|
|
|
|
|
|
|
|
get transactions_url(page: 1, per_page: 10)
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
sorted_transactions.first(10).each do |transaction|
|
2024-06-24 11:58:39 -04:00
|
|
|
assert_dom "#" + dom_id(transaction), count: 1
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
get transactions_url(page: 2, per_page: 10)
|
2024-06-24 11:58:39 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_dom "#" + dom_id(sorted_transactions.last), count: 1
|
|
|
|
|
|
|
|
get transactions_url(page: 9999999, per_page: 10) # out of range loads last page
|
|
|
|
|
|
|
|
assert_dom "#" + dom_id(sorted_transactions.last), count: 1
|
2024-04-24 21:55:52 +02:00
|
|
|
end
|
2024-02-23 21:34:33 -05:00
|
|
|
end
|