From c826ad66df066b90b09009f2f4b83b56d018436e Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 15 Nov 2023 16:37:16 +0100 Subject: refactor(hooks): rewrite useScrollPosition hook * return the scroll position (both X and Y) * no longer accepts arguments * add tests --- .../use-scroll-position.test.ts | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/utils/hooks/use-scroll-position/use-scroll-position.test.ts (limited to 'src/utils/hooks/use-scroll-position/use-scroll-position.test.ts') diff --git a/src/utils/hooks/use-scroll-position/use-scroll-position.test.ts b/src/utils/hooks/use-scroll-position/use-scroll-position.test.ts new file mode 100644 index 0000000..49370ae --- /dev/null +++ b/src/utils/hooks/use-scroll-position/use-scroll-position.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from '@jest/globals'; +import { fireEvent, renderHook } from '@testing-library/react'; +import { type ScrollPosition, useScrollPosition } from './use-scroll-position'; + +describe('useScrollPosition', () => { + it('returns the scroll position based on window', () => { + const { result } = renderHook(() => useScrollPosition()); + + expect(result.current.x).toBe(0); + expect(result.current.y).toBe(0); + + const newPos: ScrollPosition = { + x: 50, + y: 100, + }; + + fireEvent.scroll(window, { target: { scrollX: newPos.x } }); + + expect(result.current.x).toBe(newPos.x); + + fireEvent.scroll(window, { target: { scrollY: newPos.y } }); + + expect(result.current.y).toBe(newPos.y); + }); +}); -- cgit v1.2.3