The personal finance app for everyone (by everyone)
Ruby
Prevent long category labels from overflowing or crowding adjacent controls (#1498)
Copilot3199c9b76d9a
diff --git a/app/components/DS/button.html.erb b/app/components/DS/button.html.erbindex 0b69c4649c..557db0bd98 100644--- a/app/components/DS/button.html.erb+++ b/app/components/DS/button.html.erb@@ -4,7 +4,7 @@<% end %><% unless icon_only? %>- <%= text %>+ <span class="min-w-0 truncate"><%= text %></span><% end %><% if icon && icon_position == :right %>diff --git a/app/views/categories/_badge.html.erb b/app/views/categories/_badge.html.erbindex 10135c251e..5570f2c704 100644--- a/app/views/categories/_badge.html.erb+++ b/app/views/categories/_badge.html.erb@@ -1,15 +1,17 @@<%# locals: (category:) %><% category ||= Category.uncategorized %>-<div>- <span class="flex items-center gap-1 text-sm font-medium rounded-full px-1.5 py-1 border truncate focus-visible:outline-none focus-visible:ring-0"+<div class="min-w-0 w-full">+ <span class="flex w-full items-center gap-1 text-sm font-medium rounded-full px-1.5 py-1 border focus-visible:outline-none focus-visible:ring-0"style="background-color: color-mix(in oklab, <%= category.color %> 10%, transparent);border-color: color-mix(in oklab, <%= category.color %> 10%, transparent);color: <%= category.color %>;"><% if category.lucide_icon.present? %>- <%= icon category.lucide_icon, size: "sm", color: "current" %>+ <span class="shrink-0">+ <%= icon category.lucide_icon, size: "sm", color: "current" %>+ </span><% end %>- <%= category.name %>+ <span class="min-w-0 flex-1 truncate" data-testid="category-name"><%= category.name %></span></span></div>diff --git a/app/views/categories/_category.html.erb b/app/views/categories/_category.html.erbindex dbc9b80fe6..1460fe5d60 100644--- a/app/views/categories/_category.html.erb+++ b/app/views/categories/_category.html.erb@@ -1,7 +1,7 @@<%# locals: (category:) %>-<div id="<%= dom_id(category) %>" class="flex justify-between items-center px-4 pb-4 <%= "pt-4" unless category.subcategory? %> <%= "pb-4" unless category.subcategories.any? %> bg-container">- <div class="flex w-full items-center gap-2.5">+<div id="<%= dom_id(category) %>" class="flex items-center gap-3 px-4 pb-4 <%= "pt-4" unless category.subcategory? %> <%= "pb-4" unless category.subcategories.any? %> bg-container">+ <div class="flex min-w-0 flex-1 items-center gap-2.5" data-testid="category-content"><% if category.subcategory? %><span style="color: <%= category.color %>"><%= icon "corner-down-right", size: "sm", color: "current", class: "ml-2" %>@@ -11,7 +11,7 @@<%= render partial: "categories/badge", locals: { category: category } %></div>- <div class="justify-self-end">+ <div class="shrink-0" data-testid="category-actions"><%= render DS::Menu.new do |menu| %><% menu.with_item(variant: "link", text: t(".edit"), icon: "pencil", href: edit_category_path(category), data: { turbo_frame: :modal }) %>diff --git a/app/views/categories/_category_name_mobile.html.erb b/app/views/categories/_category_name_mobile.html.erbindex 2f9bd07cbb..36c13021c7 100644--- a/app/views/categories/_category_name_mobile.html.erb+++ b/app/views/categories/_category_name_mobile.html.erb@@ -1,4 +1,4 @@-<span id="category_name_mobile_<%= transaction.id %>" class="text-secondary lg:hidden">+<span id="category_name_mobile_<%= transaction.id %>" class="text-secondary lg:hidden min-w-0 truncate"><% if transaction.transfer&.categorizable? || transaction.transfer.nil? %><%= transaction.category&.name || Category.uncategorized.name %><% else %>diff --git a/app/views/categories/_menu.html.erb b/app/views/categories/_menu.html.erbindex ad7dc4f5c4..6af5a8ce8c 100644--- a/app/views/categories/_menu.html.erb+++ b/app/views/categories/_menu.html.erb@@ -1,8 +1,8 @@<%# locals: (transaction:) %><%= render DS::Menu.new(variant: "button") do |menu| %>- <% menu.with_button do %>- <div class="hidden lg:flex">+ <% menu.with_button(class: "block w-full overflow-hidden") do %>+ <div class="hidden min-w-0 w-full lg:flex"><%= render partial: "categories/badge", locals: { category: transaction.category } %></div><div class="flex lg:hidden">diff --git a/app/views/transactions/_transaction.html.erb b/app/views/transactions/_transaction.html.erbindex 184bb2536c..b0ee1ae0ab 100644--- a/app/views/transactions/_transaction.html.erb+++ b/app/views/transactions/_transaction.html.erb@@ -144,10 +144,10 @@data: { turbo_frame: "_top" },class: "hover:underline" %></span>- <div class="flex items-center gap-1 truncate">+ <div class="flex min-w-0 items-center gap-1"><%= render "categories/category_name_mobile", transaction: transaction %><% if transaction.merchant&.present? %>- <span class="lg:hidden truncate">• <%= transaction.merchant.name %></span>+ <span class="lg:hidden min-w-0 truncate">• <%= transaction.merchant.name %></span><% end %></div><% end %>@@ -158,7 +158,7 @@</div></div>- <div class="hidden md:flex items-center gap-1 col-span-2">+ <div class="hidden md:flex min-w-0 items-center gap-1 col-span-2"><% if entry.account.investment? && !transaction.transfer? %><%# For investment accounts, show activity label instead of category %><%= render "investment_activity/quick_edit_badge", entry: entry, entryable: transaction %>diff --git a/app/views/transactions/_transaction_category.html.erb b/app/views/transactions/_transaction_category.html.erbindex 3b5c8d1b90..434e422af0 100644--- a/app/views/transactions/_transaction_category.html.erb+++ b/app/views/transactions/_transaction_category.html.erb@@ -1,6 +1,6 @@<%# locals: (transaction:, variant:) %>-<div id="<%= dom_id(transaction, "category_menu_#{variant}") %>">+<div id="<%= dom_id(transaction, "category_menu_#{variant}") %>" class="min-w-0 overflow-hidden"><% if transaction.transfer&.categorizable? || transaction.transfer.nil? %><%= render "categories/menu", transaction: transaction %><% else %>diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rbindex 313c12efec..770b6256e9 100644--- a/test/application_system_test_case.rb+++ b/test/application_system_test_case.rb@@ -2,6 +2,9 @@ require "test_helper"require "socket"class ApplicationSystemTestCase < ActionDispatch::SystemTestCase+ DEFAULT_VIEWPORT_WIDTH = 1400+ DEFAULT_VIEWPORT_HEIGHT = 1400+setup doCapybara.default_max_wait_time = 5@@ -14,6 +17,8 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCaseCapybara.always_include_port = trueCapybara.app_host = "http://#{app_host}:#{server_port}"end++ reset_viewportendif ENV["SELENIUM_REMOTE_URL"].present?@@ -34,8 +39,17 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCasedriven_by :selenium, using: ENV["CI"].present? ? :headless_chrome : ENV.fetch("E2E_BROWSER", :chrome).to_sym, screen_size: [ 1400, 1400 ]end+ def teardown+ reset_viewport+ super+ end+private+ def reset_viewport+ page.current_window.resize_to(DEFAULT_VIEWPORT_WIDTH, DEFAULT_VIEWPORT_HEIGHT) if page&.current_window+ end+def sign_in(user)visit new_session_pathwithin %(form[action='#{sessions_path}']) dodiff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 74514a68c3..cda1e81e70 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -50,6 +50,31 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_select "a[href*='page=2'][href*='tab=holdings']", count: 0end+ test "account activity constrains long category labels before the amount on wide screens" do+ category = categories(:food_and_drink)+ category.update!(name: "Super Long Category Name That Should Stop Before The Amount On Wide Screens Too")++ entry = @account.entries.create!(+ name: "Wide category verification",+ date: Date.current,+ amount: 187.65,+ currency: @account.currency,+ entryable: Transaction.new(category: category)+ )++ get account_url(@account, tab: "activity")++ assert_response :success+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")}"+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")}.min-w-0"+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")}.overflow-hidden"+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")} button.block"+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")} button.w-full"+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")} button.overflow-hidden"+ assert_select "##{dom_id(entry.entryable, "category_menu_desktop")} [data-testid='category-name']"+ assert_select "div.hidden.md\\:flex.min-w-0"+ end+test "should sync account" dopost sync_account_url(@account)assert_redirected_to account_url(@account)diff --git a/test/controllers/categories_controller_test.rb b/test/controllers/categories_controller_test.rbindex 2f7c8ff5ee..33bdf6641d 100644--- a/test/controllers/categories_controller_test.rb+++ b/test/controllers/categories_controller_test.rb@@ -4,11 +4,15 @@ class CategoriesControllerTest < ActionDispatch::IntegrationTestsetup dosign_in users(:family_admin)@transaction = transactions :one+ ensure_tailwind_buildendtest "index" doget categories_urlassert_response :success+ assert_select "#category_#{categories(:food_and_drink).id} > [data-testid='category-content']", count: 1+ assert_select "#category_#{categories(:food_and_drink).id} > [data-testid='category-actions']", count: 1+ assert_select "#category_#{categories(:food_and_drink).id} [data-testid='category-name']", text: categories(:food_and_drink).nameendtest "new" dodiff --git a/test/controllers/category/deletions_controller_test.rb b/test/controllers/category/deletions_controller_test.rbindex c8bd3d37cc..4cc5c166da 100644--- a/test/controllers/category/deletions_controller_test.rb+++ b/test/controllers/category/deletions_controller_test.rb@@ -4,11 +4,14 @@ class Category::DeletionsControllerTest < ActionDispatch::IntegrationTestsetup dosign_in users(:family_admin)@category = categories(:food_and_drink)+ ensure_tailwind_buildendtest "new" doget new_category_deletion_url(@category)assert_response :success+ assert_select "turbo-frame#modal"+ assert_select "turbo-frame#modal button span.min-w-0.truncate", text: /Delete "Food & Drink" and leave uncategorized/endtest "create with replacement" dodiff --git a/test/controllers/coinstats_items_controller_test.rb b/test/controllers/coinstats_items_controller_test.rbindex a3bfc24e00..13c1d7d4e9 100644--- a/test/controllers/coinstats_items_controller_test.rb+++ b/test/controllers/coinstats_items_controller_test.rb@@ -9,9 +9,7 @@ class CoinstatsItemsControllerTest < ActionDispatch::IntegrationTestname: "Test CoinStats Connection",api_key: "test_api_key_123")- tailwind_build = Rails.root.join("app/assets/builds/tailwind.css")- FileUtils.mkdir_p(tailwind_build.dirname)- File.write(tailwind_build, "/* test */") unless tailwind_build.exist?+ ensure_tailwind_buildend# Helper to wrap data in Provider::Responsediff --git a/test/system/account_activity_test.rb b/test/system/account_activity_test.rbindex 5349b8b102..1423143240 100644--- a/test/system/account_activity_test.rb+++ b/test/system/account_activity_test.rb@@ -1,8 +1,13 @@require "application_system_test_case"class AccountActivityTest < ApplicationSystemTestCase+ DEFAULT_VIEWPORT_WIDTH = 1400+ DEFAULT_VIEWPORT_HEIGHT = 1400+setup do+ ensure_tailwind_buildsign_in users(:family_admin)+ reset_viewport@account = accounts(:depository)@transaction_entry = @account.entries.create!(@@ -10,7 +15,7 @@ class AccountActivityTest < ApplicationSystemTestCasedate: Date.current,amount: 42.50,currency: "USD",- entryable: Transaction.new+ entryable: Transaction.new(category: categories(:food_and_drink)))@valuation_entry = @account.entries.create!(name: "Current balance",@@ -40,4 +45,74 @@ class AccountActivityTest < ApplicationSystemTestCaseassert_selector "a[title='Duplicate'].hidden", visible: falseendend++ test "account activity keeps long category names from overflowing the amount on mobile" do+ category = categories(:food_and_drink)+ category.update!(name: "Super Long Category Name That Should Stop Before The Amount On Mobile")++ page.current_window.resize_to(315, 643)++ visit account_url(@account, tab: "activity")++ row = find("##{dom_id(@transaction_entry)}")+ amount = row.find("p.privacy-sensitive", visible: true)+ category_name = row.find("#category_name_mobile_#{@transaction_entry.entryable.id}", visible: true)++ assert amount.visible?+ assert category_name.visible?++ row_rect = row.native.rect+ amount_rect = amount.native.rect+ viewport_width = page.evaluate_script("window.innerWidth")+ page_scroll_width = page.evaluate_script("document.documentElement.scrollWidth")++ assert_operator amount_rect.x + amount_rect.width, :<=, row_rect.x + row_rect.width+ assert_operator page_scroll_width, :<=, viewport_width+ end++ test "account activity keeps long category names from overlapping the amount on wide screens" do+ category = categories(:food_and_drink)+ category.update!(name: "Super Long Category Name That Should Stop Before The Amount On Wide Screens Too")++ page.current_window.resize_to(1280, 900)++ visit account_url(@account, tab: "activity")++ metrics = page.evaluate_script(<<~JS)+ (() => {+ const row = document.getElementById("#{dom_id(@transaction_entry)}");+ const categoryButton = row.querySelector("##{dom_id(@transaction_entry.entryable, "category_menu_desktop")} button");+ const categoryName = categoryButton.querySelector("[data-testid='category-name']");+ const amount = row.querySelector(".privacy-sensitive");+ const categoryRect = categoryButton.getBoundingClientRect();+ const amountRect = amount.getBoundingClientRect();++ return {+ categoryRight: categoryRect.right,+ amountLeft: amountRect.left,+ categoryOverflow: categoryName.scrollWidth > categoryName.clientWidth+ };+ })()+ JS++ assert_operator metrics["categoryRight"], :<=, metrics["amountLeft"]+ assert metrics["categoryOverflow"]+ end++ private+ def ensure_tailwind_build+ return if self.class.instance_variable_defined?(:@tailwind_css_built)++ system({ "RAILS_ENV" => "test" }, "bin/rails", "tailwindcss:build", exception: true)+ self.class.instance_variable_set(:@tailwind_css_built, true)+ end++ def teardown+ reset_viewport+ super+ end++ def reset_viewport+ page.current_window.resize_to(DEFAULT_VIEWPORT_WIDTH, DEFAULT_VIEWPORT_HEIGHT) if page&.current_window+ endenddiff --git a/test/system/categories_test.rb b/test/system/categories_test.rbindex 5e92f1ad4b..5a5bf17324 100644--- a/test/system/categories_test.rb+++ b/test/system/categories_test.rb@@ -23,4 +23,23 @@ class CategoriesTest < ApplicationSystemTestCaseassert_text "Name has already been taken"end++ test "long category names truncate before the actions menu on mobile" do+ category = categories(:food_and_drink)+ category.update!(name: "Super Long Category Name That Should Stop Before The Menu Button On Mobile")++ page.current_window.resize_to(315, 643)++ visit categories_url++ row = find("##{ActionView::RecordIdentifier.dom_id(category)}")+ actions = row.find("[data-testid='category-actions'] button", visible: true)++ assert actions.visible?++ viewport_width = page.evaluate_script("window.innerWidth")+ page_scroll_width = page.evaluate_script("document.documentElement.scrollWidth")++ assert_operator page_scroll_width, :<=, viewport_width+ endenddiff --git a/test/system/property_test.rb b/test/system/property_test.rbindex 769c338f48..b0a15f35de 100644--- a/test/system/property_test.rb+++ b/test/system/property_test.rb@@ -15,11 +15,7 @@ class PropertiesEditTest < ApplicationSystemTestCaseclick_link "[system test] Property Account"find("[data-testid='account-menu']").clickclick_on "Edit"- assert_selector "#account_accountable_attributes_subtype"- assert_selector(- "#account_accountable_attributes_subtype option[selected]",- text: "Single Family Home"- )+ assert_equal "single_family_home", find("#account_accountable_attributes_subtype").valueendprivatediff --git a/test/test_helper.rb b/test/test_helper.rbindex 5af3466ff0..0496412291 100644--- a/test/test_helper.rb+++ b/test/test_helper.rb@@ -81,6 +81,12 @@ module ActiveSupportpost sessions_path, params: { email: user.email, password: user_password_test }end+ def ensure_tailwind_build+ tailwind_build = Rails.root.join("app/assets/builds/tailwind.css")+ FileUtils.mkdir_p(tailwind_build.dirname)+ File.write(tailwind_build, "/* test */") unless tailwind_build.exist?+ end+def with_env_overrides(overrides = {}, &block)ClimateControl.modify(**overrides, &block)end