mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-05-01 21:20:15 +00:00
feat: implement delete functionality in SearchHistoryDialog and add corresponding tests
This commit is contained in:
parent
6683a54abb
commit
21299788c3
3 changed files with 174 additions and 13 deletions
133
test/unit/components/SearchHistoryDialog.test.tsx
Normal file
133
test/unit/components/SearchHistoryDialog.test.tsx
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
|
||||
import { SearchHistoryDialog } from '../../../src/components/SearchHistoryDialog'
|
||||
|
||||
// ---- Mocks ----
|
||||
|
||||
vi.mock('react-router-dom', () => ({
|
||||
useNavigate: () => vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useChatStoreAdapter', () => ({
|
||||
default: () => ({
|
||||
chatStore: { activeTaskId: undefined },
|
||||
projectStore: {
|
||||
getProjectById: vi.fn(() => null),
|
||||
setHistoryId: vi.fn(),
|
||||
setActiveProject: vi.fn(),
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/store/globalStore', () => ({
|
||||
useGlobalStore: () => ({ history_type: 'list' }),
|
||||
}))
|
||||
|
||||
const proxyFetchDeleteMock = vi.fn().mockResolvedValue({ code: 0 })
|
||||
vi.mock('@/api/http', () => ({
|
||||
proxyFetchDelete: (...args: any[]) => proxyFetchDeleteMock(...args),
|
||||
}))
|
||||
|
||||
vi.mock('@/store/authStore', () => ({
|
||||
getAuthStore: () => ({ email: 'test@example.com' }),
|
||||
}))
|
||||
|
||||
vi.mock('@/service/historyApi', () => ({
|
||||
fetchHistoryTasks: (setter: (tasks: any[]) => void) => {
|
||||
setter([
|
||||
{
|
||||
id: '1',
|
||||
task_id: 'task-1',
|
||||
project_id: 'project-1',
|
||||
question: 'My history item',
|
||||
},
|
||||
])
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/components/ui/command', () => ({
|
||||
CommandDialog: ({ open, children }: any) => (open ? <div data-testid="cmd-dialog">{children}</div> : null),
|
||||
CommandEmpty: ({ children }: any) => <div>{children}</div>,
|
||||
CommandGroup: ({ children }: any) => <div>{children}</div>,
|
||||
CommandInput: (props: any) => <input aria-label="command-input" {...props} />,
|
||||
CommandItem: ({ children, onSelect }: any) => (
|
||||
<div role="button" tabIndex={0} onClick={() => onSelect?.('')}>
|
||||
{children}
|
||||
</div>
|
||||
),
|
||||
CommandList: ({ children }: any) => <div>{children}</div>,
|
||||
CommandSeparator: () => <hr />,
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/components/ui/button', () => ({
|
||||
Button: ({ children, onClick, ...rest }: any) => (
|
||||
<button onClick={onClick} {...rest}>
|
||||
{children}
|
||||
</button>
|
||||
),
|
||||
}))
|
||||
|
||||
vi.mock('../../../src/components/ui/dialog', () => ({
|
||||
DialogTitle: ({ children }: any) => <div>{children}</div>,
|
||||
}))
|
||||
|
||||
vi.mock('@radix-ui/react-visually-hidden', () => ({
|
||||
VisuallyHidden: ({ children }: any) => <span>{children}</span>,
|
||||
}))
|
||||
|
||||
vi.mock('lucide-react', () => ({
|
||||
Search: (props: any) => <div data-testid="search-icon" {...props} />,
|
||||
ScanFace: (props: any) => <div data-testid="scanface-icon" {...props} />,
|
||||
Trash2: (props: any) => <div data-testid="trash-icon" {...props} />,
|
||||
}))
|
||||
|
||||
vi.mock('@/components/GroupedHistoryView', () => ({
|
||||
default: () => <div data-testid="grouped-history-view" />,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib', () => ({
|
||||
replayProject: vi.fn(),
|
||||
}))
|
||||
|
||||
describe('SearchHistoryDialog', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
;(window as any).ipcRenderer = {
|
||||
invoke: vi.fn().mockResolvedValue(undefined),
|
||||
}
|
||||
})
|
||||
|
||||
it('deletes a history item from list view', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<SearchHistoryDialog />)
|
||||
|
||||
// Open dialog
|
||||
await user.click(screen.getByRole('button'))
|
||||
|
||||
// History item should appear
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('My history item')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
// Click delete button
|
||||
const deleteButton = screen.getByRole('button', { name: 'Delete history' })
|
||||
await user.click(deleteButton)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(proxyFetchDeleteMock).toHaveBeenCalledWith('/api/chat/history/1')
|
||||
})
|
||||
|
||||
expect((window as any).ipcRenderer.invoke).toHaveBeenCalledWith(
|
||||
'delete-task-files',
|
||||
'test@example.com',
|
||||
'task-1',
|
||||
'project-1'
|
||||
)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText('My history item')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue