Files
sure/test/controllers/splits_controller_test.rb
T
eb498cfa0c Feature/category hierarchy and account search (#2845)
* Add parent/child category hierarchy to all category selects; add search to account select

Category selection consistency:
- DS::Select (shared component powering the main transaction form,
  transaction edit, bulk-update, and transfer category pickers) now
  indents subcategories with a corner-down-right icon, matching the
  existing transaction-row category dropdown.
- Feed DS::Select-based category pickers with Category.alphabetically_by_hierarchy
  (parent name, then parent-before-children, then own name) so children
  render directly under their parent.
- Added Category::Group.select_options, a shared helper producing
  parent-then-child ordered options (with an indent marker) for plain
  HTML <select> elements. Used by:
  - Rule builder category condition/action selects
  - Bulk 'categorize transactions' select
  - CSV/QIF import category mapping select
- Grouped the splits category combobox and the transaction search
  category filter checklist the same way, both with the
  corner-down-right indent icon used elsewhere.

Account selection:
- Added searchable: true to the account select in the new/edit
  transaction form, matching the category and merchant selects next
  to it.

* Fix SyntaxError: 'for' is a Ruby reserved keyword

Category::Group.select_options called for(categories) as a bare method
call, but Ruby parses a bare 'for' as the start of a for..in loop
statement, not a method invocation. Qualify it as self.for(categories)
to call the class method explicitly.

Verified with 'ruby -c' on all touched .rb files and ERB.new(...).src
on all touched .erb files.

* Add test coverage for category hierarchy and account search

Model-level:
- Category::GroupTest (new): for() grouping and the new select_options
  helper (order + indent labels).
- CategoryTest: alphabetically_by_hierarchy scope ordering.
- Rule::ConditionFilter::TransactionCategoryTest (new)
- Rule::ActionExecutor::SetTransactionCategoryTest (new)
- Import::CategoryMappingTest (new): grouping + 'Add as new category'
  still prepends correctly.

Controller/integration-level (asserting actual rendered HTML order):
- SplitsControllerTest: category combobox data-value ordering.
- Transactions::CategorizesControllerTest: bulk-categorize <select>
  option ordering.
- TransactionsControllerTest:
  - search filter checkbox ordering (q[categories][])
  - new-transaction DS::Select category ordering (via trigger id +
    ancestor traversal)
  - new-transaction account select renders a search box

All new/modified test files verified with 'ruby -c' (syntax) and
cross-checked fixture names, family scoping, route helpers, and field
names against the actual fixtures/routes/views. Ruby/Bundler network
access to rubygems.org is unavailable in this sandbox, so the suite
itself has not been executed — run 'bin/rails test' before merging.

* Align with design-sure conventions: keep domain logic in component, not template

Per .cursor/rules/view_conventions.mdc ('keep domain logic out of the
views'), the parent/child hierarchy check for DS::Select items belongs
in the component class, not inline in the ERB template. DS::Select
already has this exact pattern for other per-item derived properties
(color_for, icon_for, logo_for) — added child? alongside them and
updated the template to call it instead of computing it inline.

Added test/components/DS/select_test.rb (ViewComponent::TestCase,
no rendering needed) covering child? directly: subcategory objects,
root-category objects, non-hierarchical objects (merchants), and the
include_blank placeholder item.

Also did a broader pass against the design-sure .cursor/rules to confirm
the rest of this branch's changes already comply:
- Uses Current.family (never current_family) throughout
- Uses the icon() helper exclusively, never lucide_icon directly
- No new/hardcoded colors; only existing semantic Tailwind tokens
  already used elsewhere in these same files
- No changes to sure-design-system.css / application.css
- Extended existing components/partials rather than creating new ones
  where one already existed (view_conventions.mdc component-vs-partial
  guidance)
- Test additions stay in Minitest + fixtures, avoid system tests,
  and test query-method output directly (testing.mdc)

* Address CodeRabbit review: sort category groups, tighten test assertion, move grouping out of view

* Address review: fix arrow leak in rule summaries, filter panel alignment, simplify splits ordering

* fix(pages): set breadcrumbs for changelog and feedback pages (#2889)

* fix(app): set breadcrumbs for changelog and feedback pages

* feat(test): add test to assert breadcrumbs

* fix(test): remove changes

* feat(app): update breadcrumbs to use semantic nav element

* feat(test): add breadcrumb assertions to changelog and feedback pages

* fix(app): replace breadcrumb nav element with div containing data-breadcrumbs attribute

* fix ci failures

* resolved failures

* Regenerate schema.rb from migrations

* Fix test

* Remove schema dump noise

---------

Signed-off-by: Shibu M <23173570+DataEnginr@users.noreply.github.com>
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: Kenrick Tandrian <60643640+KenTandrian@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-08-26 07:59:00 +02:00

269 lines
7.9 KiB
Ruby

require "test_helper"
class SplitsControllerTest < ActionDispatch::IntegrationTest
include EntriesTestHelper
setup do
sign_in @user = users(:family_admin)
@entry = create_transaction(
amount: 100,
name: "Grocery Store",
account: accounts(:depository)
)
end
test "new renders split editor" do
get new_transaction_split_path(@entry)
assert_response :success
end
test "new groups subcategories immediately after their parent in the category selector" do
get new_transaction_split_path(@entry)
assert_response :success
doc = Nokogiri::HTML::Document.parse(response.body)
category_values = doc.css(".category-select-container [data-value]").map { |node| node["data-value"] }
parent_index = category_values.index(categories(:food_and_drink).id)
child_index = category_values.index(categories(:subcategory).id)
assert_not_nil parent_index
assert_not_nil child_index
assert_equal parent_index + 1, child_index
end
test "create with valid params splits transaction" do
assert_difference "Entry.count", 2 do
post transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Groceries", amount: "-70", category_id: categories(:food_and_drink).id },
{ name: "Household", amount: "-30", category_id: "" }
]
}
}
end
assert_redirected_to transactions_url
assert_equal I18n.t("splits.create.success"), flash[:notice]
assert @entry.reload.excluded?
assert @entry.split_parent?
end
test "create with mismatched amounts rejects" do
assert_no_difference "Entry.count" do
post transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Part 1", amount: "-60", category_id: "" },
{ name: "Part 2", amount: "-20", category_id: "" }
]
}
}
end
assert_redirected_to transactions_url
assert flash[:alert].present?
end
test "destroy unsplits transaction" do
@entry.split!([
{ name: "Part 1", amount: 50, category_id: nil },
{ name: "Part 2", amount: 50, category_id: nil }
])
assert_difference "Entry.count", -2 do
delete transaction_split_path(@entry)
end
assert_redirected_to transactions_url
assert_equal I18n.t("splits.destroy.success"), flash[:notice]
refute @entry.reload.excluded?
end
test "create with income transaction applies correct sign" do
income_entry = create_transaction(
amount: -400,
name: "Reimbursement",
account: accounts(:depository)
)
assert_difference "Entry.count", 2 do
post transaction_split_path(income_entry), params: {
split: {
splits: [
{ name: "Part 1", amount: "200", category_id: "" },
{ name: "Part 2", amount: "200", category_id: "" }
]
}
}
end
assert income_entry.reload.excluded?
children = income_entry.child_entries
assert_equal(-200, children.first.amount.to_i)
assert_equal(-200, children.last.amount.to_i)
end
test "create with mixed sign amounts on expense" do
assert_difference "Entry.count", 2 do
post transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Main expense", amount: "-130", category_id: "" },
{ name: "Refund", amount: "30", category_id: "" }
]
}
}
end
assert @entry.reload.excluded?
children = @entry.child_entries.order(:amount)
assert_equal(-30, children.first.amount.to_i)
assert_equal 130, children.last.amount.to_i
end
test "only family members can access splits" do
other_family_entry = create_transaction(
amount: 100,
name: "Other",
account: accounts(:depository)
)
# This should work since both belong to same family
get new_transaction_split_path(other_family_entry)
assert_response :success
end
test "create with excluded parameter sets child as excluded" do
assert_difference "Entry.count", 2 do
post transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Groceries", amount: "-70", category_id: categories(:food_and_drink).id, excluded: "true" },
{ name: "Household", amount: "-30", category_id: "", excluded: "false" }
]
}
}
end
assert_redirected_to transactions_url
children = @entry.child_entries.order(:amount)
# Household has amount 30 (smaller), Groceries has amount 70 (larger)
# Household is NOT excluded, Groceries IS excluded
refute children.first.excluded?
assert children.last.excluded?
end
# Edit action tests
test "edit renders with existing children pre-filled" do
@entry.split!([
{ name: "Part 1", amount: 60, category_id: nil },
{ name: "Part 2", amount: 40, category_id: nil }
])
get edit_transaction_split_path(@entry)
assert_response :success
end
test "edit on a child redirects to parent edit" do
@entry.split!([
{ name: "Part 1", amount: 60, category_id: nil },
{ name: "Part 2", amount: 40, category_id: nil }
])
child = @entry.child_entries.first
get edit_transaction_split_path(child)
assert_response :success
end
test "edit on a non-split entry redirects with alert" do
get edit_transaction_split_path(@entry)
assert_redirected_to transactions_url
assert_equal I18n.t("splits.edit.not_split"), flash[:alert]
end
# Update action tests
test "update modifies split entries" do
@entry.split!([
{ name: "Part 1", amount: 60, category_id: nil },
{ name: "Part 2", amount: 40, category_id: nil }
])
patch transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Food", amount: "-50", category_id: categories(:food_and_drink).id },
{ name: "Transport", amount: "-30", category_id: "" },
{ name: "Other", amount: "-20", category_id: "" }
]
}
}
assert_redirected_to transactions_url
assert_equal I18n.t("splits.update.success"), flash[:notice]
@entry.reload
assert @entry.split_parent?
assert_equal 3, @entry.child_entries.count
end
test "update with mismatched amounts rejects" do
@entry.split!([
{ name: "Part 1", amount: 60, category_id: nil },
{ name: "Part 2", amount: 40, category_id: nil }
])
patch transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Part 1", amount: "-70", category_id: "" },
{ name: "Part 2", amount: "-20", category_id: "" }
]
}
}
assert_redirected_to transactions_url
assert flash[:alert].present?
# Original splits should remain intact
assert_equal 2, @entry.reload.child_entries.count
end
test "update with excluded parameter sets child as excluded" do
@entry.split!([
{ name: "Groceries", amount: 70, category_id: nil },
{ name: "Household", amount: 30, category_id: nil }
])
patch transaction_split_path(@entry), params: {
split: {
splits: [
{ name: "Groceries", amount: "-70", category_id: "", excluded: "true" },
{ name: "Household", amount: "-30", category_id: "", excluded: "false" }
]
}
}
assert_redirected_to transactions_url
children = @entry.child_entries.order(:amount)
refute children.first.excluded?
assert children.last.excluded?
end
# Destroy from child tests
test "destroy from child resolves to parent and unsplits" do
@entry.split!([
{ name: "Part 1", amount: 60, category_id: nil },
{ name: "Part 2", amount: 40, category_id: nil }
])
child = @entry.child_entries.first
assert_difference "Entry.count", -2 do
delete transaction_split_path(child)
end
assert_redirected_to transactions_url
assert_equal I18n.t("splits.destroy.success"), flash[:notice]
refute @entry.reload.excluded?
end
end