import {renderHook, act} from '@testing-library/react';
import {useScrollPosition} from '../useScrollPosition';

describe('useScrollPosition', () => {
  beforeEach(() => {
    Object.defineProperty(window, 'scrollY', {value: 0, writable: true});
  });

  it('should return false when scroll is below threshold', () => {
    const {result} = renderHook(() => useScrollPosition(50));
    expect(result.current).toBe(false);
  });

  it('should return true when scroll exceeds threshold', () => {
    const {result} = renderHook(() => useScrollPosition(50));

    act(() => {
      Object.defineProperty(window, 'scrollY', {value: 100, writable: true});
      window.dispatchEvent(new Event('scroll'));
    });

    expect(result.current).toBe(true);
  });

  it('should return false when scroll goes back below threshold', () => {
    Object.defineProperty(window, 'scrollY', {value: 100, writable: true});
    const {result} = renderHook(() => useScrollPosition(50));

    act(() => {
      window.dispatchEvent(new Event('scroll'));
    });
    expect(result.current).toBe(true);

    act(() => {
      Object.defineProperty(window, 'scrollY', {value: 10, writable: true});
      window.dispatchEvent(new Event('scroll'));
    });
    expect(result.current).toBe(false);
  });

  it('should use default threshold of 50', () => {
    const {result} = renderHook(() => useScrollPosition());

    act(() => {
      Object.defineProperty(window, 'scrollY', {value: 51, writable: true});
      window.dispatchEvent(new Event('scroll'));
    });

    expect(result.current).toBe(true);
  });
});
