UploadModal.test.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Tests for the UploadModal component.
  3. * Tests file upload functionality with drag-and-drop support.
  4. */
  5. import { describe, it, expect, vi, beforeEach } from 'vitest';
  6. import { screen, fireEvent, waitFor } from '@testing-library/react';
  7. import { render } from '../utils';
  8. import { UploadModal } from '../../components/UploadModal';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. describe('UploadModal', () => {
  12. const mockOnClose = vi.fn();
  13. beforeEach(() => {
  14. vi.clearAllMocks();
  15. server.use(
  16. http.post('/api/v1/archives/upload-bulk', async () => {
  17. return HttpResponse.json({
  18. uploaded: 1,
  19. failed: 0,
  20. results: [{ id: 1, filename: 'test.3mf' }],
  21. errors: [],
  22. });
  23. })
  24. );
  25. });
  26. describe('rendering', () => {
  27. it('renders the modal with title', () => {
  28. render(<UploadModal onClose={mockOnClose} />);
  29. expect(screen.getByText('Upload 3MF Files')).toBeInTheDocument();
  30. });
  31. it('renders drag and drop zone', () => {
  32. render(<UploadModal onClose={mockOnClose} />);
  33. expect(screen.getByText('Drag & drop .3mf files here')).toBeInTheDocument();
  34. });
  35. it('renders Browse Files button', () => {
  36. render(<UploadModal onClose={mockOnClose} />);
  37. expect(screen.getByRole('button', { name: 'Browse Files' })).toBeInTheDocument();
  38. });
  39. it('renders Cancel button', () => {
  40. render(<UploadModal onClose={mockOnClose} />);
  41. expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
  42. });
  43. it('renders Upload button (disabled initially)', () => {
  44. render(<UploadModal onClose={mockOnClose} />);
  45. const uploadButton = screen.getByRole('button', { name: /Upload/i });
  46. expect(uploadButton).toBeDisabled();
  47. });
  48. });
  49. describe('file handling with initialFiles', () => {
  50. it('shows initial files when provided', () => {
  51. const initialFiles = [
  52. new File(['content'], 'model.3mf', { type: 'application/3mf' }),
  53. ];
  54. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  55. expect(screen.getByText('model.3mf')).toBeInTheDocument();
  56. });
  57. it('enables Upload button when files are present', () => {
  58. const initialFiles = [
  59. new File(['content'], 'model.3mf', { type: 'application/3mf' }),
  60. ];
  61. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  62. const uploadButton = screen.getByRole('button', { name: /Upload/i });
  63. expect(uploadButton).not.toBeDisabled();
  64. });
  65. it('shows file count in Upload button', () => {
  66. const initialFiles = [
  67. new File(['content'], 'model1.3mf', { type: 'application/3mf' }),
  68. new File(['content'], 'model2.3mf', { type: 'application/3mf' }),
  69. ];
  70. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  71. expect(screen.getByRole('button', { name: /Upload \(2\)/i })).toBeInTheDocument();
  72. });
  73. it('filters out non-3mf files from initialFiles', () => {
  74. const initialFiles = [
  75. new File(['content'], 'model.3mf', { type: 'application/3mf' }),
  76. new File(['content'], 'image.png', { type: 'image/png' }),
  77. new File(['content'], 'doc.txt', { type: 'text/plain' }),
  78. ];
  79. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  80. expect(screen.getByText('model.3mf')).toBeInTheDocument();
  81. expect(screen.queryByText('image.png')).not.toBeInTheDocument();
  82. expect(screen.queryByText('doc.txt')).not.toBeInTheDocument();
  83. });
  84. });
  85. describe('file removal', () => {
  86. it('allows removing a file before upload', async () => {
  87. const initialFiles = [
  88. new File(['content'], 'model.3mf', { type: 'application/3mf' }),
  89. ];
  90. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  91. expect(screen.getByText('model.3mf')).toBeInTheDocument();
  92. // Find and click the remove button (X icon next to file)
  93. const fileItem = screen.getByText('model.3mf').closest('.flex');
  94. const removeButton = fileItem?.querySelector('button');
  95. if (removeButton) {
  96. fireEvent.click(removeButton);
  97. await waitFor(() => {
  98. expect(screen.queryByText('model.3mf')).not.toBeInTheDocument();
  99. });
  100. }
  101. });
  102. });
  103. describe('upload button behavior', () => {
  104. it('Upload button triggers upload mutation when clicked', async () => {
  105. const initialFiles = [
  106. new File(['content'], 'test.3mf', { type: 'application/3mf' }),
  107. ];
  108. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  109. const uploadButton = screen.getByRole('button', { name: /Upload/i });
  110. expect(uploadButton).not.toBeDisabled();
  111. // Click should trigger upload (button text will change)
  112. fireEvent.click(uploadButton);
  113. // The button should show uploading state or become disabled
  114. await waitFor(() => {
  115. // Either showing "Uploading..." or a spinner is present
  116. const hasUploadingText = screen.queryByText(/Uploading/i) !== null;
  117. const hasSpinner = document.querySelector('.animate-spin') !== null;
  118. expect(hasUploadingText || hasSpinner).toBe(true);
  119. });
  120. });
  121. it('Upload button is disabled when no files are pending', async () => {
  122. render(<UploadModal onClose={mockOnClose} initialFiles={[]} />);
  123. const uploadButton = screen.getByRole('button', { name: /Upload/i });
  124. expect(uploadButton).toBeDisabled();
  125. });
  126. it('shows correct file count in Upload button', () => {
  127. const initialFiles = [
  128. new File(['content'], 'file1.3mf', { type: 'application/3mf' }),
  129. new File(['content'], 'file2.3mf', { type: 'application/3mf' }),
  130. new File(['content'], 'file3.3mf', { type: 'application/3mf' }),
  131. ];
  132. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  133. expect(screen.getByRole('button', { name: /Upload \(3\)/i })).toBeInTheDocument();
  134. });
  135. });
  136. describe('close behavior', () => {
  137. it('calls onClose when Cancel button is clicked', () => {
  138. render(<UploadModal onClose={mockOnClose} />);
  139. const cancelButton = screen.getByRole('button', { name: 'Cancel' });
  140. fireEvent.click(cancelButton);
  141. expect(mockOnClose).toHaveBeenCalled();
  142. });
  143. it('calls onClose when X button is clicked', () => {
  144. render(<UploadModal onClose={mockOnClose} />);
  145. // Find the X button in the header
  146. const buttons = screen.getAllByRole('button');
  147. const closeButton = buttons.find(btn =>
  148. btn.querySelector('.lucide-x')
  149. );
  150. if (closeButton) {
  151. fireEvent.click(closeButton);
  152. expect(mockOnClose).toHaveBeenCalled();
  153. }
  154. });
  155. it('calls onClose when Escape key is pressed', () => {
  156. render(<UploadModal onClose={mockOnClose} />);
  157. fireEvent.keyDown(window, { key: 'Escape' });
  158. expect(mockOnClose).toHaveBeenCalled();
  159. });
  160. });
  161. describe('drag and drop', () => {
  162. it('highlights drop zone on drag over', () => {
  163. render(<UploadModal onClose={mockOnClose} />);
  164. const dropZone = screen.getByText('Drag & drop .3mf files here').closest('div');
  165. if (dropZone) {
  166. fireEvent.dragOver(dropZone, {
  167. dataTransfer: { files: [] },
  168. });
  169. // The drop zone should have the highlight class
  170. expect(dropZone.className).toContain('border-bambu-green');
  171. }
  172. });
  173. it('removes highlight on drag leave', () => {
  174. render(<UploadModal onClose={mockOnClose} />);
  175. const dropZone = screen.getByText('Drag & drop .3mf files here').closest('div');
  176. if (dropZone) {
  177. fireEvent.dragOver(dropZone, { dataTransfer: { files: [] } });
  178. fireEvent.dragLeave(dropZone, { dataTransfer: { files: [] } });
  179. // The drop zone should not have the highlight class
  180. expect(dropZone.className).not.toContain('bg-bambu-green');
  181. }
  182. });
  183. });
  184. describe('file size display', () => {
  185. it('shows file size in MB', () => {
  186. const file = new File(['x'.repeat(1048576)], 'large.3mf', { type: 'application/3mf' }); // 1 MB
  187. render(<UploadModal onClose={mockOnClose} initialFiles={[file]} />);
  188. expect(screen.getByText('1.0 MB')).toBeInTheDocument();
  189. });
  190. });
  191. });