import React from 'react'; import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { render, screen, fireEvent, act } from '@testing-library/react'; import VideoModalPlayer from '../VideoModalPlayer'; // Mock getMediaUrl helper vi.mock('../../lib/media', () => ({ getMediaUrl: (url: string) => url, })); describe('VideoModalPlayer Component', () => { const mockVideo = { id: 'vid-123', title: 'آموزش تقویت مفاصل سگ‌های ژرمن', doctor: 'دکتر علیرضا محمدی', videoUrl: 'https://canina.ir/videos/german-joints.mp4', thumbnail: 'https://canina.ir/images/thumbnail.jpg', description: 'در این ویدیو به بررسی تخصصی ترکیبات گلوکوزامین و کندرویتین پرداخته می‌شود.', }; const mockOnClose = vi.fn(); beforeEach(() => { vi.clearAllMocks(); localStorage.clear(); // Mock HTMLMediaElement methods in JSDOM window.HTMLMediaElement.prototype.play = vi.fn().mockImplementation(() => Promise.resolve()); window.HTMLMediaElement.prototype.pause = vi.fn(); window.HTMLMediaElement.prototype.load = vi.fn(); }); afterEach(() => { vi.restoreAllMocks(); }); it('does not render anything when isOpen is false', () => { const { container } = render( ); expect(container.firstChild).toBeNull(); }); it('does not render anything when video is null', () => { const { container } = render( ); expect(container.firstChild).toBeNull(); }); it('renders video details, doctor, description, and controls when isOpen is true', () => { render( ); expect(screen.getByText('آموزش تقویت مفاصل سگ‌های ژرمن')).toBeInTheDocument(); expect(screen.getByText('دکتر علیرضا محمدی')).toBeInTheDocument(); expect( screen.getByText('در این ویدیو به بررسی تخصصی ترکیبات گلوکوزامین و کندرویتین پرداخته می‌شود.') ).toBeInTheDocument(); expect(screen.getByTitle('توقف')).toBeInTheDocument(); // Initially playing expect(screen.getByTitle('بستن')).toBeInTheDocument(); }); it('calls onClose when close button is clicked', () => { render( ); const closeButton = screen.getByTitle('بستن'); fireEvent.click(closeButton); expect(mockOnClose).toHaveBeenCalledTimes(1); }); it('toggles play/pause state when play button is clicked', () => { const { container } = render( ); const videoEl = container.querySelector('video') as HTMLVideoElement; expect(videoEl).toBeInTheDocument(); // In jsdom paused defaults to true unless defined. // If paused is true, togglePlay calls videoRef.current.play() const playSpy = vi.spyOn(videoEl, 'play'); const pauseSpy = vi.spyOn(videoEl, 'pause'); // Currently videoEl.paused is true const toggleButton = screen.getByTitle('توقف'); fireEvent.click(toggleButton); expect(playSpy).toHaveBeenCalled(); // Simulate video playing Object.defineProperty(videoEl, 'paused', { value: false, configurable: true }); fireEvent.click(toggleButton); expect(pauseSpy).toHaveBeenCalled(); }); it('allows jumping forward and backward in time', () => { const { container } = render( ); const videoEl = container.querySelector('video') as HTMLVideoElement; Object.defineProperty(videoEl, 'duration', { value: 120, configurable: true }); videoEl.currentTime = 30; const skipForward = screen.getByTitle('۱۰ ثانیه جلو'); const skipBackward = screen.getByTitle('۱۰ ثانیه عقب'); fireEvent.click(skipForward); expect(videoEl.currentTime).toBe(40); fireEvent.click(skipBackward); expect(videoEl.currentTime).toBe(30); }); it('allows toggling mute and volume change', () => { const { container } = render( ); const videoEl = container.querySelector('video') as HTMLVideoElement; const muteBtn = screen.getByTitle('بی‌صدا'); fireEvent.click(muteBtn); expect(videoEl.muted).toBe(true); expect(screen.getByTitle('صدا وصل')).toBeInTheDocument(); fireEvent.click(screen.getByTitle('صدا وصل')); expect(videoEl.muted).toBe(false); expect(screen.getByTitle('بی‌صدا')).toBeInTheDocument(); }); it('opens playback rate menu and updates rate', () => { const { container } = render( ); const videoEl = container.querySelector('video') as HTMLVideoElement; const rateBtn = screen.getByTitle('سرعت پخش'); expect(rateBtn).toHaveTextContent('1x'); fireEvent.click(rateBtn); const speed15 = screen.getByRole('button', { name: '1.5x' }); fireEvent.click(speed15); expect(rateBtn).toHaveTextContent('1.5x'); expect(videoEl.playbackRate).toBe(1.5); }); it('renders iframe embed when videoUrl contains iframe tag', () => { const iframeVideo = { ...mockVideo, videoUrl: '', }; render( ); const closeBtn = screen.getByTitle('بستن'); expect(closeBtn).toBeInTheDocument(); fireEvent.click(closeBtn); expect(mockOnClose).toHaveBeenCalledTimes(1); expect(document.querySelector('iframe')).toBeInTheDocument(); }); it('shares or copies link when share button is clicked', async () => { const writeTextMock = vi.fn().mockResolvedValue(undefined); Object.assign(navigator, { clipboard: { writeText: writeTextMock, }, }); render( ); const shareBtn = screen.getByTitle('اشتراک‌گذاری'); await act(async () => { fireEvent.click(shareBtn); }); expect(writeTextMock).toHaveBeenCalled(); }); });