/**
 * Tests for useGameLoop hook
 */

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

describe('useGameLoop Hook', () => {
  let originalRaf: typeof window.requestAnimationFrame;
  let originalCaf: typeof window.cancelAnimationFrame;

  beforeEach(() => {
    originalRaf = window.requestAnimationFrame;
    originalCaf = window.cancelAnimationFrame;

    window.requestAnimationFrame = ((callback: FrameRequestCallback) => {
      callback(Date.now());
      return 1;
    }) as typeof window.requestAnimationFrame;

    window.cancelAnimationFrame = jest.fn() as typeof window.cancelAnimationFrame;
  });

  afterEach(() => {
    window.requestAnimationFrame = originalRaf;
    window.cancelAnimationFrame = originalCaf;
  });

  test('calls callback when running', () => {
    const callback = jest.fn();
    
    renderHook(() => useGameLoop(callback, true));
    
    expect(callback).toHaveBeenCalled();
  });

  test('does not call callback when not running', () => {
    const callback = jest.fn();
    
    renderHook(() => useGameLoop(callback, false));
    
    expect(callback).not.toHaveBeenCalled();
  });

  test('cleans up on unmount', () => {
    const callback = jest.fn();
    const cancelSpy = jest.spyOn(window, 'cancelAnimationFrame');
    
    const { unmount } = renderHook(() => useGameLoop(callback, true));
    
    act(() => {
      unmount();
    });
    
    expect(cancelSpy).toHaveBeenCalled();
    
    cancelSpy.mockRestore();
  });
});

