From e62e04f247bb119855c7ef46f6e6f31a49efbf39 Mon Sep 17 00:00:00 2001 From: Sergey Kozlov Date: Thu, 25 Aug 2022 17:10:29 +0300 Subject: [PATCH] add tests for useEventListener --- .../__tests__/use-event-listener.test.tsx | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 packages/utils/__tests__/use-event-listener.test.tsx 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(); + }); +});