diff --git a/src/components/ProjectSidebar.tsx b/src/components/ProjectSidebar.tsx index ba4567fe..3d6782a0 100644 --- a/src/components/ProjectSidebar.tsx +++ b/src/components/ProjectSidebar.tsx @@ -123,10 +123,10 @@ export function ProjectSidebar({ } }; - const handleImportZip = async (file: File, customId?: string, overwrite = false) => { + const handleImportZip = async (file: File, overwrite = false, projectId?: string) => { try { // Import project from zip file - const project = await projectsManager.importProjectFromZip(file, customId, overwrite); + const project = await projectsManager.importProjectFromZip(file, projectId, overwrite); // Show success toast toast({ diff --git a/src/components/ZipImportDialog.test.tsx b/src/components/ZipImportDialog.test.tsx new file mode 100644 index 00000000..19ecadbe --- /dev/null +++ b/src/components/ZipImportDialog.test.tsx @@ -0,0 +1,203 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; +import { ZipImportDialog } from './ZipImportDialog'; +import { TestApp } from '@/test/TestApp'; + +// Mock the useProjects hook +const mockProjects = [ + { id: 'test-project', name: 'Test Project', path: '/projects/test-project', lastModified: new Date() } +]; + +vi.mock('@/hooks/useProjects', () => ({ + useProjects: () => ({ + data: mockProjects, + isLoading: false, + error: null + }) +})); + +describe('ZipImportDialog', () => { + const mockOnImport = vi.fn(); + const defaultProps = { + onImport: mockOnImport, + disabled: false + }; + + beforeEach(() => { + mockOnImport.mockClear(); + vi.clearAllMocks(); + }); + + it('renders dialog trigger button', () => { + render( + + + + ); + + expect(screen.getByText('Import ZIP')).toBeInTheDocument(); + }); + + it('opens dialog when trigger is clicked', async () => { + render( + + + + ); + + const triggerButton = screen.getByText('Import ZIP'); + fireEvent.click(triggerButton); + + await waitFor(() => { + expect(screen.getByText('Import Project from ZIP')).toBeInTheDocument(); + }); + }); + + it('processes file selection without displaying project ID', async () => { + render( + + + + ); + + // Open dialog + const triggerButton = screen.getByText('Import ZIP'); + fireEvent.click(triggerButton); + + // Create a mock file + const file = new File(['test content'], 'my-project.zip', { type: 'application/zip' }); + + // Find the hidden file input by ref + const fileInput = document.querySelector('input[type="file"][accept=".zip"]'); + + if (fileInput) { + fireEvent.change(fileInput, { target: { files: [file] } }); + } + + await waitFor(() => { + // Verify that the file name is displayed (from the file selection area) + expect(screen.getByText('my-project.zip')).toBeInTheDocument(); + // Verify that Project ID label is not displayed + expect(screen.queryByText('Project ID')).not.toBeInTheDocument(); + }); + }); + + it('shows overwrite warning when project ID already exists', async () => { + render( + + + + ); + + // Open dialog + const triggerButton = screen.getByText('Import ZIP'); + fireEvent.click(triggerButton); + + // Create a mock file that will generate an existing project ID + const file = new File(['test content'], 'test-project.zip', { type: 'application/zip' }); + + // Find the hidden file input by ref + const fileInput = document.querySelector('input[type="file"][accept=".zip"]'); + + if (fileInput) { + fireEvent.change(fileInput, { target: { files: [file] } }); + } + + await waitFor(() => { + expect(screen.getByText(/A project with this ID already exists/)).toBeInTheDocument(); + expect(screen.getByText('Overwrite Project')).toBeInTheDocument(); + }); + }); + + it('calls onImport with correct parameters when import button is clicked', async () => { + render( + + + + ); + + // Open dialog + const triggerButton = screen.getByText('Import ZIP'); + fireEvent.click(triggerButton); + + // Create a mock file + const file = new File(['test content'], 'new-project.zip', { type: 'application/zip' }); + + // Find the hidden file input by ref + const fileInput = document.querySelector('input[type="file"][accept=".zip"]'); + + if (fileInput) { + fireEvent.change(fileInput, { target: { files: [file] } }); + } + + // Wait for file to be processed + await waitFor(() => { + expect(screen.getByText('new-project.zip')).toBeInTheDocument(); + }); + + // Click import button + const importButton = screen.getByText('Import Project'); + fireEvent.click(importButton); + + await waitFor(() => { + expect(mockOnImport).toHaveBeenCalledWith(file, false, "new-project"); + }); + }); + + it('shows overwrite confirmation dialog when overwrite button is clicked', async () => { + render( + + + + ); + + // Open dialog + const triggerButton = screen.getByText('Import ZIP'); + fireEvent.click(triggerButton); + + // Create a mock file that will generate an existing project ID + const file = new File(['test content'], 'test-project.zip', { type: 'application/zip' }); + + // Find the hidden file input by ref + const fileInput = document.querySelector('input[type="file"][accept=".zip"]'); + + if (fileInput) { + fireEvent.change(fileInput, { target: { files: [file] } }); + } + + // Wait for overwrite button to appear + await waitFor(() => { + expect(screen.getByText('Overwrite Project')).toBeInTheDocument(); + }); + + // Click overwrite button + const overwriteButton = screen.getByText('Overwrite Project'); + fireEvent.click(overwriteButton); + + // Check for overwrite confirmation dialog + await waitFor(() => { + expect(screen.getByText('Overwrite Existing Project')).toBeInTheDocument(); + // Use a more flexible matcher for the text that might be split across elements + const dialogContent = screen.getByText('Overwrite Existing Project').closest('.AlertDialogContent'); + if (dialogContent) { + expect(dialogContent.textContent).toContain('A project with the ID "test-project" already exists'); + } + }, { timeout: 2000 }); + }); + + it('does not show project ID input field (simplified interface)', async () => { + render( + + + + ); + + // Open dialog + const triggerButton = screen.getByText('Import ZIP'); + fireEvent.click(triggerButton); + + // Check that there's no input field for project ID + expect(screen.queryByLabelText('Project ID')).not.toBeInTheDocument(); + expect(screen.queryByPlaceholderText('project-name')).not.toBeInTheDocument(); + }); +}); \ No newline at end of file diff --git a/src/components/ZipImportDialog.tsx b/src/components/ZipImportDialog.tsx index 35a8f7ff..5f6a3436 100644 --- a/src/components/ZipImportDialog.tsx +++ b/src/components/ZipImportDialog.tsx @@ -8,7 +8,7 @@ import { useProjects } from '@/hooks/useProjects'; import { cn } from '@/lib/utils'; interface ZipImportDialogProps { - onImport: (file: File, customId?: string, overwrite?: boolean) => Promise; + onImport: (file: File, overwrite?: boolean, projectId?: string) => Promise; disabled?: boolean; className?: string; open?: boolean; @@ -21,7 +21,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o const isOpen = open !== undefined ? open : internalIsOpen; const setIsOpen = onOpenChange || setInternalIsOpen; const [selectedFile, setSelectedFile] = useState(null); - const [customId, setCustomId] = useState(''); + const [generatedId, setGeneratedId] = useState(''); const [isImporting, setIsImporting] = useState(false); const [importProgress, setImportProgress] = useState(0); const [error, setError] = useState(null); @@ -41,24 +41,24 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o setSelectedFile(file); setError(null); - // Auto-generate custom ID from file name + // Auto-generate ID from file name const baseName = file.name.replace(/\.zip$/i, ''); - const generatedId = baseName + const id = baseName .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); - setCustomId(generatedId); + setGeneratedId(id); }; - // Check for existing project when custom ID changes + // Check for existing project when generated ID changes useEffect(() => { - if (customId && projects.length > 0) { - const existing = projects.find(project => project.id === customId); + if (generatedId && projects.length > 0) { + const existing = projects.find(project => project.id === generatedId); setExistingProject(existing || null); } else { setExistingProject(null); } - }, [customId, projects]); + }, [generatedId, projects]); // Drag and drop handlers const handleDragOver = useCallback((e: React.DragEvent) => { @@ -110,7 +110,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o const handleFileRemove = () => { setSelectedFile(null); - setCustomId(''); + setGeneratedId(''); setError(null); }; @@ -127,7 +127,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o setImportProgress(prev => Math.min(prev + 10, 90)); }, 200); - await onImport(selectedFile, customId || undefined, overwrite); + await onImport(selectedFile, overwrite, generatedId); clearInterval(progressInterval); setImportProgress(100); @@ -136,7 +136,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o setTimeout(() => { setIsOpen(false); setSelectedFile(null); - setCustomId(''); + setGeneratedId(''); setImportProgress(0); setExistingProject(null); }, 1000); @@ -149,7 +149,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o }; const handleImportWithOverwriteCheck = async () => { - if (!selectedFile || !customId) return; + if (!selectedFile || !generatedId) return; // Check if project with this ID already exists try { @@ -175,7 +175,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o if (!isImporting) { setIsOpen(false); setSelectedFile(null); - setCustomId(''); + setGeneratedId(''); setError(null); setImportProgress(0); } @@ -290,30 +290,13 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o )} - {/* Custom Project ID */} -
- - setCustomId(e.target.value)} - placeholder="project-name" - className="w-full px-3 py-2 text-sm border border-input rounded-md bg-background focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2" - disabled={isImporting} - /> -

- Auto-generated from filename. Only use lowercase letters, numbers, and hyphens. -

- {existingProject && ( -
- - A project with this ID already exists. You can choose to overwrite it. -
- )} -
+ {/* Overwrite Warning */} + {existingProject && ( +
+ + A project with this ID already exists. You can choose to overwrite it. +
+ )} {/* Import Button */}
@@ -322,7 +305,7 @@ export function ZipImportDialog({ onImport, disabled = false, className, open, o