import React, { FC } from "react"; import { render } from "@testing-library/react"; import FocusContainer, { FocusContainerProps } from "../FocusContainer"; const requestAnimationFrame = jest.spyOn(window, "requestAnimationFrame"); type TestProps = FocusContainerProps & { visible: boolean }; const Test: FC = ({ visible, ...props }) => ( <> {visible && ( )} ); beforeEach(() => { requestAnimationFrame.mockClear(); requestAnimationFrame.mockImplementation((cb) => { cb(0); return 0; }); }); afterAll(() => { requestAnimationFrame.mockRestore(); }); describe("FocusContainer", () => { it("should handle the focus mounting flow correctly", () => { const getMainButton = () => document.getElementById("main-button"); const getButton1 = () => document.getElementById("button-1"); const { rerender } = render(); expect(document.activeElement).toBe(getMainButton()); rerender(); expect(document.activeElement).toBe(getButton1()); rerender(); expect(document.activeElement).toBe(getMainButton()); }); it("should not focus any elements while the disableFocusOnMount and disableFocusOnUnmount props are true", () => { const getMainButton = () => document.getElementById("main-button"); const { rerender } = render( ); expect(document.activeElement).toBe(getMainButton()); rerender(); expect(document.activeElement).toBe(getMainButton()); rerender(); expect(document.activeElement).toBe(getMainButton()); }); it("should render correctly (with snapshots)", () => { const { container, rerender } = render( Link ); expect(container).toMatchSnapshot(); rerender( ); expect(container).toMatchSnapshot(); }); });