1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 21:29:38 +02:00
Maybe/test/controllers/transactions_controller_test.rb

190 lines
5.8 KiB
Ruby
Raw Normal View History

require "test_helper"
class TransactionsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@transaction = transactions(:checking_one)
feat: Transaction pagination Improvements (#873) * feat: make transaction container fixed height * feat: pagination per page query * fix: linting errors * Changelog page that pulls from Github Release notes (#867) * Changelog page that pulls from Github Release notes * Review changelog page styles * Move changelog page title to i18n translations * Bump to 0.1.0-alpha.6 Signed-off-by: Zach Gollwitzer <zach@maybe.co> * Bump aws-sdk-s3 from 1.152.0 to 1.152.3 (#880) Bumps [aws-sdk-s3](https://github.com/aws/aws-sdk-ruby) from 1.152.0 to 1.152.3. - [Release notes](https://github.com/aws/aws-sdk-ruby/releases) - [Changelog](https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-s3/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-ruby/commits) --- updated-dependencies: - dependency-name: aws-sdk-s3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump mocha from 2.3.0 to 2.4.0 (#878) Bumps [mocha](https://github.com/freerange/mocha) from 2.3.0 to 2.4.0. - [Changelog](https://github.com/freerange/mocha/blob/main/RELEASE.md) - [Commits](https://github.com/freerange/mocha/compare/v2.3.0...v2.4.0) --- updated-dependencies: - dependency-name: mocha dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump octokit from 8.1.0 to 9.1.0 (#877) Bumps [octokit](https://github.com/octokit/octokit.rb) from 8.1.0 to 9.1.0. - [Release notes](https://github.com/octokit/octokit.rb/releases) - [Changelog](https://github.com/octokit/octokit.rb/blob/main/RELEASE.md) - [Commits](https://github.com/octokit/octokit.rb/compare/v8.1.0...v9.1.0) --- updated-dependencies: - dependency-name: octokit dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump rails from `f9c847f` to `5d34172` (#879) Bumps [rails](https://github.com/rails/rails) from `f9c847f` to `5d34172`. - [Release notes](https://github.com/rails/rails/releases) - [Commits](https://github.com/rails/rails/compare/f9c847fac102039d9174106f44b59144da267751...5d34172ff44ec0c88ac03a979679b31e1ed78745) --- updated-dependencies: - dependency-name: rails dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Zach Gollwitzer <zach@maybe.co> * Update issue templates * Add merchant select when editing transaction (#885) * Transaction transfers, payments, and matching (#883) * Add transfer model and clean up family snapshot fixtures * Ignore transfers in income and expense snapshots * Add transfer validations * Implement basic transfer matching UI * Fix merge conflicts * Add missing translations * Tweak selection states for transfer types * Add missing i18n translation * Ensure correct form's hidden input for selectedIds (#891) * feat: make transaction container fixed height * feat: pagination per page query * fix: linting errors * Transaction transfers, payments, and matching (#883) * Add transfer model and clean up family snapshot fixtures * Ignore transfers in income and expense snapshots * Add transfer validations * Implement basic transfer matching UI * Fix merge conflicts * Add missing translations * Tweak selection states for transfer types * Add missing i18n translation * feat: make transaction container fixed height * feat: pagination per page query * fix: linting errors * revert unnecessary changes * revert unnecessary changes * code review changes * code review changes * code review changes * remove unused imports * fix: unit tests * remove border * fix: transaction padding * fix: transaction container height --------- Signed-off-by: Zach Gollwitzer <zach@maybe.co> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Karan Kiri <karankiri.96@gmail.com> Co-authored-by: Mattia <malnis.mattia@gmail.com> Co-authored-by: Zach Gollwitzer <zach@maybe.co> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> Co-authored-by: Jakub Kottnauer <jk@jakubkottnauer.com> Co-authored-by: ziraq young <ziraqyoung@outlook.com>
2024-06-21 21:34:40 +05:30
@recent_transactions = @user.family.transactions.ordered.limit(20).to_a
end
test "should get paginated index with most recent transactions first" do
get transactions_url
assert_response :success
@recent_transactions.first(10).each do |transaction|
assert_dom "#" + dom_id(transaction), count: 1
end
end
test "transaction count represents filtered total" do
get transactions_url
assert_dom "#total-transactions", count: 1, text: @user.family.transactions.select { |t| t.currency == "USD" }.count.to_s
new_transaction = @user.family.accounts.first.transactions.create! \
name: "Transaction to search for",
date: Date.current,
amount: 0
get transactions_url(q: { search: new_transaction.name })
# Only finds 1 transaction that matches filter
assert_dom "#" + dom_id(new_transaction), count: 1
assert_dom "#total-transactions", count: 1, text: "1"
end
test "can navigate to paginated result" do
get transactions_url(page: 2)
assert_response :success
feat: Transaction pagination Improvements (#873) * feat: make transaction container fixed height * feat: pagination per page query * fix: linting errors * Changelog page that pulls from Github Release notes (#867) * Changelog page that pulls from Github Release notes * Review changelog page styles * Move changelog page title to i18n translations * Bump to 0.1.0-alpha.6 Signed-off-by: Zach Gollwitzer <zach@maybe.co> * Bump aws-sdk-s3 from 1.152.0 to 1.152.3 (#880) Bumps [aws-sdk-s3](https://github.com/aws/aws-sdk-ruby) from 1.152.0 to 1.152.3. - [Release notes](https://github.com/aws/aws-sdk-ruby/releases) - [Changelog](https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-s3/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-ruby/commits) --- updated-dependencies: - dependency-name: aws-sdk-s3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump mocha from 2.3.0 to 2.4.0 (#878) Bumps [mocha](https://github.com/freerange/mocha) from 2.3.0 to 2.4.0. - [Changelog](https://github.com/freerange/mocha/blob/main/RELEASE.md) - [Commits](https://github.com/freerange/mocha/compare/v2.3.0...v2.4.0) --- updated-dependencies: - dependency-name: mocha dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump octokit from 8.1.0 to 9.1.0 (#877) Bumps [octokit](https://github.com/octokit/octokit.rb) from 8.1.0 to 9.1.0. - [Release notes](https://github.com/octokit/octokit.rb/releases) - [Changelog](https://github.com/octokit/octokit.rb/blob/main/RELEASE.md) - [Commits](https://github.com/octokit/octokit.rb/compare/v8.1.0...v9.1.0) --- updated-dependencies: - dependency-name: octokit dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump rails from `f9c847f` to `5d34172` (#879) Bumps [rails](https://github.com/rails/rails) from `f9c847f` to `5d34172`. - [Release notes](https://github.com/rails/rails/releases) - [Commits](https://github.com/rails/rails/compare/f9c847fac102039d9174106f44b59144da267751...5d34172ff44ec0c88ac03a979679b31e1ed78745) --- updated-dependencies: - dependency-name: rails dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Zach Gollwitzer <zach@maybe.co> * Update issue templates * Add merchant select when editing transaction (#885) * Transaction transfers, payments, and matching (#883) * Add transfer model and clean up family snapshot fixtures * Ignore transfers in income and expense snapshots * Add transfer validations * Implement basic transfer matching UI * Fix merge conflicts * Add missing translations * Tweak selection states for transfer types * Add missing i18n translation * Ensure correct form's hidden input for selectedIds (#891) * feat: make transaction container fixed height * feat: pagination per page query * fix: linting errors * Transaction transfers, payments, and matching (#883) * Add transfer model and clean up family snapshot fixtures * Ignore transfers in income and expense snapshots * Add transfer validations * Implement basic transfer matching UI * Fix merge conflicts * Add missing translations * Tweak selection states for transfer types * Add missing i18n translation * feat: make transaction container fixed height * feat: pagination per page query * fix: linting errors * revert unnecessary changes * revert unnecessary changes * code review changes * code review changes * code review changes * remove unused imports * fix: unit tests * remove border * fix: transaction padding * fix: transaction container height --------- Signed-off-by: Zach Gollwitzer <zach@maybe.co> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Karan Kiri <karankiri.96@gmail.com> Co-authored-by: Mattia <malnis.mattia@gmail.com> Co-authored-by: Zach Gollwitzer <zach@maybe.co> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> Co-authored-by: Jakub Kottnauer <jk@jakubkottnauer.com> Co-authored-by: ziraq young <ziraqyoung@outlook.com>
2024-06-21 21:34:40 +05:30
@recent_transactions[10, 10].select { |t| t.transfer_id == nil }.each do |transaction|
assert_dom "#" + dom_id(transaction), count: 1
end
end
test "loads last page when page is out of range" do
user_oldest_transaction = @user.family.transactions.ordered.reject(&:transfer?).last
get transactions_url(page: 9999999999)
assert_response :success
assert_dom "#" + dom_id(user_oldest_transaction), count: 1
end
test "should get new" do
get new_transaction_url
assert_response :success
end
test "prefills account_id if provided" do
get new_transaction_url(account_id: @transaction.account_id)
assert_response :success
assert_select "option[selected][value='#{@transaction.account_id}']"
end
test "should create transaction" do
account = @user.family.accounts.first
transaction_params = {
account_id: account.id,
amount: 100.45,
currency: "USD",
date: Date.current,
name: "Test transaction"
}
assert_difference("Transaction.count") do
post transactions_url, params: { transaction: transaction_params }
end
assert_equal transaction_params[:amount].to_d, Transaction.order(created_at: :desc).first.amount
assert_equal flash[:notice], "New transaction created successfully"
assert_enqueued_with(job: AccountSyncJob)
assert_redirected_to transactions_url
end
test "expenses are positive" do
assert_difference("Transaction.count") do
post transactions_url, params: { transaction: {
nature: "expense",
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name } }
end
assert_redirected_to transactions_url
assert Transaction.order(created_at: :desc).first.amount.positive?, "Amount should be positive"
end
test "incomes are negative" do
assert_difference("Transaction.count") do
post transactions_url, params: {
transaction: {
nature: "income",
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name
}
}
end
assert_redirected_to transactions_url
assert Transaction.order(created_at: :desc).first.amount.negative?, "Amount should be negative"
end
test "should show transaction" do
get transaction_url(@transaction)
assert_response :success
end
test "should update transaction" do
patch transaction_url(@transaction), params: {
transaction: {
account_id: @transaction.account_id,
amount: @transaction.amount,
currency: @transaction.currency,
date: @transaction.date,
name: @transaction.name,
tag_ids: [ Tag.first.id, Tag.second.id ]
}
}
assert_redirected_to transaction_url(@transaction)
assert_enqueued_with(job: AccountSyncJob)
end
test "should destroy transaction" do
assert_difference("Transaction.count", -1) do
delete transaction_url(@transaction)
end
assert_redirected_to transactions_url
assert_enqueued_with(job: AccountSyncJob)
end
test "can destroy many transactions at once" do
delete_count = 10
assert_difference("Transaction.count", -delete_count) do
post bulk_delete_transactions_url, params: { bulk_delete: { transaction_ids: @recent_transactions.first(delete_count).pluck(:id) } }
end
assert_redirected_to transactions_url
assert_equal "10 transactions deleted", flash[:notice]
end
test "can update many transactions at once" do
transactions = @user.family.transactions.ordered.limit(20)
transactions.each do |transaction|
transaction.update! \
excluded: false,
category_id: Category.first.id,
merchant_id: Merchant.first.id,
notes: "Starting note"
end
post bulk_update_transactions_url, params: {
bulk_update: {
date: Date.current,
transaction_ids: transactions.map(&:id),
excluded: true,
category_id: Category.second.id,
merchant_id: Merchant.second.id,
notes: "Updated note"
}
}
assert_redirected_to transactions_url
assert_equal "#{transactions.count} transactions updated", flash[:notice]
transactions.reload.each do |transaction|
assert_equal Date.current, transaction.date
assert transaction.excluded
assert_equal Category.second, transaction.category
assert_equal Merchant.second, transaction.merchant
assert_equal "Updated note", transaction.notes
end
end
end