Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
2.8 KB
Raw Blame
require "application_system_test_case"

class AccountsSyncUiTest < ApplicationSystemTestCase
  setup do
    @user = users(:family_admin)
    # These tests control sync state explicitly; login must not start a new
    # sync when fixtures were loaded before midnight.
    @user.family.update!(auto_sync_on_login: false)
    Sync.incomplete.for_family(@user.family).destroy_all
    # Exercise the previous-day fixture boundary without waiting for midnight.
    @user.family.syncs.update_all(created_at: 1.day.ago)
    sign_in @user
    # Guard against background syncs enqueued during the login flow even when auto_sync_on_login is false
    Sync.incomplete.for_family(@user.family).destroy_all
  end

  teardown do
    Sync.incomplete.destroy_all
  end

  test "idle state shows a plain refresh trigger with no cancel control" do
    visit accounts_path

    within "#accounts-sync-controls" do
      assert_selector "button:not([disabled])[aria-label='#{I18n.t("accounts.index.sync")}']"
      assert_no_text I18n.t("accounts.index.cancel_sync")
    end
  end

  test "an in-progress family sync shows a spinner and a matching Cancel sync button" do
    Sync.create!(syncable: @user.family, status: :syncing)

    visit accounts_path

    within "#accounts-sync-controls" do
      # `icon(..., as_button: true, class: "animate-spin")` renders a
      # DS::Button whose icon classes are fully internal to the component —
      # the caller's `class:` lands on the button element itself, not the
      # inner svg. Same visual result for a centered square icon button.
      assert_selector "button.animate-spin[disabled]"
      assert_selector "button", text: I18n.t("accounts.index.cancel_sync")
    end
  end

  test "cancelling a sync shows a flash notice, and the tray centers on the content pane" do
    Sync.create!(syncable: @user.family, status: :syncing)

    visit accounts_path
    within "#accounts-sync-controls" do
      click_on I18n.t("accounts.index.cancel_sync")
    end

    assert_text I18n.t("syncs.cancel.cancelled")

    # The tray is a fixed overlay, positioned by notification_tray_controller.js
    # to center on <main> (the actual content pane next to the sidebar) rather
    # than the full viewport — assert the two centers actually match, not just
    # that the tray exists somewhere on screen.
    tray_rect = page.evaluate_script("document.querySelector('[data-notification-tray-target=\"tray\"]').getBoundingClientRect()")
    main_rect = page.evaluate_script("document.querySelector('#main').getBoundingClientRect()")

    tray_center = tray_rect["left"] + tray_rect["width"] / 2.0
    main_center = main_rect["left"] + main_rect["width"] / 2.0

    assert_in_delta main_center, tray_center, 1.0,
      "expected the tray (center: #{tray_center}) to align with the content pane (center: #{main_center}), not drift to viewport-center"
  end
end