From 42ac8e507b6ba79362f8de28af9a3599edd34f36 Mon Sep 17 00:00:00 2001 From: Opulence Chuks Date: Mon, 29 Jun 2026 07:37:50 +0100 Subject: [PATCH] feat: make maxRetries configurable via hook options and add tests --- .../hooks/__tests__/useVideoPlayer.test.tsx | 36 +++++++++++++++++++ src/app/hooks/useVideoPlayer.tsx | 17 +++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/app/hooks/__tests__/useVideoPlayer.test.tsx diff --git a/src/app/hooks/__tests__/useVideoPlayer.test.tsx b/src/app/hooks/__tests__/useVideoPlayer.test.tsx new file mode 100644 index 00000000..d4b3634f --- /dev/null +++ b/src/app/hooks/__tests__/useVideoPlayer.test.tsx @@ -0,0 +1,36 @@ +import { renderHook, act } from '@testing-library/react'; +import { useVideoPlayer } from '../useVideoPlayer'; +import { RefObject } from 'react'; +import { vi } from 'vitest'; + +describe('useVideoPlayer maxRetries option', () => { + // Mock a simple video element + const createVideoMock = () => { + const video = document.createElement('video') as HTMLVideoElement; + video.play = vi.fn().mockRejectedValue(new Error('play failed')); + video.src = 'test.mp4'; + return video; + }; + + const videoRef = { current: createVideoMock() } as RefObject; + + it('should respect maxRetries option', () => { + const { result } = renderHook(() => useVideoPlayer(videoRef, { maxRetries: 1 })); + + // Initial state + expect(result.current.maxRetries).toBe(1); + expect(result.current.retryCount).toBe(0); + + // First retry should work + act(() => { + result.current.retry(); + }); + expect(result.current.retryCount).toBe(1); + + // Second retry should not increase because maxRetries is 1 + act(() => { + result.current.retry(); + }); + expect(result.current.retryCount).toBe(1); + }); +}); diff --git a/src/app/hooks/useVideoPlayer.tsx b/src/app/hooks/useVideoPlayer.tsx index 8df17afe..4e8e47d9 100644 --- a/src/app/hooks/useVideoPlayer.tsx +++ b/src/app/hooks/useVideoPlayer.tsx @@ -8,7 +8,20 @@ interface VideoError { retryCount: number; } -export const useVideoPlayer = (videoRef: RefObject) => { +interface UseVideoPlayerOptions { + maxRetries?: number; +} + +/** + * Hook to manage video playback. + * @param videoRef Ref to HTMLVideoElement + * @param options Optional configuration + */ +export const useVideoPlayer = ( + videoRef: RefObject, + options: UseVideoPlayerOptions = {} +) => { + const { maxRetries = 3 } = options; const [isPlaying, setIsPlaying] = useState(false); const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0); @@ -19,7 +32,7 @@ export const useVideoPlayer = (videoRef: RefObject) => { const [error, setError] = useState(null); const [isMuted, setIsMuted] = useState(false); const [retryCount, setRetryCount] = useState(0); - const maxRetries = 3; + const play = useCallback(() => { if (videoRef.current) {