From 53980ddb4ec597275847f5b28e292559575adfa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kry=C5=A1p=C3=ADn?= Date: Thu, 7 Dec 2023 16:11:32 +0100 Subject: [PATCH] Fix(web-react): Mock window.scrollTo in test of useScrollControl hook --- .../src/hooks/__tests__/useScrollControl.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/web-react/src/hooks/__tests__/useScrollControl.test.ts b/packages/web-react/src/hooks/__tests__/useScrollControl.test.ts index ed3fe7bb64..eb7a83d4fd 100644 --- a/packages/web-react/src/hooks/__tests__/useScrollControl.test.ts +++ b/packages/web-react/src/hooks/__tests__/useScrollControl.test.ts @@ -3,12 +3,23 @@ import { MutableRefObject } from 'react'; import { useScrollControl } from '../useScrollControl'; describe('useScrollControl', () => { + let originalScrollTo: typeof window.scrollTo; + const createMockRef = (isOpen: boolean) => ({ current: { open: isOpen, }, }); + beforeEach(() => { + originalScrollTo = window.scrollTo; + window.scrollTo = jest.fn(); + }); + + afterEach(() => { + window.scrollTo = originalScrollTo; + }); + it('should disable scroll when isOpen is true', () => { const mockRef = createMockRef(true) as MutableRefObject; @@ -24,6 +35,8 @@ describe('useScrollControl', () => { it('should enable scroll when isOpen is false', () => { const mockRef = createMockRef(false) as MutableRefObject; + const scrollToSpy = jest.spyOn(window, 'scrollTo'); + act(() => { renderHook(() => useScrollControl(mockRef, true)); }); @@ -32,6 +45,7 @@ describe('useScrollControl', () => { renderHook(() => useScrollControl(mockRef, false)); }); + expect(scrollToSpy).toHaveBeenCalledWith(0, 0); expect(document.body.classList.contains('is-scrolling-disabled')).toBe(false); expect(document.body.style.top).toBe(''); });