- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
218 lines
7.6 KiB
TypeScript
218 lines
7.6 KiB
TypeScript
import React from 'react';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { render, screen, fireEvent, act } from '@testing-library/react';
|
|
import PodcastPlayerModal from '../PodcastPlayerModal';
|
|
|
|
// Mock motion/react
|
|
vi.mock('motion/react', () => ({
|
|
AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
|
motion: {
|
|
div: ({ children, onClick, className, ...props }: any) => (
|
|
<div onClick={onClick} className={className} {...props}>
|
|
{children}
|
|
</div>
|
|
),
|
|
},
|
|
}));
|
|
|
|
// Mock SafeImage
|
|
vi.mock('../SafeImage', () => ({
|
|
default: ({ src, alt, className }: { src: string; alt: string; className?: string }) => (
|
|
<img src={src} alt={alt} className={className} data-testid="podcast-cover" />
|
|
),
|
|
}));
|
|
|
|
describe('PodcastPlayerModal Component', () => {
|
|
const mockPodcast = {
|
|
audioUrl: 'https://canina.ir/audio/canhydrox-review.mp3',
|
|
title: 'بررسی بالینی قرص کانهیدروکس',
|
|
description: 'توضیحات تکمیلی پیرامون جذب کلسیم و مفاصل در توله سگها.',
|
|
cover: 'https://canina.ir/images/canhydrox-cover.jpg',
|
|
productName: 'کانهیدروکس گگ',
|
|
};
|
|
|
|
const mockOnClose = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
window.HTMLMediaElement.prototype.play = vi.fn().mockImplementation(() => Promise.resolve());
|
|
window.HTMLMediaElement.prototype.pause = vi.fn();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('does not render anything when isOpen is false', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={false} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
|
|
it('does not render anything when podcast is null', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={null} />
|
|
);
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
|
|
it('renders podcast metadata, cover art, and triggers onClose when close button clicked', () => {
|
|
render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
expect(screen.getByText('بررسی بالینی قرص کانهیدروکس')).toBeInTheDocument();
|
|
expect(screen.getByText(/محصول: کانهیدروکس گگ/)).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText('توضیحات تکمیلی پیرامون جذب کلسیم و مفاصل در توله سگها.')
|
|
).toBeInTheDocument();
|
|
|
|
const cover = screen.getByTestId('podcast-cover');
|
|
expect(cover).toHaveAttribute('src', mockPodcast.cover);
|
|
|
|
const closeBtn = screen.getByTitle('بستن');
|
|
fireEvent.click(closeBtn);
|
|
expect(mockOnClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('toggles audio play and pause states when play/pause button is clicked', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const audioEl = container.querySelector('audio') as HTMLAudioElement;
|
|
expect(audioEl).toBeTruthy();
|
|
expect(audioEl.src).toBe(mockPodcast.audioUrl);
|
|
|
|
// In JSDOM, HTMLAudioElement.paused is true by default
|
|
// Clicking the play button when paused calls audio.play()
|
|
const toggleBtn = screen.getByTitle('توقف');
|
|
expect(toggleBtn).toBeInTheDocument();
|
|
|
|
// Set paused to false to simulate playing
|
|
Object.defineProperty(audioEl, 'paused', { value: false, writable: true });
|
|
fireEvent.click(toggleBtn);
|
|
expect(audioEl.pause).toHaveBeenCalled();
|
|
expect(screen.getByTitle('پخش')).toBeInTheDocument();
|
|
|
|
// Now set paused to true and click play
|
|
Object.defineProperty(audioEl, 'paused', { value: true, writable: true });
|
|
fireEvent.click(screen.getByTitle('پخش'));
|
|
expect(audioEl.play).toHaveBeenCalled();
|
|
});
|
|
|
|
it('skips forward and backward 15 seconds', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const audioEl = container.querySelector('audio') as HTMLAudioElement;
|
|
Object.defineProperty(audioEl, 'duration', { value: 300, configurable: true });
|
|
audioEl.currentTime = 50;
|
|
|
|
const skipForwardBtn = screen.getByTitle('۱۵ ثانیه جلو');
|
|
const skipBackBtn = screen.getByTitle('۱۵ ثانیه عقب');
|
|
|
|
fireEvent.click(skipForwardBtn);
|
|
expect(audioEl.currentTime).toBe(65);
|
|
|
|
fireEvent.click(skipBackBtn);
|
|
expect(audioEl.currentTime).toBe(50);
|
|
});
|
|
|
|
it('changes playback rate via speed dropdown menu', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const audioEl = container.querySelector('audio') as HTMLAudioElement;
|
|
const speedBtn = screen.getByTitle('سرعت پخش');
|
|
expect(speedBtn).toHaveTextContent('1x');
|
|
|
|
fireEvent.click(speedBtn);
|
|
|
|
const option15 = screen.getByRole('button', { name: '1.5x' });
|
|
fireEvent.click(option15);
|
|
|
|
expect(audioEl.playbackRate).toBe(1.5);
|
|
expect(speedBtn).toHaveTextContent('1.5x');
|
|
});
|
|
|
|
it('handles volume slider adjustments and mute toggle', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const audioEl = container.querySelector('audio') as HTMLAudioElement;
|
|
const volumeInput = container.querySelector('input[type="range"]') as HTMLInputElement;
|
|
|
|
// Change volume to 0.4
|
|
fireEvent.change(volumeInput, { target: { value: '0.4' } });
|
|
expect(audioEl.volume).toBe(0.4);
|
|
|
|
// Click mute button
|
|
const muteBtn = screen.getByTitle('بیصدا');
|
|
fireEvent.click(muteBtn);
|
|
expect(audioEl.muted).toBe(true);
|
|
|
|
// Unmute
|
|
const unmuteBtn = screen.getByTitle('صدا وصل');
|
|
fireEvent.click(unmuteBtn);
|
|
expect(audioEl.muted).toBe(false);
|
|
});
|
|
|
|
it('shares or copies podcast URL to clipboard when share button is clicked', async () => {
|
|
const writeTextMock = vi.fn().mockResolvedValue(undefined);
|
|
Object.assign(navigator, {
|
|
clipboard: {
|
|
writeText: writeTextMock,
|
|
},
|
|
});
|
|
|
|
render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const shareBtn = screen.getByTitle('اشتراکگذاری');
|
|
await act(async () => {
|
|
fireEvent.click(shareBtn);
|
|
});
|
|
|
|
expect(writeTextMock).toHaveBeenCalled();
|
|
});
|
|
|
|
it('triggers file download when download button is clicked', () => {
|
|
render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const appendChildSpy = vi.spyOn(document.body, 'appendChild');
|
|
const removeChildSpy = vi.spyOn(document.body, 'removeChild');
|
|
|
|
const downloadBtn = screen.getByTitle('دانلود فایل صوتی');
|
|
fireEvent.click(downloadBtn);
|
|
|
|
expect(appendChildSpy).toHaveBeenCalled();
|
|
expect(removeChildSpy).toHaveBeenCalled();
|
|
});
|
|
|
|
it('handles audio metadata loading and time updates correctly', () => {
|
|
const { container } = render(
|
|
<PodcastPlayerModal isOpen={true} onClose={mockOnClose} podcast={mockPodcast} />
|
|
);
|
|
|
|
const audioEl = container.querySelector('audio') as HTMLAudioElement;
|
|
Object.defineProperty(audioEl, 'duration', { value: 125, configurable: true });
|
|
Object.defineProperty(audioEl, 'currentTime', { value: 45, configurable: true });
|
|
|
|
act(() => {
|
|
fireEvent.loadedMetadata(audioEl);
|
|
fireEvent.timeUpdate(audioEl);
|
|
});
|
|
|
|
expect(screen.getByText('00:45')).toBeInTheDocument();
|
|
expect(screen.getByText('02:05')).toBeInTheDocument();
|
|
});
|
|
});
|