From cf09a478f6e11fbcdc7e331f1797bbfd8ec77422 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 3 Jun 2024 18:32:03 +0300 Subject: [PATCH] [test]: usePageLeave --- src/hooks/usePageLeave/usePageLeave.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hooks/usePageLeave/usePageLeave.test.ts b/src/hooks/usePageLeave/usePageLeave.test.ts index 11f0038..63222c5 100644 --- a/src/hooks/usePageLeave/usePageLeave.test.ts +++ b/src/hooks/usePageLeave/usePageLeave.test.ts @@ -1,4 +1,4 @@ -import { act, renderHook } from '@testing-library/react'; +import { act, fireEvent, renderHook } from '@testing-library/react'; import { usePageLeave } from './usePageLeave'; @@ -6,7 +6,7 @@ it('Should callback after the mouse has moved off the page', () => { const callback = vi.fn(); renderHook(() => usePageLeave(callback)); - act(() => document.dispatchEvent(new Event('mouseleave'))); + act(() => fireEvent.mouseLeave(document)); expect(callback).toBeCalledTimes(1); }); @@ -15,7 +15,7 @@ it("Shouldn't callback after the mouse has moved to the page", () => { const callback = vi.fn(); renderHook(() => usePageLeave(callback)); - act(() => document.dispatchEvent(new Event('mouseenter'))); + act(() => fireEvent.mouseEnter(document)); expect(callback).not.toBeCalled(); }); @@ -24,12 +24,12 @@ it('Should be called more than once when the mouse goes off the page repeatedly' const callback = vi.fn(); renderHook(() => usePageLeave(callback)); - act(() => document.dispatchEvent(new Event('mouseleave'))); + act(() => fireEvent.mouseLeave(document)); expect(callback).toBeCalledTimes(1); - act(() => document.dispatchEvent(new Event('mouseenter'))); - act(() => document.dispatchEvent(new Event('mouseleave'))); + act(() => fireEvent.mouseEnter(document)); + act(() => fireEvent.mouseLeave(document)); expect(callback).toBeCalledTimes(2); });