-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |