refactor(tests): improve mock document creation and enhance test stability

- Updated the  function to use a deterministic counter for IDs, reducing flakiness in tests.
- Ensured the ID counter resets before each test to maintain reproducibility.
- Adjusted assertions in the  tests to explicitly check for error handling when the documents array is empty, improving clarity and reliability of edge case testing.
- Fixed a link in the  tests to ensure it points to the correct documentation URL.

These changes enhance the robustness and reliability of the testing suite for document processing components.
This commit is contained in:
CodingOnStar
2025-12-18 18:50:21 +08:00
parent 050acae92d
commit dae130d8d2
3 changed files with 36 additions and 28 deletions

View File

@@ -68,9 +68,11 @@ jest.mock('@/hooks/use-api-access-url', () => ({
/**
* Creates a mock InitialDocumentDetail for testing
* Uses deterministic counter-based IDs to avoid flaky tests
*/
let documentIdCounter = 0
const createMockDocument = (overrides: Partial<InitialDocumentDetail> = {}): InitialDocumentDetail => ({
id: `doc-${Math.random().toString(36).slice(2, 9)}`,
id: overrides.id ?? `doc-${++documentIdCounter}`,
name: 'test-document.txt',
data_source_type: DatasourceType.localFile,
data_source_info: {},
@@ -127,6 +129,9 @@ describe('EmbeddingProcess', () => {
jest.clearAllMocks()
jest.useFakeTimers()
// Reset deterministic ID counter for reproducible tests
documentIdCounter = 0
// Reset mock states
mockEnableBilling = false
mockPlanType = Plan.sandbox
@@ -908,26 +913,20 @@ describe('EmbeddingProcess', () => {
// ==========================================
describe('Edge Cases', () => {
// Tests for boundary conditions
it('should handle empty documents array gracefully', () => {
it('should throw error when documents array is empty', () => {
// Arrange
// Note: The component accesses documents[0].id for useProcessRule,
// so empty array will cause undefined access. This tests that behavior.
// The component accesses documents[0].id for useProcessRule (line 81-82),
// which throws TypeError when documents array is empty.
// This test documents this known limitation.
const props = createDefaultProps({ documents: [] })
// Suppress console errors for this test
// Suppress console errors for expected error
const consoleError = jest.spyOn(console, 'error').mockImplementation(Function.prototype as () => void)
// Act - component may throw due to accessing documents[0].id
// This documents the current behavior
try {
// Act & Assert - explicitly assert the error behavior
expect(() => {
render(<EmbeddingProcess {...props} />)
// If it doesn't throw, that's also acceptable
expect(screen.getByTestId('rule-detail')).toBeInTheDocument()
}
catch {
// Expected behavior when documents array is empty
expect(true).toBe(true)
}
}).toThrow(TypeError)
consoleError.mockRestore()
})

View File

@@ -58,9 +58,11 @@ jest.mock('./embedding-process', () => ({
/**
* Creates a mock InitialDocumentDetail for testing
* Uses deterministic counter-based IDs to avoid flaky tests
*/
let documentIdCounter = 0
const createMockDocument = (overrides: Partial<InitialDocumentDetail> = {}): InitialDocumentDetail => ({
id: `doc-${Math.random().toString(36).slice(2, 9)}`,
id: overrides.id ?? `doc-${++documentIdCounter}`,
name: 'test-document.txt',
data_source_type: DatasourceType.localFile,
data_source_info: {},
@@ -91,6 +93,8 @@ describe('Processing', () => {
beforeEach(() => {
jest.clearAllMocks()
embeddingProcessProps = {}
// Reset deterministic ID counter for reproducible tests
documentIdCounter = 0
// Reset mock dataset with default values
mockDataset = {
id: 'dataset-123',
@@ -160,7 +164,7 @@ describe('Processing', () => {
// Assert
const link = screen.getByRole('link', { name: 'datasetPipeline.addDocuments.stepThree.learnMore' })
expect(link).toHaveAttribute('href', 'https://docs.dify.ai/en-US//guides/knowledge-base/integrate-knowledge-within-application')
expect(link).toHaveAttribute('href', 'https://docs.dify.ai/en-US/guides/knowledge-base/integrate-knowledge-within-application')
expect(link).toHaveAttribute('target', '_blank')
expect(link).toHaveAttribute('rel', 'noreferrer noopener')
})