Files
canina/frontend/application/components/__tests__/VideosPage.test.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

30 lines
4.0 KiB
TypeScript

import React from 'react';
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react';
const h = vi.hoisted(() => ({ push: vi.fn(), back: vi.fn(), replace: vi.fn() }));
vi.mock('next/navigation', () => ({ useRouter: () => h, useSearchParams: () => new URLSearchParams(window.location.search) }));
vi.mock('motion/react', async () => {
const R = await import('react'); const cache: Record<string, React.ComponentType<any>> = {};
return { AnimatePresence: ({children}: any) => children, motion: new Proxy({}, { get: (_, tag: string) => cache[tag] ||= R.forwardRef(({children, ...props}: any, ref) => {
const clean = Object.fromEntries(Object.entries(props).filter(([k]) => !['initial','animate','exit','transition','whileHover','whileTap','whileInView','viewport','layout','layoutId','drag','dragConstraints','dragElastic','onDragEnd'].includes(k)));
return R.createElement(tag, {...clean, ref}, children);
}) }) };
});
vi.mock('next/image', () => ({ default: ({unoptimized, priority, fill, ...props}: any) => <img {...props} /> }));
vi.mock('sonner', () => ({ toast: {success: vi.fn(), error: vi.fn(), info: vi.fn()} }));
import { toast } from 'sonner';
afterEach(cleanup);
import VideosPage from '../VideosPage';
import { videoService } from '../../lib/services/videoService';
vi.mock('../../lib/services/videoService',()=>({videoService:{getVideos:vi.fn(),incrementViews:vi.fn()}}));
const videos=[{id:'1',title:'Joint care',doctor:'Dr Alpha',description:'Mobility',thumbnail:'/a.png',videoUrl:'/a.mp4'},{id:'2',title:'Skin care',doctor:'Dr Beta',description:'Coat',thumbnail:'/b.png',videoUrl:'/b.mp4'}];
beforeEach(()=>{vi.clearAllMocks();vi.mocked(videoService.getVideos).mockResolvedValue(videos as any);});
describe('VideosPage',()=>{
it('shows six skeletons until loading completes',async()=>{let resolve!:(v:any)=>void;vi.mocked(videoService.getVideos).mockReturnValueOnce(new Promise(r=>{resolve=r;}));const {container}=render(<VideosPage/>);expect(container.querySelectorAll('.animate-pulse')).toHaveLength(6);resolve(videos);await screen.findByText('Joint care');expect(container.querySelectorAll('.animate-pulse.space-y-4')).toHaveLength(0);expect(videoService.getVideos).toHaveBeenCalledWith({limit:100});});
it.each(['JOINT','alpha','mobility'])('filters field %s',async term=>{render(<VideosPage/>);await screen.findByText('Joint care');fireEvent.change(screen.getByPlaceholderText('جستجو در ویدئوهای آموزشی...'),{target:{value:term}});expect(screen.getByText('Joint care')).toBeInTheDocument();expect(screen.queryByText('Skin care')).not.toBeInTheDocument();});
it('opens selected video and records its view',async()=>{render(<VideosPage/>);fireEvent.click(await screen.findByText('Joint care'));expect(videoService.incrementViews).toHaveBeenCalledExactlyOnceWith('1');expect(document.querySelector('video')).toHaveAttribute('src','/a.mp4');});
it.each([[],null])('handles empty or malformed response %s',async result=>{vi.mocked(videoService.getVideos).mockResolvedValueOnce(result as any);render(<VideosPage/>);expect(await screen.findByText('ویدئویی یافت نشد')).toBeInTheDocument();});
it('handles request rejection',async()=>{const err=vi.spyOn(console,'error').mockImplementation(()=>{});vi.mocked(videoService.getVideos).mockRejectedValueOnce(new Error('offline'));render(<VideosPage/>);await screen.findByText('ویدئویی یافت نشد');expect(err).toHaveBeenCalled();err.mockRestore();});
it('shares current URL through clipboard',async()=>{const writeText=vi.fn().mockResolvedValue(undefined);Object.defineProperty(navigator,'clipboard',{configurable:true,value:{writeText}});render(<VideosPage/>);await screen.findByText('Joint care');fireEvent.click(screen.getByRole('button',{name:'اشتراک‌گذاری صفحه'}));expect(writeText).toHaveBeenCalledWith(window.location.href);expect(toast.success).toHaveBeenCalledWith('لینک آکادمی ویدئویی کپی شد.');});
});