Skip to content

Commit

Permalink
add tests for useEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
dartess committed Aug 25, 2022
1 parent d58d027 commit e62e04f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/utils/__tests__/use-event-listener.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<Test onBodyClick={handleBodyClick} />);
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(<Test onBodyClick={handleBodyClick1} />);
fireEvent.click(document.body);
rerender(<Test onBodyClick={handleBodyClick2} />);
fireEvent.click(document.body);
expect(handleBodyClick1).toHaveBeenCalledOnce();
expect(handleBodyClick2).toHaveBeenCalledOnce();
});
});

0 comments on commit e62e04f

Please sign in to comment.