Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
8.1 KB
Raw Blame
require "application_system_test_case"

class CategoryRulePromptTest < ApplicationSystemTestCase
  include ActionView::RecordIdentifier

  setup do
    sign_in @user = users(:family_admin)
    @user.update!(rule_prompts_disabled: false, rule_prompt_dismissed_at: nil)
    @entry = @user.family.entries.transactions.order(date: :desc).first
    ruled_category_ids = Rule::Action.joins(:rule)
                                     .where(rules: { family_id: @user.family_id }, action_type: "set_transaction_category")
                                     .pluck(:value)
    # Filtered in Ruby: rule action values are free-form strings, and a non-UUID one
    # would turn a SQL NOT IN into NOT IN (NULL), which matches nothing.
    @categories = @user.family.categories.alphabetically.reject do |category|
      category.id.in?(ruled_category_ids) || category.id == @entry.entryable.category_id
    end.first(2)
    assert_equal 2, @categories.size, "fixtures need two categories without a rule"
    page.current_window.resize_to(1280, 900)
  end

  test "closing the prompt hides only that prompt and does not snooze future ones" do
    visit transactions_url
    assign_category @categories.first

    within "#cta" do
      assert_text "Updated to #{@categories.first.name}"
      assert_text "Always categorize similar transactions?"
    end

    # The X sits in the toast's top-right corner (not pushed down by the content stack).
    offsets = page.evaluate_script(<<~JS)
      (() => {
        const toast = document.querySelector("#cta > div");
        const close = toast.querySelector("button[aria-label='Close']");
        const t = toast.getBoundingClientRect(), c = close.getBoundingClientRect();
        return { top: c.top - t.top, right: t.right - c.right };
      })()
    JS
    assert_operator offsets["top"], :<=, 12
    assert_operator offsets["right"], :<=, 12

    within("#cta") { click_button "Close" }

    assert_no_text "Updated to #{@categories.first.name}"
    assert_nil @user.reload.rule_prompt_dismissed_at

    assign_category @categories.second
    assert_selector "#cta", text: "Updated to #{@categories.second.name}"
  end

  test "not today snoozes rule prompts" do
    visit transactions_url
    assign_category @categories.first

    within("#cta") { click_button "Not today" }

    assert_no_text "Updated to #{@categories.first.name}"
    assert_not_nil @user.reload.rule_prompt_dismissed_at
    assert_not @user.rule_prompts_disabled

    # While snoozed, the next eligible category change doesn't prompt either.
    assign_category @categories.second
    assert_no_text "Updated to #{@categories.second.name}"

    # Once the snooze is over a day old, prompts come back.
    @user.update!(rule_prompt_dismissed_at: 25.hours.ago)
    assign_category @categories.first
    assert_selector "#cta", text: "Updated to #{@categories.first.name}"
  end

  test "the prompt's buttons stay inside the toast when their labels are long" do
    # French has the longest labels of the shipped locales, and the desktop tray
    # is only 20rem wide, so the button row has to wrap rather than overflow.
    # A user-named category: system names such as "Income" are shown
    # translated, so assign_category could not search for them in French.
    category = @user.family.categories.create!(name: "Layout check", color: "#123456")
    @user.update!(locale: "fr")
    visit transactions_url
    assign_category category
    assert_selector "#cta form button", text: "Pas aujourd'hui"
    assert_selector "#cta form a", text: "Créer une règle"

    overflow = page.evaluate_script(<<~JS)
      (() => {
        const toast = document.querySelector("#cta > div").getBoundingClientRect();
        return [...document.querySelectorAll("#cta form a, #cta form button")]
          .map((el) => el.getBoundingClientRect())
          .filter((r) => r.left < toast.left - 0.5 || r.right > toast.right + 0.5).length;
      })()
    JS
    assert_equal 0, overflow, "every button must stay inside the toast"
  end

  test "turning rule suggestions off from the prompt can be undone in preferences" do
    visit transactions_url
    assign_category @categories.first

    within("#cta") { click_button "Turn off rule suggestions" }

    assert_no_text "Updated to #{@categories.first.name}"
    assert @user.reload.rule_prompts_disabled

    assign_category @categories.second
    assert_no_text "Updated to #{@categories.second.name}"

    visit settings_preferences_url
    assert_not find("#user_rule_suggestions", visible: :all).checked?
    find("label[for='user_rule_suggestions']").click
    assert_selector "#user_rule_suggestions:checked", visible: :all
    # The preferences form saves itself after the change, so give it a moment.
    Timeout.timeout(5) { sleep 0.1 while @user.reload.rule_prompts_disabled }
    assert_not @user.rule_prompts_disabled
    assert_nil @user.rule_prompt_dismissed_at, "turning suggestions off must not start the 24-hour snooze"

    # Back on, suggestions show again right away.
    visit transactions_url
    assign_category @categories.first
    assert_selector "#cta", text: "Updated to #{@categories.first.name}"
  end

  test "a prompt from the transaction drawer waits until the drawer closes" do
    visit transactions_url
    open_drawer @entry
    assign_category_in_drawer @categories.first

    # The drawer sits in the browser's top layer: a prompt rendered now would be hidden behind it.
    assert_no_selector "#cta *", visible: :all

    close_drawer
    assert_selector "#cta", text: "Updated to #{@categories.first.name}"
  end

  test "a held prompt survives the drawer being replaced by another one" do
    other_entry = @user.family.entries.transactions.where.not(id: @entry.id).order(date: :desc).first

    visit transactions_url
    open_drawer @entry
    assign_category_in_drawer @categories.first

    # Swapping the drawer's content removes the open dialog without a close event.
    open_drawer other_entry
    assert_no_selector "#cta *", visible: :all

    close_drawer
    assert_selector "#cta", text: "Updated to #{@categories.first.name}"
  end

  test "a held prompt shows when the open dialog is removed without closing" do
    visit transactions_url
    open_drawer @entry
    assign_category_in_drawer @categories.first
    assert_no_selector "#cta *", visible: :all

    # Like a stream replacing the modal frame with an empty one: no close event fires.
    page.execute_script(%(document.querySelector("turbo-frame#drawer").replaceChildren()))

    assert_selector "#cta", text: "Updated to #{@categories.first.name}"
  end

  test "a held prompt shows when the open attribute is removed from the dialog" do
    visit transactions_url
    open_drawer @entry
    assign_category_in_drawer @categories.first
    assert_no_selector "#cta *", visible: :all

    page.execute_script(%(document.querySelector("turbo-frame#drawer dialog[open]").removeAttribute("open")))

    assert_selector "#cta", text: "Updated to #{@categories.first.name}"
  end

  private
    def open_drawer(entry)
      page.execute_script <<~JS
        document.querySelector("turbo-frame#drawer").src = "#{transaction_url(entry)}"
      JS
      assert_selector "turbo-frame#drawer dialog[open] ##{dom_id(entry, :header)}"
    end

    def assign_category_in_drawer(category)
      within "turbo-frame#drawer [data-controller='category-select']" do
        find("button", match: :first).click
        find("button[data-category-id='#{category.id}']").click
      end
      assert_selector "##{dom_id(@entry)}", text: category.name
    end

    def close_drawer
      find("turbo-frame#drawer dialog[open]").send_keys(:escape)
      assert_no_selector "turbo-frame#drawer dialog[open]"
    end

    def assign_category(category)
      within "##{dom_id(@entry.entryable, 'category_menu_desktop')}" do
        find("button", match: :first).click
      end

      within "turbo-frame#category_dropdown" do
        find("input[type='search']").fill_in with: category.name
        find("##{dom_id(category, 'category_option')} button", match: :first).click
      end

      assert_selector "##{dom_id(@entry.entryable, 'category_menu_desktop')} [data-testid='category-name']", text: category.name
    end
end