Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/app/hooks/__tests__/useVideoPlayer.test.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLVideoElement>;

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);
});
});
17 changes: 15 additions & 2 deletions src/app/hooks/useVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ interface VideoError {
retryCount: number;
}

export const useVideoPlayer = (videoRef: RefObject<HTMLVideoElement>) => {
interface UseVideoPlayerOptions {
maxRetries?: number;
}

/**
* Hook to manage video playback.
* @param videoRef Ref to HTMLVideoElement
* @param options Optional configuration
*/
export const useVideoPlayer = (
videoRef: RefObject<HTMLVideoElement>,
options: UseVideoPlayerOptions = {}
) => {
const { maxRetries = 3 } = options;
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
Expand All @@ -19,7 +32,7 @@ export const useVideoPlayer = (videoRef: RefObject<HTMLVideoElement>) => {
const [error, setError] = useState<VideoError | null>(null);
const [isMuted, setIsMuted] = useState(false);
const [retryCount, setRetryCount] = useState(0);
const maxRetries = 3;


const play = useCallback(() => {
if (videoRef.current) {
Expand Down
Loading