mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 02:46:52 +00:00
* feat(upload): simplify upload file list display and add live speed and ETA * fix(upload): retain authoritative server search results and handle local failed upload state gracefully
254 lines
7.0 KiB
TypeScript
254 lines
7.0 KiB
TypeScript
// @vitest-environment happy-dom
|
|
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
|
|
import React from 'react'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { FileListItem } from './file-list-item'
|
|
import type { AssetInfo } from '@shumai/dtos'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { useUploadStore } from '@/ui/stores/upload'
|
|
|
|
vi.mock('@dnd-kit/react', () => ({
|
|
useDraggable: () => ({
|
|
ref: vi.fn(),
|
|
isDragging: false,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/ui/api/client', () => ({
|
|
client: {
|
|
api: {
|
|
files: {
|
|
':fileId': {
|
|
$get: vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({}),
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}))
|
|
|
|
describe('FileListItem', () => {
|
|
let queryClient: QueryClient
|
|
|
|
beforeEach(() => {
|
|
queryClient = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
})
|
|
useUploadStore.setState({ fileProgress: {} })
|
|
vi.useFakeTimers()
|
|
})
|
|
|
|
afterEach(() => {
|
|
act(() => {
|
|
vi.runOnlyPendingTimers()
|
|
})
|
|
vi.useRealTimers()
|
|
cleanup()
|
|
})
|
|
|
|
const mockItem: AssetInfo = {
|
|
id: 'file-123',
|
|
name: 'very_long_file_name_for_testing_purposes.mov',
|
|
type: 'file',
|
|
sizeByte: 1024 * 1024 * 5,
|
|
createdAt: '2026-01-01T00:00:00Z',
|
|
updatedAt: '2026-01-01T00:00:00Z',
|
|
status: 'processed',
|
|
} as AssetInfo
|
|
|
|
const renderComponent = (props: Partial<React.ComponentProps<typeof FileListItem>> = {}) => {
|
|
return render(
|
|
<QueryClientProvider client={queryClient}>
|
|
<FileListItem
|
|
item={mockItem}
|
|
isSelected={false}
|
|
isChecked={false}
|
|
isEditing={false}
|
|
onSelect={vi.fn()}
|
|
onDoubleClick={vi.fn()}
|
|
onContextMenu={vi.fn()}
|
|
onDragStart={vi.fn()}
|
|
onDrop={vi.fn()}
|
|
onRename={vi.fn()}
|
|
onFinishEditing={vi.fn()}
|
|
onSaveField={vi.fn()}
|
|
fields={[]}
|
|
{...props}
|
|
/>
|
|
</QueryClientProvider>,
|
|
)
|
|
}
|
|
|
|
it('renders truncated text span when not editing', () => {
|
|
renderComponent()
|
|
|
|
expect(screen.queryByRole('textbox')).toBeNull()
|
|
const nameSpan = screen.getByText('very_long_file_name_for_testing_purposes.mov')
|
|
expect(nameSpan).toBeTruthy()
|
|
expect(nameSpan.tagName.toLowerCase()).toBe('span')
|
|
expect(nameSpan.className).toContain('truncate')
|
|
})
|
|
|
|
it('shows tooltip on hover when item name overflows', () => {
|
|
renderComponent()
|
|
|
|
const span = screen.getByText('very_long_file_name_for_testing_purposes.mov')
|
|
Object.defineProperty(span, 'scrollWidth', { value: 350, configurable: true })
|
|
Object.defineProperty(span, 'clientWidth', { value: 150, configurable: true })
|
|
|
|
fireEvent.pointerMove(span, { pointerType: 'mouse' })
|
|
|
|
act(() => {
|
|
vi.advanceTimersByTime(200)
|
|
})
|
|
|
|
const tooltip = screen.getByRole('tooltip')
|
|
expect(tooltip).toBeTruthy()
|
|
expect(tooltip.textContent).toContain('very_long_file_name_for_testing_purposes.mov')
|
|
})
|
|
|
|
it('switches to input in editing mode', () => {
|
|
renderComponent({ isEditing: true })
|
|
|
|
const input = screen.getByRole('textbox') as HTMLInputElement
|
|
expect(input).toBeTruthy()
|
|
expect(input.value).toBe('very_long_file_name_for_testing_purposes.mov')
|
|
})
|
|
|
|
it('calls onSelect when row is clicked', () => {
|
|
const onSelect = vi.fn()
|
|
renderComponent({ onSelect })
|
|
|
|
const span = screen.getByText('very_long_file_name_for_testing_purposes.mov')
|
|
fireEvent.click(span)
|
|
|
|
expect(onSelect).toHaveBeenCalled()
|
|
})
|
|
|
|
it('calls onDoubleClick when row is double clicked', () => {
|
|
const onDoubleClick = vi.fn()
|
|
renderComponent({ onDoubleClick })
|
|
|
|
const span = screen.getByText('very_long_file_name_for_testing_purposes.mov')
|
|
fireEvent.doubleClick(span)
|
|
|
|
expect(onDoubleClick).toHaveBeenCalled()
|
|
})
|
|
|
|
it('renders (Processing failed) label when status is failed', () => {
|
|
const failedItem: AssetInfo = {
|
|
...mockItem,
|
|
status: 'failed',
|
|
} as AssetInfo
|
|
|
|
renderComponent({ item: failedItem })
|
|
|
|
const failedLabel = screen.getByTestId('file-list-item-failed')
|
|
expect(failedLabel).toBeTruthy()
|
|
expect(failedLabel.textContent).toMatch(/Processing failed|处理失败/i)
|
|
})
|
|
|
|
it('does not trigger onSelect when clicking a custom field in list view', () => {
|
|
const onSelect = vi.fn()
|
|
const itemWithFields: AssetInfo = {
|
|
...mockItem,
|
|
fieldValues: [
|
|
{
|
|
fieldId: 'field_text',
|
|
value: 'List text value',
|
|
},
|
|
],
|
|
} as AssetInfo
|
|
|
|
const fields = [
|
|
{
|
|
id: 'field_text',
|
|
name: 'Custom Column',
|
|
scope: 'project',
|
|
readOnly: false,
|
|
visible: true,
|
|
description: '',
|
|
config: {
|
|
type: 'text' as const,
|
|
name: 'Custom Column',
|
|
},
|
|
},
|
|
]
|
|
|
|
renderComponent({ item: itemWithFields, fields, onSelect })
|
|
|
|
const textField = screen.getByText('List text value')
|
|
fireEvent.click(textField)
|
|
|
|
expect(screen.getByDisplayValue('List text value')).toBeTruthy()
|
|
expect(onSelect).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('renders dynamic ETA and speed for local uploader', () => {
|
|
useUploadStore.setState({
|
|
fileProgress: {
|
|
'file-123': {
|
|
fileId: 'file-123',
|
|
name: 'very_long_file_name_for_testing_purposes.mov',
|
|
loaded: 500,
|
|
total: 1000,
|
|
status: 'uploading',
|
|
speed: 400 * 1024,
|
|
eta: 480,
|
|
},
|
|
},
|
|
})
|
|
|
|
const uploadingItem: AssetInfo = {
|
|
...mockItem,
|
|
status: 'uploading',
|
|
} as AssetInfo
|
|
|
|
renderComponent({ item: uploadingItem })
|
|
|
|
const statusBadge = screen.getByTestId('file-list-item-uploading-status')
|
|
expect(statusBadge.textContent).toMatch(/8m.*(left|剩余).*409\.6 KB\/s/i)
|
|
expect(screen.queryByTestId('file-list-item-remote-uploading-circle')).toBeNull()
|
|
})
|
|
|
|
it('renders "Uploading..." fallback during local warm-up', () => {
|
|
useUploadStore.setState({
|
|
fileProgress: {
|
|
'file-123': {
|
|
fileId: 'file-123',
|
|
name: 'very_long_file_name_for_testing_purposes.mov',
|
|
loaded: 100,
|
|
total: 1000,
|
|
status: 'uploading',
|
|
},
|
|
},
|
|
})
|
|
|
|
const uploadingItem: AssetInfo = {
|
|
...mockItem,
|
|
status: 'uploading',
|
|
} as AssetInfo
|
|
|
|
renderComponent({ item: uploadingItem })
|
|
|
|
const statusBadge = screen.getByTestId('file-list-item-uploading-status')
|
|
expect(statusBadge.textContent).toMatch(/Uploading|上传中/i)
|
|
})
|
|
|
|
it('renders "[User] is uploading..." and indeterminate circle for remote viewer', () => {
|
|
const uploadingItem: AssetInfo = {
|
|
...mockItem,
|
|
status: 'uploading',
|
|
creator: { id: 'u1', name: 'Alice' },
|
|
} as AssetInfo
|
|
|
|
renderComponent({ item: uploadingItem })
|
|
|
|
const statusBadge = screen.getByTestId('file-list-item-uploading-status')
|
|
expect(statusBadge.textContent).toMatch(/Alice.*(is uploading|正在上传)/i)
|
|
expect(screen.getByTestId('file-list-item-remote-uploading-circle')).toBeTruthy()
|
|
})
|
|
})
|