Files
dify/web/app/components/datasets/create/website/base/crawled-result-item.spec.tsx
CodingOnStar 4655a6a244 test: refactor and enhance unit tests for dataset creation components
- Moved unit tests for child components (usePreviewState, DataSourceTypeSelector, NextStepButton, PreviewPanel) to dedicated spec files for better organization.
- Added new tests for the StepTwo component, covering rendering, user interactions, and state management.
- Improved test coverage for CrawledResultItem, ensuring proper handling of checkbox interactions.
- Updated tests for MenuBar and other components to validate user interactions and rendering.

These changes enhance the maintainability and reliability of the dataset creation and processing features.
2026-02-10 18:41:23 +08:00

48 lines
1.8 KiB
TypeScript

import type { CrawlResultItem as CrawlResultItemType } from '@/models/datasets'
import { fireEvent, render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import CrawledResultItem from './crawled-result-item'
vi.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}))
describe('CrawledResultItem', () => {
const defaultProps = {
payload: { title: 'Example Page', source_url: 'https://example.com/page' } as CrawlResultItemType,
isChecked: false,
isPreview: false,
onCheckChange: vi.fn(),
onPreview: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
})
it('should render title and url', () => {
render(<CrawledResultItem {...defaultProps} />)
expect(screen.getByText('Example Page')).toBeInTheDocument()
expect(screen.getByText('https://example.com/page')).toBeInTheDocument()
})
it('should apply active styling when isPreview', () => {
const { container } = render(<CrawledResultItem {...defaultProps} isPreview={true} />)
expect((container.firstChild as HTMLElement).className).toContain('bg-state-base-active')
})
it('should call onCheckChange with true when unchecked checkbox is clicked', () => {
render(<CrawledResultItem {...defaultProps} isChecked={false} testId="crawl-item" />)
const checkbox = screen.getByTestId('checkbox-crawl-item')
fireEvent.click(checkbox)
expect(defaultProps.onCheckChange).toHaveBeenCalledWith(true)
})
it('should call onCheckChange with false when checked checkbox is clicked', () => {
render(<CrawledResultItem {...defaultProps} isChecked={true} testId="crawl-item" />)
const checkbox = screen.getByTestId('checkbox-crawl-item')
fireEvent.click(checkbox)
expect(defaultProps.onCheckChange).toHaveBeenCalledWith(false)
})
})