aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-scroll-position/use-scroll-position.test.ts
blob: 49370aecf3fc74db9128e22f6271bf39cbe24b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
  });
});