mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 23:24:21 +00:00
feat(webui): add simplified mobile-friendly layout for file and share lists (#356)
* feat(webui): add simplified mobile-friendly layout for file and share lists * refactor(webui): switch mobile file list layout to 1-column grid * feat(webui): show foldertree by default in child sidebar and add close button * feat(webui): add collapsible section headers and fix folder card full-width on mobile * feat(webui): improve mobile breadcrumb bar with middle truncation and dual ellipsis * fix(webui): unify breadcrumb DOM structure across desktop and mobile * fix(webui): remove unused breadcrumbs variable in BreadcrumbNav * fix(webui): complete and confirm file uploads on mobile and handle errors properly * fix(webui): remove active scale animation on mobile FAB to stabilize dropdown menu anchor * refactor(webui): use PresignedUrl type from dtos for uploadFiles and eliminate any cast * fix(webui): scope folder card max width to desktop with responsive md:max-w-[340px]
This commit is contained in:
@@ -0,0 +1,424 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { MobileFileBrowser } from './mobile-file-browser'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
|
||||
vi.mock('@dnd-kit/react', () => ({
|
||||
useDraggable: () => ({
|
||||
ref: vi.fn(),
|
||||
isDragging: false,
|
||||
}),
|
||||
useDroppable: () => ({
|
||||
ref: vi.fn(),
|
||||
isOver: false,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/hooks/use-permissions', () => ({
|
||||
usePermissions: () => ({
|
||||
canEdit: true,
|
||||
canAdmin: true,
|
||||
canView: true,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
files: {
|
||||
$delete: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
'download-links': {
|
||||
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({ files: [] }) }),
|
||||
},
|
||||
restore: {
|
||||
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
},
|
||||
':fileId': {
|
||||
$get: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
}),
|
||||
$put: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
folders: {
|
||||
$post: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ id: 'new-folder-id' }),
|
||||
}),
|
||||
$delete: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
restore: {
|
||||
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
},
|
||||
':folderId': {
|
||||
$put: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
},
|
||||
},
|
||||
teams: {
|
||||
':teamId': {
|
||||
upload: {
|
||||
tasks: {
|
||||
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
':taskId': {
|
||||
$patch: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
projects: {
|
||||
':projectId': {
|
||||
'empty-trash': {
|
||||
$post: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
describe('MobileFileBrowser', () => {
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
const mockFolder: AssetInfo = {
|
||||
id: 'folder-1',
|
||||
name: 'Test Folder',
|
||||
type: 'folder',
|
||||
fileCount: 3,
|
||||
sizeByte: 0,
|
||||
createdAt: '2026-01-01T00:00:00Z',
|
||||
updatedAt: '2026-01-01T00:00:00Z',
|
||||
status: 'processed',
|
||||
} as AssetInfo
|
||||
|
||||
const mockFile: AssetInfo = {
|
||||
id: 'file-1',
|
||||
name: 'test_document.pdf',
|
||||
type: 'file',
|
||||
sizeByte: 1024 * 100,
|
||||
createdAt: '2026-01-01T00:00:00Z',
|
||||
updatedAt: '2026-01-01T00:00:00Z',
|
||||
status: 'processed',
|
||||
} as AssetInfo
|
||||
|
||||
const defaultProps = {
|
||||
teamId: 'team-1',
|
||||
projectId: 'project-1',
|
||||
assetId: 'root-folder',
|
||||
folders: [mockFolder],
|
||||
files: [mockFile],
|
||||
selectedItem: null,
|
||||
selectedIds: new Set<string>(),
|
||||
onItemSelect: vi.fn(),
|
||||
onItemDoubleClick: vi.fn(),
|
||||
onClearSelection: vi.fn(),
|
||||
fetchNextFoldersPage: vi.fn(),
|
||||
hasNextFoldersPage: false,
|
||||
isFetchingNextFoldersPage: false,
|
||||
fetchNextFilesPage: vi.fn(),
|
||||
hasNextFilesPage: false,
|
||||
isFetchingNextFilesPage: false,
|
||||
}
|
||||
|
||||
const renderComponent = (props: Partial<React.ComponentProps<typeof MobileFileBrowser>> = {}) => {
|
||||
return render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MobileFileBrowser {...defaultProps} {...props} />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
}
|
||||
|
||||
it('renders folders and files in mobile grid', () => {
|
||||
renderComponent()
|
||||
|
||||
expect(screen.getByText('Test Folder')).toBeDefined()
|
||||
expect(screen.getByText('test_document.pdf')).toBeDefined()
|
||||
})
|
||||
|
||||
it('triggers onItemSelect when an item is clicked', () => {
|
||||
const onItemSelect = vi.fn()
|
||||
renderComponent({ onItemSelect })
|
||||
|
||||
const folderElement = screen.getByText('Test Folder')
|
||||
fireEvent.click(folderElement)
|
||||
|
||||
expect(onItemSelect).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('triggers onItemDoubleClick when an item is double-clicked', () => {
|
||||
const onItemDoubleClick = vi.fn()
|
||||
renderComponent({ onItemDoubleClick })
|
||||
|
||||
const folderElement = screen.getByText('Test Folder')
|
||||
fireEvent.doubleClick(folderElement)
|
||||
|
||||
expect(onItemDoubleClick).toHaveBeenCalledWith(mockFolder)
|
||||
})
|
||||
|
||||
it('renders empty state when there are no folders and files', () => {
|
||||
renderComponent({ folders: [], files: [] })
|
||||
|
||||
expect(screen.getByText(/This folder is empty|此文件夹为空/i)).toBeDefined()
|
||||
})
|
||||
|
||||
it('renders collapsible section headers for folders and files with count and size', () => {
|
||||
renderComponent({
|
||||
totalFoldersSize: 50000,
|
||||
totalFilesSize: 100000,
|
||||
})
|
||||
|
||||
expect(screen.getByText(/1 Folder • 50 KB/i)).toBeDefined()
|
||||
expect(screen.getByText(/1 Asset • 100 KB/i)).toBeDefined()
|
||||
})
|
||||
|
||||
it('collapses and expands folders and files when clicking section headers', () => {
|
||||
renderComponent()
|
||||
|
||||
expect(screen.getByText('Test Folder')).toBeDefined()
|
||||
expect(screen.getByText('test_document.pdf')).toBeDefined()
|
||||
|
||||
// Collapse folders
|
||||
const folderHeaderBtn = screen.getByRole('button', { name: /Folder/i })
|
||||
fireEvent.click(folderHeaderBtn)
|
||||
|
||||
expect(screen.queryByText('Test Folder')).toBeNull()
|
||||
|
||||
// Expand folders back
|
||||
fireEvent.click(folderHeaderBtn)
|
||||
expect(screen.getByText('Test Folder')).toBeDefined()
|
||||
|
||||
// Collapse files
|
||||
const fileHeaderBtn = screen.getByRole('button', { name: /Asset/i })
|
||||
fireEvent.click(fileHeaderBtn)
|
||||
|
||||
expect(screen.queryByText('test_document.pdf')).toBeNull()
|
||||
|
||||
// Expand files back
|
||||
fireEvent.click(fileHeaderBtn)
|
||||
expect(screen.getByText('test_document.pdf')).toBeDefined()
|
||||
})
|
||||
|
||||
it('renders Floating Action Button when canEdit is true', () => {
|
||||
renderComponent()
|
||||
|
||||
expect(screen.getByRole('button', { name: /actions/i })).toBeDefined()
|
||||
})
|
||||
|
||||
it('completes upload and confirms upload on 2xx response', async () => {
|
||||
const originalXhr = window.XMLHttpRequest
|
||||
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
|
||||
|
||||
const { client } = await import('@/ui/api/client')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(client.api.teams[':teamId'].upload.tasks as any)['$post'] = vi
|
||||
.fn()
|
||||
.mockImplementation(async (req) => {
|
||||
const json = req.json
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
taskId: 'task-1',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
presignedUrls: json.files.map((f: any) => ({
|
||||
id: f.id,
|
||||
url: 'https://upload.example.com',
|
||||
fileId: 'server-file-1',
|
||||
})),
|
||||
}),
|
||||
}
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(client.api.teams[':teamId'].upload.tasks[':taskId'] as any)['$patch'] = patchMock
|
||||
|
||||
class MockXhr {
|
||||
open = vi.fn()
|
||||
setRequestHeader = vi.fn()
|
||||
send = vi.fn(() => {
|
||||
this.status = 200
|
||||
this.onload?.()
|
||||
})
|
||||
status = 200
|
||||
upload = { onprogress: null as unknown }
|
||||
onload: (() => void) | null = null
|
||||
onerror: (() => void) | null = null
|
||||
onabort: (() => void) | null = null
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
window.XMLHttpRequest = MockXhr as any
|
||||
|
||||
const { container } = renderComponent()
|
||||
const fileInput = container.querySelector(
|
||||
'input[type="file"]:not([webkitdirectory])',
|
||||
) as HTMLInputElement
|
||||
expect(fileInput).toBeDefined()
|
||||
|
||||
const testFile = new File(['content'], 'hello.txt', { type: 'text/plain' })
|
||||
fireEvent.change(fileInput, { target: { files: [testFile] } })
|
||||
|
||||
// Wait for mutation to finish
|
||||
await vi.waitFor(() => {
|
||||
expect(patchMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
param: { teamId: 'team-1', taskId: 'task-1' },
|
||||
json: { fileId: 'server-file-1' },
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
window.XMLHttpRequest = originalXhr
|
||||
})
|
||||
|
||||
it('fails upload and confirms with error on non-2xx status', async () => {
|
||||
const originalXhr = window.XMLHttpRequest
|
||||
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
|
||||
|
||||
const { client } = await import('@/ui/api/client')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(client.api.teams[':teamId'].upload.tasks as any)['$post'] = vi
|
||||
.fn()
|
||||
.mockImplementation(async (req) => {
|
||||
const json = req.json
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
taskId: 'task-2',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
presignedUrls: json.files.map((f: any) => ({
|
||||
id: f.id,
|
||||
url: 'https://upload.example.com',
|
||||
fileId: 'server-file-2',
|
||||
})),
|
||||
}),
|
||||
}
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(client.api.teams[':teamId'].upload.tasks[':taskId'] as any)['$patch'] = patchMock
|
||||
|
||||
class MockXhr500 {
|
||||
open = vi.fn()
|
||||
setRequestHeader = vi.fn()
|
||||
send = vi.fn(() => {
|
||||
this.status = 500
|
||||
this.onload?.()
|
||||
})
|
||||
status = 500
|
||||
upload = { onprogress: null as unknown }
|
||||
onload: (() => void) | null = null
|
||||
onerror: (() => void) | null = null
|
||||
onabort: (() => void) | null = null
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
window.XMLHttpRequest = MockXhr500 as any
|
||||
|
||||
const { container } = renderComponent()
|
||||
const fileInput = container.querySelector(
|
||||
'input[type="file"]:not([webkitdirectory])',
|
||||
) as HTMLInputElement
|
||||
|
||||
const testFile = new File(['content'], 'error.txt', { type: 'text/plain' })
|
||||
fireEvent.change(fileInput, { target: { files: [testFile] } })
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(patchMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
param: { teamId: 'team-1', taskId: 'task-2' },
|
||||
json: {
|
||||
fileId: 'server-file-2',
|
||||
errorMessage: 'upload failed with status: 500',
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
window.XMLHttpRequest = originalXhr
|
||||
})
|
||||
|
||||
it('fails upload and confirms with error on network failure', async () => {
|
||||
const originalXhr = window.XMLHttpRequest
|
||||
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
|
||||
|
||||
const { client } = await import('@/ui/api/client')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(client.api.teams[':teamId'].upload.tasks as any)['$post'] = vi
|
||||
.fn()
|
||||
.mockImplementation(async (req) => {
|
||||
const json = req.json
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
taskId: 'task-3',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
presignedUrls: json.files.map((f: any) => ({
|
||||
id: f.id,
|
||||
url: 'https://upload.example.com',
|
||||
fileId: 'server-file-3',
|
||||
})),
|
||||
}),
|
||||
}
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(client.api.teams[':teamId'].upload.tasks[':taskId'] as any)['$patch'] = patchMock
|
||||
|
||||
class MockXhrError {
|
||||
open = vi.fn()
|
||||
setRequestHeader = vi.fn()
|
||||
send = vi.fn(() => {
|
||||
this.onerror?.()
|
||||
})
|
||||
status = 0
|
||||
upload = { onprogress: null as unknown }
|
||||
onload: (() => void) | null = null
|
||||
onerror: (() => void) | null = null
|
||||
onabort: (() => void) | null = null
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
window.XMLHttpRequest = MockXhrError as any
|
||||
|
||||
const { container } = renderComponent()
|
||||
const fileInput = container.querySelector(
|
||||
'input[type="file"]:not([webkitdirectory])',
|
||||
) as HTMLInputElement
|
||||
|
||||
const testFile = new File(['content'], 'network-error.txt', { type: 'text/plain' })
|
||||
fireEvent.change(fileInput, { target: { files: [testFile] } })
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(patchMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
param: { teamId: 'team-1', taskId: 'task-3' },
|
||||
json: {
|
||||
fileId: 'server-file-3',
|
||||
errorMessage: 'upload failed with error: Network error',
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
window.XMLHttpRequest = originalXhr
|
||||
})
|
||||
|
||||
it('hides Floating Action Button in public share mode', () => {
|
||||
renderComponent({ isPublic: true })
|
||||
|
||||
expect(screen.queryByRole('button', { name: /actions/i })).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user