Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
7.5 KB
Raw Blame
require "application_system_test_case"

# A bill opens the way a transaction or a budget category does: in the drawer,
# over the list. Closing it has to put keyboard focus back on the row, or every
# bill someone checks sends them back to the top of the page.
class BillDrawerTest < ApplicationSystemTestCase
  setup do
    sign_in @user = users(:family_admin)
    @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))

    # Overdue, so its row and its drawer offer Find payment, and so its row is
    # the only link to this cycle: Next up lists nothing already past due.
    due = 6.days.ago.to_date
    @bill = bill = @user.family.recurring_transactions.create!(
      name: "City Water", account: accounts(:depository), amount: 80, currency: "USD",
      expected_day_of_month: due.day, last_occurrence_date: 2.months.ago.to_date,
      next_expected_date: due, status: "active"
    )
    overdue = bill.recurring_occurrences.detect(&:overdue?)
    @row_link = "a[data-turbo-frame='drawer'][href='#{bill_path(bill, display: "drawer", occurrence: overdue.id)}']"
    @find_payment_link = "a[data-turbo-frame='drawer'][href='#{recurring_occurrence_path(overdue)}']"
  end

  # The inline expansion used to push everything below it down the page. The
  # drawer opens over the list, so closing it leaves the row where it was.
  test "on a phone, a row opens its drawer, and closing it puts focus back on the row where it was" do
    # Enough rows below it that the list scrolls, or "where it was" is a
    # position nothing could have moved.
    8.times do |i|
      due = Date.current + 10 + i
      @user.family.recurring_transactions.create!(
        name: "Later bill #{i}", account: accounts(:depository), amount: 20 + i, currency: "USD",
        expected_day_of_month: due.day, last_occurrence_date: due - 1.month,
        next_expected_date: due, status: "active"
      )
    end
    page.driver.browser.manage.window.resize_to(375, 812)
    visit bills_url
    row = find(@row_link)
    scroll_to row, align: :center
    assert_operator page.evaluate_script("document.querySelector('#main').scrollTop"), :>, 0
    top = row.evaluate_script("this.getBoundingClientRect().top")

    row.click
    within("dialog[open]") do
      assert_selector "h2", text: "City Water"
      click_on I18n.t("ds.dialog.close")
    end

    assert_no_selector "dialog[open]"
    assert_selector @row_link, focused: true
    assert_equal top, row.evaluate_script("this.getBoundingClientRect().top"), "the list moved"
  ensure
    page.driver.browser.manage.window.resize_to(1400, 1400)
  end

  # A pasted link, a new tab or a notification has no list to open the drawer
  # over, so the overview renders around it. Closing it leaves the overview.
  test "a bill visited directly opens its drawer over the overview" do
    visit bill_url(@bill)

    within("dialog[open]") do
      assert_selector "h2", text: "City Water"
      # The deep part arrives once scrolled to, in the same drawer.
      scroll_to find("turbo-frame[src*='display=history']")
      assert_text(/#{I18n.t("bills.detail.history_title")}/i)
      click_on I18n.t("ds.dialog.close")
    end

    assert_no_selector "dialog[open]"
    assert_selector @row_link
  end

  # The payment drawer's "View bill" swaps back to the bill's drawer, in the
  # same frame, over the same list.
  test "the payment drawer's View bill opens the bill's drawer" do
    # Rows carry their verb only from @lg, and at 1400 both sidebars leave the
    # list just short of it.
    page.current_window.resize_to(1440, 1400)
    visit bills_url

    find(@find_payment_link).click
    within("dialog[open]") { click_on I18n.t("recurring_occurrences.show.view_bill") }
    within("dialog[open]") do
      assert_selector "h2", text: "City Water"
      assert_selector "turbo-frame[src*='display=history']"
    end
    assert_selector @row_link

    # Drawer swaps are frame visits, so Back leaves the overview. Turbo's
    # cached copy of it must not bring the closed drawer back open.
    page.send_keys(:escape)
    assert_no_selector "dialog[open]"
    click_on I18n.t("layouts.application.nav.transactions"), match: :first
    assert_current_path transactions_path
    page.go_back
    assert_selector @row_link
    assert_no_selector "dialog[open]"
  end

  # Find payment swaps the bill's drawer for the payment drawer inside the same
  # frame, and the link that did it goes with the first one.
  test "closing the payment drawer opened from a bill's drawer returns focus to the row" do
    visit bills_url

    find(@row_link).click
    within("dialog[open]") { click_on I18n.t("bills.find_payment") }
    # Waits on a control only the payment drawer has: its "remaining" line
    # reads exactly like the bill drawer's.
    within("dialog[open]") { assert_link I18n.t("recurring_occurrences.show.mark_paid") }

    page.send_keys(:escape)
    assert_no_selector "dialog[open]"
    assert_selector @row_link, focused: true
  end

  # History is the bill drawer's only list of earlier cycles, and the payment
  # drawer it opens is the only place a payment can be unlinked. The rows were
  # static text, so a past payment had no route from here at all.
  test "a settled cycle in history opens its payment drawer and can be unlinked" do
    settled = settle_past_cycle(months_ago: 2, amount: "80")

    visit bills_url
    find(@row_link).click

    within("dialog[open]") do
      scroll_to find("turbo-frame[src*='display=history']")
      assert_text(/#{I18n.t("bills.detail.history_title")}/i)
      # The disclosure's title is uppercased by CSS, and Capybara reads the
      # rendered text, so match the word rather than its stored casing.
      find("summary", text: /#{Regexp.escape(I18n.t("bills.detail.history"))}/i).click
      # The summary's own Manage payments action points at this same cycle, so
      # name the history row by its date instead of the href alone.
      find("a[href='#{recurring_occurrence_path(settled)}']",
           text: I18n.l(settled.due_on, format: :long)).click

      assert_link I18n.t("recurring_occurrences.show.unlink")
      click_on I18n.t("recurring_occurrences.show.unlink")
    end

    assert_text I18n.t("recurring_allocations.destroy.success")
    assert_empty settled.reload.allocations.confirmed
    assert settled.scheduled?, "unlinking the last payment reopens the cycle"
  end

  # The drawer's verb is chosen for the cycle it is showing. With the next
  # cycle open that is Find payment, so the bill's settled payments used to be
  # unreachable from the drawer even though the Manage payments surface is one
  # level below it.
  test "an open cycle's drawer offers Manage payments for the last settled cycle" do
    settled = settle_past_cycle(months_ago: 2, amount: "80")

    visit bills_url(view: "all")
    find("a[data-turbo-frame='drawer'][href='#{bill_path(@bill)}']", match: :first).click

    within("dialog[open]") do
      assert_link I18n.t("bills.manage_payments"), href: recurring_occurrence_path(settled)
      click_on I18n.t("bills.manage_payments")
      assert_link I18n.t("recurring_occurrences.show.unlink")
    end
  end

  private
    def settle_past_cycle(months_ago:, amount:)
      due = months_ago.months.ago.to_date
      occurrence = @bill.recurring_occurrences.create!(
        family: @user.family, original_due_on: due, due_on: due, currency: "USD"
      )
      RecurringTransaction::Allocator.new(occurrence).allocate!(amount: amount)
      assert occurrence.reload.paid?, "#{amount} must settle the cycle for this test"
      occurrence
    end
end