diff --git a/packages/utils/__tests__/use-event-listener.test.tsx b/packages/utils/__tests__/use-event-listener.test.tsx new file mode 100644 index 000000000..c81fcf398 --- /dev/null +++ b/packages/utils/__tests__/use-event-listener.test.tsx @@ -0,0 +1,33 @@ +import * as React from "react"; +import { render, fireEvent, cleanup } from "@reach-internal/test/utils"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { useEventListener } from "@reach/utils"; + +afterEach(cleanup); + +describe("useEventListener", () => { + const Test = ({ onBodyClick }: { onBodyClick: () => void }) => { + useEventListener("click", onBodyClick, document.body); + return null; + }; + + it("should call event listener when it's need", () => { + const handleBodyClick = vi.fn(); + render(); + fireEvent.click(document.body); + expect(handleBodyClick).toHaveBeenCalledTimes(1); + fireEvent.click(document.body); + expect(handleBodyClick).toHaveBeenCalledTimes(2); + }); + + it("should can change event listener from args", () => { + const handleBodyClick1 = vi.fn(); + const handleBodyClick2 = vi.fn(); + const { rerender } = render(); + fireEvent.click(document.body); + rerender(); + fireEvent.click(document.body); + expect(handleBodyClick1).toHaveBeenCalledOnce(); + expect(handleBodyClick2).toHaveBeenCalledOnce(); + }); +});