// @vitest-environment jsdom // // E28 review F7: while hidden the button only went transparent — a Tab still landed on the invisible // control. And a keyboard press scrolled to the top and hid it with the focus still on it. import { afterEach, describe, expect, it, vi } from "vitest"; import { act, cleanup, fireEvent, render, screen } from "@testing-library/react"; vi.mock("react-i18next", async (importOriginal) => ({ ...(await importOriginal()), useTranslation: () => ({ t: (k: string) => k }), })); import BackToTop from "./BackToTop"; afterEach(cleanup); function scrolled(main: HTMLElement, top: number) { main.scrollTop = top; act(() => void main.dispatchEvent(new Event("scroll"))); } describe("BackToTop", () => { it("is no Tab stop while hidden; a keyboard press leaves the focus on the page", () => { const main = document.createElement("main"); main.tabIndex = -1; main.scrollTo = vi.fn(); document.body.appendChild(main); render(); const btn = screen.getByText("common.backToTop").closest("button")!; expect(btn.tabIndex).toBe(-1); scrolled(main, 900); expect(btn.tabIndex).toBe(0); btn.focus(); fireEvent.click(btn, { detail: 0 }); expect(main.scrollTo).toHaveBeenCalled(); expect(document.activeElement).toBe(main); main.remove(); }); });