Files
sure/test/components/UI/account/chart_test.rb
Antoine 🚀🏃‍♂️ 6b6ae0ca31 feat(accounts): add Gains / ROI chart view for investment accounts (#2660)
* feat(accounts): add Gains / ROI chart view for investment accounts

Adds a fourth chart view to the account details page showing the
historical unrealized gains series (market value - cost basis per
holding, summed daily with LOCF and FX conversion), following the
same ChartSeriesBuilder/Series pipeline as the existing views.

Holdings without a usable cost basis (nil, or unlocked zero from
providers) contribute zero gain, matching Holding#avg_cost semantics.

* fix(accounts): carry cost basis forward over gap-filled holdings in gains series

Gap-filled holding rows (weekends, price-history gaps) are persisted
without cost_basis even though the position and basis are unchanged,
which zeroed the gains series on those dates. Look up the basis from
the latest snapshot that has a usable one instead of reading it from
the current row, so already-persisted gap-filled rows are handled too.

* docs(accounts): add method docstrings for gains chart view code

Satisfies the pre-merge docstring coverage check on methods added or
touched by the Gains / ROI feature.

* fix(accounts): sign converted amount like main indicator in gains view

Extracts the gains sign-prefix logic into a shared signed_format helper
so the family-currency converted amount shown on foreign-currency
accounts matches the main indicator (+€79.53 / +$85.00), and adds
component tests covering the positive, negative, non-gains and
foreign-currency formatting paths.

* test(accounts): cover FX conversion path in gains series

The gains_series tests only used USD holdings against a USD target,
leaving the exchange_rates LATERAL join untested. Adds a case with EUR
holdings converted to USD, including LOCF rate carry-forward.

---------

Co-authored-by: Antoine GUYON <agy@ibanfirst.com>
2026-07-25 20:14:56 +02:00

58 lines
1.7 KiB
Ruby

require "test_helper"
class UI::Account::ChartTest < ViewComponent::TestCase
setup do
@account = accounts(:investment)
@account.holdings.destroy_all
end
test "renders positive gains with explicit plus sign" do
create_holding(cost_basis: 90)
render_inline(UI::Account::Chart.new(account: @account, view: "gains"))
assert_text "+$100.00"
end
test "does not sign non-gains views" do
component = UI::Account::Chart.new(account: @account, view: "balance")
assert_equal @account.balance_money.format, component.view_balance_display
refute component.view_balance_display.start_with?("+")
end
test "negative gains keep plain money formatting" do
create_holding(cost_basis: 110)
component = UI::Account::Chart.new(account: @account, view: "gains")
assert_equal "-$100.00", component.view_balance_display
end
test "converted amount is signed like the main indicator for foreign-currency accounts" do
@account.update!(currency: "EUR")
create_holding(cost_basis: 90)
ExchangeRate.create!(date: Date.current, from_currency: "EUR", to_currency: "USD", rate: 1.1)
component = UI::Account::Chart.new(account: @account, view: "gains")
assert_equal "+€100.00", component.view_balance_display
assert_equal "+$110.00", component.converted_balance_display
end
private
# 10 shares at $100 market price; gain = 1000 - cost_basis * 10
def create_holding(cost_basis:)
Holding.create!(
account: @account,
security: securities(:aapl),
date: Date.current,
qty: 10,
price: 100,
amount: 1000,
currency: @account.currency,
cost_basis: cost_basis
)
end
end