From 966e308e8b9b357fa8f7d02c80e379b24b37984f Mon Sep 17 00:00:00 2001 From: twwu Date: Thu, 29 Jan 2026 18:42:33 +0800 Subject: [PATCH] test(billing, rag-pipeline): enhance tests by adding act wrapper and mocking download utility --- .../components/billing/plan/index.spec.tsx | 8 +-- .../rag-pipeline/hooks/use-DSL.spec.ts | 52 +++++++------------ 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/web/app/components/billing/plan/index.spec.tsx b/web/app/components/billing/plan/index.spec.tsx index fb1800653e..db22b47db4 100644 --- a/web/app/components/billing/plan/index.spec.tsx +++ b/web/app/components/billing/plan/index.spec.tsx @@ -1,4 +1,4 @@ -import { fireEvent, render, screen, waitFor } from '@testing-library/react' +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react' import { EDUCATION_VERIFYING_LOCALSTORAGE_ITEM } from '@/app/education-apply/constants' import { Plan } from '../type' import PlanComp from './index' @@ -188,7 +188,9 @@ describe('PlanComp', () => { expect(lastCall.onCancel).toBeDefined() // Call onConfirm to close modal - lastCall.onConfirm() - lastCall.onCancel() + act(() => { + lastCall.onConfirm() + lastCall.onCancel() + }) }) }) diff --git a/web/app/components/rag-pipeline/hooks/use-DSL.spec.ts b/web/app/components/rag-pipeline/hooks/use-DSL.spec.ts index 0f235516e0..1b258058db 100644 --- a/web/app/components/rag-pipeline/hooks/use-DSL.spec.ts +++ b/web/app/components/rag-pipeline/hooks/use-DSL.spec.ts @@ -67,6 +67,12 @@ vi.mock('@/service/workflow', () => ({ fetchWorkflowDraft: (url: string) => mockFetchWorkflowDraft(url), })) +// Mock download utility +const mockDownloadBlob = vi.fn() +vi.mock('@/utils/download', () => ({ + downloadBlob: (options: { data: Blob, fileName: string }) => mockDownloadBlob(options), +})) + // Mock workflow constants vi.mock('@/app/components/workflow/constants', () => ({ DSL_EXPORT_CHECK: 'DSL_EXPORT_CHECK', @@ -77,33 +83,9 @@ vi.mock('@/app/components/workflow/constants', () => ({ // ============================================================================ describe('useDSL', () => { - let mockLink: { href: string, download: string, click: ReturnType } - let originalCreateElement: typeof document.createElement - let mockCreateObjectURL: ReturnType - let mockRevokeObjectURL: ReturnType - beforeEach(() => { vi.clearAllMocks() - // Create a proper mock link element - mockLink = { - href: '', - download: '', - click: vi.fn(), - } - - // Save original and mock selectively - only intercept 'a' elements - originalCreateElement = document.createElement.bind(document) - document.createElement = vi.fn((tagName: string) => { - if (tagName === 'a') { - return mockLink as unknown as HTMLElement - } - return originalCreateElement(tagName) - }) as typeof document.createElement - - mockCreateObjectURL = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:test-url') - mockRevokeObjectURL = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {}) - // Default store state mockWorkflowStoreGetState.mockReturnValue({ pipelineId: 'test-pipeline-id', @@ -118,9 +100,6 @@ describe('useDSL', () => { }) afterEach(() => { - document.createElement = originalCreateElement - mockCreateObjectURL.mockRestore() - mockRevokeObjectURL.mockRestore() vi.clearAllMocks() }) @@ -187,9 +166,10 @@ describe('useDSL', () => { await result.current.handleExportDSL() }) - expect(document.createElement).toHaveBeenCalledWith('a') - expect(mockCreateObjectURL).toHaveBeenCalled() - expect(mockRevokeObjectURL).toHaveBeenCalledWith('blob:test-url') + expect(mockDownloadBlob).toHaveBeenCalled() + const callArg = mockDownloadBlob.mock.calls[0][0] + expect(callArg.data).toBeInstanceOf(Blob) + expect(callArg.fileName).toBe('Test Knowledge Base.pipeline') }) it('should use correct file extension for download', async () => { @@ -199,17 +179,23 @@ describe('useDSL', () => { await result.current.handleExportDSL() }) - expect(mockLink.download).toBe('Test Knowledge Base.pipeline') + expect(mockDownloadBlob).toHaveBeenCalledWith( + expect.objectContaining({ + fileName: 'Test Knowledge Base.pipeline', + }), + ) }) - it('should trigger download click', async () => { + it('should trigger download with yaml blob', async () => { const { result } = renderHook(() => useDSL()) await act(async () => { await result.current.handleExportDSL() }) - expect(mockLink.click).toHaveBeenCalled() + expect(mockDownloadBlob).toHaveBeenCalled() + const callArg = mockDownloadBlob.mock.calls[0][0] + expect(callArg.data.type).toBe('application/yaml') }) it('should show error notification on export failure', async () => {