Files
sure/test/system/transactions_form_exchange_rate_test.rb
William Wei Ming b613d107be Fix N+1 queries on categories index by batching lookups and removing … (#2163)
* Fix N+1 queries on categories index by batching lookups and removing partial fallbacks

* resolve Codex review suggestion about Guard subcategory against missing parents

* resolve coderabbitai review suggestion - Guard against empty categories when computing category_ids_with_transactions

* resolve coderabbitai review suggestion - Redundant pb-4 makes the conditional dead code

* resolve coderabbitai caution on PR review

* resolve sure-design review - DS Drift Patrol

* fix conflicting hidden/flex classes

* resolve jjmata review suggestion - schema.rb noise

* fix conflict and adjust related parts

* Ignore Brakeman EOLRails warning for Rails 7.2
Restore ignore entry lost during merge from main; documents upgrade
tracking and matches brakeman 7.1.2 in Gemfile.lock.

* db migration executed

* Remove deprecated focus-ring override from goals color picker.
The summary_class override was reintroduced during merge conflict
resolution and clobbered DS::Disclosure's canonical focus styling.

* fix merge conflict on disclosure.rb

* Restore parent-based semantics while keeping the performance win for root categories

* fix(ci): skip scheduled preview cleanup on forks
Only run the hourly Cloudflare preview cleanup on we-promise/sure,
where the required secrets exist.

* Revert schema.rb dump noise unrelated to categories N+1 fix

Restore db/schema.rb from main so PostgreSQL check-constraint and
column-order churn does not obscure the real PR changes.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Remove obsolete Rails 7.2 EOLRails Brakeman ignore

Main is already on Rails 8.1, so the 7.2.3.1 ignore entry is no longer needed.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address review: bare disclosure docs, category picker aria label

* fix(test): wait for async exchange rate updates in system test

* fix(test): retry account edit submit around Turbo morph races

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-21 10:00:15 +02:00

152 lines
5.3 KiB
Ruby

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 is fetched asynchronously — wait for the field value.
assert_equal "1.27", wait_for_exchange_rate_value("1.27")
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 (async fetch)
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
first_rate = wait_for_exchange_rate_value("1.10")
# Change to GBP
find("select[data-money-field-target='currency']").find("option[value='GBP']").select_option
# Wait for the field to flip from EUR→GBP; reading immediately is flaky
# because the previous value stays until the fetch completes.
assert_selector "[data-transaction-form-target='exchangeRateContainer']", visible: true
second_rate = wait_for_exchange_rate_value("1.27")
# 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
wait_for_exchange_rate_value("1.10")
# 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
private
# JS sets the input's value property after an async fetch. Waiting on the
# HTML value= attribute is unreliable, so filter the node by .value.
def wait_for_exchange_rate_value(expected)
find("[data-transaction-form-target='exchangeRateField']") { |node| node.value == expected }.value
end
end