Files
shumai/packages/webui/components/viewers/video/video-viewer.test.tsx
T
Yiling-J 87e88396d1 feat(webui): add space key quick preview for files and folders (#402)
* feat(webui): add space key quick preview for files and folders

Allow users in file list views to press the Space key to quick preview the most recently selected item.

- Add FilePreviewDialog supporting both folder previews (icon, name, count) and real file previews (reusing FileViewer with viewer area and control bar).
- Constrain dialog to max 1920x1080 or 70vw/70vh.
- Support dismissing via close button, clicking outside, or pressing Space again (intercepted in capture phase).
- Enable across project file lists, share links, and public share views.

* feat(webui): auto play video in quick preview dialog

- Pass autoPlay prop from FilePreviewDialog through FileViewer to VideoViewer
- Configure videojs with autoplay: 'any' and handle policy rejection with muted autoplay fallback
- Add test coverage for autoPlay prop forwarding in FilePreviewDialog

* fix(webui): resolve video quick preview initialization and autoplay pause bugs

* fix(webui): support folder symlinks and password-protected shares in quick preview
2026-09-13 18:51:06 +08:00

186 lines
4.7 KiB
TypeScript

// @vitest-environment happy-dom
import { cleanup, render } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import VideoViewer from './video-viewer'
import type { AssetInfo } from '@shumai/dtos'
const videojsMock = vi.fn((...args: unknown[]) => {
void args
return {
on: vi.fn(),
volume: vi.fn(),
duration: vi.fn(() => 10),
currentTime: vi.fn(() => 0),
bufferedEnd: vi.fn(() => 0),
playbackRate: vi.fn(() => 1),
ready: vi.fn(),
dispose: vi.fn(),
isDisposed: vi.fn(() => false),
}
})
vi.mock('video.js', () => ({
default: (...args: unknown[]) => videojsMock(...args),
}))
vi.mock('@/ui/components/drawing-canvas', () => ({
default: () => <div data-testid="drawing-canvas" />,
}))
vi.mock('@/ui/paraglide/messages.js', () => ({
m: new Proxy({}, { get: () => () => '' }),
}))
describe('VideoViewer', () => {
beforeEach(() => {
videojsMock.mockClear()
})
afterEach(() => {
cleanup()
})
it('reloads video player with new video source when file prop changes without unmounting', () => {
const videoA: AssetInfo = {
id: 'video-a',
name: 'video-a.mp4',
proxyType: 'video',
media: {
metadata: {
originalWidth: 1280,
originalHeight: 720,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
videoTranscodes: [
{
resolution: '720p',
url: 'https://cdn.example.com/videoA-720p.mp4',
width: 1280,
height: 720,
},
],
},
} as unknown as AssetInfo
const videoB: AssetInfo = {
id: 'video-b',
name: 'video-b.mp4',
proxyType: 'video',
media: {
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 25,
frameRate: 30,
totalFrames: 750,
},
videoTranscodes: [
{
resolution: '1080p',
url: 'https://cdn.example.com/videoB-1080p.mp4',
width: 1920,
height: 1080,
},
],
},
} as unknown as AssetInfo
const { rerender } = render(<VideoViewer file={videoA} />)
// Initial render should initialize videojs with video A URL
expect(videojsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
sources: [
{
src: 'https://cdn.example.com/videoA-720p.mp4',
type: 'video/mp4',
},
],
}),
)
// Rerender with video B (simulating selecting another video from the carousel)
rerender(<VideoViewer file={videoB} />)
// VideoJS should be re-initialized with video B's URL
expect(videojsMock).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({
sources: [
{
src: 'https://cdn.example.com/videoB-1080p.mp4',
type: 'video/mp4',
},
],
}),
)
})
it('initializes VideoJS when transcode URL becomes available after initially empty with same ID', () => {
const initialVideo: AssetInfo = {
id: 'video-pending',
name: 'pending.mp4',
proxyType: 'video',
media: {
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
videoTranscodes: [
{
resolution: '1080p',
// URL not yet presigned (e.g. from file list payload)
url: undefined as unknown as string,
width: 1920,
height: 1080,
},
],
},
} as unknown as AssetInfo
const detailedVideo: AssetInfo = {
...initialVideo,
media: {
...initialVideo.media,
videoTranscodes: [
{
resolution: '1080p',
url: 'https://cdn.example.com/signed-pending-1080p.mp4',
width: 1920,
height: 1080,
},
],
},
} as unknown as AssetInfo
const { rerender } = render(<VideoViewer file={initialVideo} autoPlay={true} />)
// Initially targetSrc is empty, videojs should not be initialized
expect(videojsMock).not.toHaveBeenCalled()
// Rerender with detailedVideo (same id, but signed URL now available)
rerender(<VideoViewer file={detailedVideo} autoPlay={true} />)
// VideoJS should now be initialized with the new URL
expect(videojsMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
autoplay: false,
sources: [
{
src: 'https://cdn.example.com/signed-pending-1080p.mp4',
type: 'video/mp4',
},
],
}),
)
})
})