mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 03:54:08 +00:00
Add exchange rate feature with multi-currency transactions and transfers support (#1099)
Co-authored-by: Pedro J. Aramburu <pedro@joakin.dev>
This commit is contained in:
committed by
GitHub
parent
8e81e967fc
commit
f699660479
146
test/system/transactions_form_exchange_rate_test.rb
Normal file
146
test/system/transactions_form_exchange_rate_test.rb
Normal file
@@ -0,0 +1,146 @@
|
||||
require "application_system_test_case"
|
||||
|
||||
class TransactionsFormExchangeRateTest < ApplicationSystemTestCase
|
||||
setup do
|
||||
@user = users(:family_admin)
|
||||
@family = @user.family
|
||||
@account_usd = accounts(:depository) # USD account
|
||||
sign_in @user
|
||||
|
||||
# Set up real exchange rates for testing
|
||||
@eur_usd_rate = ExchangeRate.create!(
|
||||
from_currency: "EUR",
|
||||
to_currency: "USD",
|
||||
date: Date.current,
|
||||
rate: 1.1
|
||||
)
|
||||
|
||||
@gbp_usd_rate = ExchangeRate.create!(
|
||||
from_currency: "GBP",
|
||||
to_currency: "USD",
|
||||
date: Date.current,
|
||||
rate: 1.27
|
||||
)
|
||||
end
|
||||
|
||||
test "changing amount currency to different currency shows exchange rate UI" do
|
||||
visit new_transaction_path
|
||||
|
||||
# Select USD account (which is in USD)
|
||||
select_ds("Account", @account_usd)
|
||||
|
||||
# Currency defaults to USD (same as account)
|
||||
# Change currency to EUR
|
||||
find("select[data-money-field-target='currency']").find("option[value='EUR']").select_option
|
||||
|
||||
# Exchange rate UI should appear
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
end
|
||||
|
||||
test "changing amount currency to same as account currency hides exchange rate UI" do
|
||||
visit new_transaction_path
|
||||
|
||||
# Select USD account
|
||||
select_ds("Account", @account_usd)
|
||||
|
||||
# Change to EUR first
|
||||
find("select[data-money-field-target='currency']").find("option[value='EUR']").select_option
|
||||
|
||||
# Verify exchange rate UI is shown
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
|
||||
# Change back to USD (same as account)
|
||||
find("select[data-money-field-target='currency']").find("option[value='USD']").select_option
|
||||
|
||||
# Exchange rate UI should hide
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: false
|
||||
end
|
||||
|
||||
test "exchange rate field is prefilled when rate is available" do
|
||||
visit new_transaction_path
|
||||
|
||||
# Select USD account
|
||||
select_ds("Account", @account_usd)
|
||||
|
||||
# Change to GBP (exchange rate is set up in fixtures)
|
||||
find("select[data-money-field-target='currency']").find("option[value='GBP']").select_option
|
||||
|
||||
# Wait for exchange rate container to become visible
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
|
||||
# Exchange rate field should be populated
|
||||
exchange_rate_field = find("[data-transaction-form-target='exchangeRateField']")
|
||||
assert_not_empty exchange_rate_field.value
|
||||
assert_equal "1.27", exchange_rate_field.value
|
||||
end
|
||||
|
||||
test "exchange rate field is empty when rate not found" do
|
||||
visit new_transaction_path
|
||||
|
||||
# Select USD account
|
||||
select_ds("Account", @account_usd)
|
||||
|
||||
# Change to CHF (Swiss Franc - no rate set up in fixtures)
|
||||
find("select[data-money-field-target='currency']").find("option[value='CHF']").select_option
|
||||
|
||||
# Wait for exchange rate container to become visible (manual rate entry mode)
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
|
||||
# Exchange rate section should be visible but field should be empty (manual entry)
|
||||
exchange_rate_field = find("[data-transaction-form-target='exchangeRateField']")
|
||||
assert_empty exchange_rate_field.value
|
||||
end
|
||||
|
||||
test "exchange rate is recalculated when currency changes" do
|
||||
visit new_transaction_path
|
||||
|
||||
# Select USD account
|
||||
select_ds("Account", @account_usd)
|
||||
|
||||
# Change to EUR
|
||||
find("select[data-money-field-target='currency']").find("option[value='EUR']").select_option
|
||||
|
||||
# Wait for EUR rate to load
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
first_rate = find("[data-transaction-form-target='exchangeRateField']").value
|
||||
assert_equal "1.10", first_rate
|
||||
|
||||
# Change to GBP
|
||||
find("select[data-money-field-target='currency']").find("option[value='GBP']").select_option
|
||||
|
||||
# Wait for GBP rate to be updated
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
second_rate = find("[data-transaction-form-target='exchangeRateField']").value
|
||||
assert_equal "1.27", second_rate
|
||||
|
||||
# Rates should be different
|
||||
assert_not_equal first_rate, second_rate
|
||||
end
|
||||
|
||||
test "changing account also recalculates exchange rate for current currency" do
|
||||
# Create a second account in EUR
|
||||
eur_account = @family.accounts.create!(
|
||||
name: "EUR Account",
|
||||
balance: 1000,
|
||||
currency: "EUR",
|
||||
accountable: Depository.new
|
||||
)
|
||||
|
||||
visit new_transaction_path
|
||||
|
||||
# Start with USD account, then currency EUR
|
||||
select_ds("Account", @account_usd)
|
||||
|
||||
find("select[data-money-field-target='currency']").find("option[value='EUR']").select_option
|
||||
|
||||
# Exchange rate shown (both USD and EUR exist, they differ)
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
|
||||
|
||||
# Switch to EUR account
|
||||
select_ds("Account", eur_account)
|
||||
|
||||
# Now account is EUR and currency is EUR (same)
|
||||
# Exchange rate UI should hide
|
||||
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: false
|
||||
end
|
||||
end
|
||||
@@ -27,6 +27,58 @@ class TransfersTest < ApplicationSystemTestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "shows exchange rate field for different currencies" do
|
||||
# Create an account with a different currency
|
||||
eur_account = @user.family.accounts.create!(
|
||||
name: "EUR Savings",
|
||||
balance: 1000,
|
||||
currency: "EUR",
|
||||
accountable: Depository.new
|
||||
)
|
||||
|
||||
# Set up exchange rate
|
||||
ExchangeRate.create!(
|
||||
from_currency: "USD",
|
||||
to_currency: "EUR",
|
||||
date: Date.current,
|
||||
rate: 0.92
|
||||
)
|
||||
|
||||
transfer_date = Date.current
|
||||
|
||||
click_on "New transaction"
|
||||
click_on "Transfer"
|
||||
assert_text "New transfer"
|
||||
|
||||
# Initially, exchange rate field should be hidden
|
||||
assert_selector "[data-transfer-form-target='exchangeRateContainer'].hidden", visible: :all
|
||||
|
||||
# Select accounts with different currencies
|
||||
select_ds("From", accounts(:depository))
|
||||
select_ds("To", eur_account)
|
||||
|
||||
# Exchange rate container should become visible
|
||||
assert_selector "[data-transfer-form-target='exchangeRateContainer']", visible: true
|
||||
|
||||
# Exchange rate field should be populated with fetched rate
|
||||
exchange_rate_field = find("[data-transfer-form-target='exchangeRateField']")
|
||||
assert_not_empty exchange_rate_field.value
|
||||
assert_equal "0.92", exchange_rate_field.value
|
||||
|
||||
# Fill in amount
|
||||
fill_in "transfer[amount]", with: 100
|
||||
fill_in "Date", with: transfer_date
|
||||
|
||||
# Submit form
|
||||
click_button "Create transfer"
|
||||
|
||||
# Should redirect and show transfer created
|
||||
assert_current_path transactions_url
|
||||
within "#entry-group-#{transfer_date}" do
|
||||
assert_text "Transfer to"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def select_ds(label_text, record)
|
||||
|
||||
Reference in New Issue
Block a user