Files
dify/web/app/components/datasets/hit-testing/modify-retrieval-modal.spec.tsx
CodingOnStar 5006a5e804 test: add unit tests for website crawl and document preview components
- Introduced new test files for CheckboxWithLabel, CrawledResultItem, ErrorMessage, and various components related to website crawling and document preview.
- Enhanced test coverage by validating rendering, user interactions, and edge cases for each component.
- Ensured proper functionality of user interactions such as checkbox selections, button clicks, and rendering of dynamic content.

These additions improve the reliability and maintainability of the website crawl and document preview features.
2026-02-10 17:31:40 +08:00

125 lines
4.1 KiB
TypeScript

import type { RetrievalConfig } from '@/types/app'
import { fireEvent, render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { RETRIEVE_METHOD } from '@/types/app'
import ModifyRetrievalModal from './modify-retrieval-modal'
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string) => key,
}),
}))
vi.mock('@remixicon/react', () => ({
RiCloseLine: () => <span data-testid="close-icon" />,
}))
vi.mock('@/app/components/base/button', () => ({
default: ({ children, onClick, variant }: { children: React.ReactNode, onClick: () => void, variant?: string }) => (
<button data-testid={variant === 'primary' ? 'save-button' : 'cancel-button'} onClick={onClick}>
{children}
</button>
),
}))
vi.mock('@/app/components/datasets/common/check-rerank-model', () => ({
isReRankModelSelected: vi.fn(() => true),
}))
vi.mock('@/app/components/datasets/common/retrieval-method-config', () => ({
default: ({ value, onChange }: { value: RetrievalConfig, onChange: (v: RetrievalConfig) => void }) => (
<div data-testid="retrieval-method-config">
<span>{value.search_method}</span>
<button data-testid="change-config" onClick={() => onChange({ ...value, search_method: RETRIEVE_METHOD.hybrid })}>change</button>
</div>
),
}))
vi.mock('@/app/components/datasets/common/economical-retrieval-method-config', () => ({
default: () => <div data-testid="economical-config" />,
}))
vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', () => ({
useModelList: () => ({ data: [] }),
}))
vi.mock('@/context/dataset-detail', () => ({
useDatasetDetailContextWithSelector: () => 'model-name',
}))
vi.mock('@/context/i18n', () => ({
useDocLink: () => (path: string) => `https://docs.dify.ai${path}`,
}))
vi.mock('../../base/toast', () => ({
default: { notify: vi.fn() },
}))
vi.mock('../settings/utils', () => ({
checkShowMultiModalTip: () => false,
}))
describe('ModifyRetrievalModal', () => {
const defaultProps = {
indexMethod: 'high_quality',
value: {
search_method: 'semantic_search',
reranking_enable: false,
reranking_model: {
reranking_provider_name: '',
reranking_model_name: '',
},
} as RetrievalConfig,
isShow: true,
onHide: vi.fn(),
onSave: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
})
it('should return null when isShow is false', () => {
const { container } = render(<ModifyRetrievalModal {...defaultProps} isShow={false} />)
expect(container.firstChild).toBeNull()
})
it('should render title when isShow is true', () => {
render(<ModifyRetrievalModal {...defaultProps} />)
expect(screen.getByText('form.retrievalSetting.title')).toBeInTheDocument()
})
it('should render high quality retrieval config for high_quality index', () => {
render(<ModifyRetrievalModal {...defaultProps} />)
expect(screen.getByTestId('retrieval-method-config')).toBeInTheDocument()
})
it('should render economical config for non high_quality index', () => {
render(<ModifyRetrievalModal {...defaultProps} indexMethod="economy" />)
expect(screen.getByTestId('economical-config')).toBeInTheDocument()
})
it('should call onHide when close button clicked', () => {
render(<ModifyRetrievalModal {...defaultProps} />)
fireEvent.click(screen.getByTestId('close-icon'))
expect(defaultProps.onHide).toHaveBeenCalled()
})
it('should call onHide when cancel button clicked', () => {
render(<ModifyRetrievalModal {...defaultProps} />)
fireEvent.click(screen.getByTestId('cancel-button'))
expect(defaultProps.onHide).toHaveBeenCalled()
})
it('should call onSave with retrieval config when save clicked', () => {
render(<ModifyRetrievalModal {...defaultProps} />)
fireEvent.click(screen.getByTestId('save-button'))
expect(defaultProps.onSave).toHaveBeenCalled()
})
it('should render learn more link', () => {
render(<ModifyRetrievalModal {...defaultProps} />)
expect(screen.getByText('form.retrievalSetting.learnMore')).toBeInTheDocument()
})
})