Skip to content

Commit

Permalink
Merge pull request #130 from str0yka/test-usePageLeave
Browse files Browse the repository at this point in the history
[test]: usePageLeave
  • Loading branch information
debabin authored Jun 3, 2024
2 parents dc0c1b5 + cf09a47 commit 8873e8a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/hooks/usePageLeave/usePageLeave.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { act, fireEvent, renderHook } from '@testing-library/react';

import { usePageLeave } from './usePageLeave';

it('Should callback after the mouse has moved off the page', () => {
const callback = vi.fn();
renderHook(() => usePageLeave(callback));

act(() => fireEvent.mouseLeave(document));

expect(callback).toBeCalledTimes(1);
});

it("Shouldn't callback after the mouse has moved to the page", () => {
const callback = vi.fn();
renderHook(() => usePageLeave(callback));

act(() => fireEvent.mouseEnter(document));

expect(callback).not.toBeCalled();
});

it('Should be called more than once when the mouse goes off the page repeatedly', () => {
const callback = vi.fn();
renderHook(() => usePageLeave(callback));

act(() => fireEvent.mouseLeave(document));

expect(callback).toBeCalledTimes(1);

act(() => fireEvent.mouseEnter(document));
act(() => fireEvent.mouseLeave(document));

expect(callback).toBeCalledTimes(2);
});

0 comments on commit 8873e8a

Please sign in to comment.