UploadModal.test.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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: [{ index: 0, id: 1, filename: 'test.3mf', status: 'success' }],
  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. // SN-C10: rows took their outcome by file name, so two files of the same
  122. // name both showed the first one's success and the failure was lost.
  123. it('shows each file its own outcome when two share a name', async () => {
  124. server.use(
  125. http.post('/api/v1/archives/upload-bulk', () =>
  126. HttpResponse.json({
  127. uploaded: 1,
  128. failed: 1,
  129. results: [{ index: 0, id: 1, filename: 'Benchy.3mf', status: 'success' }],
  130. errors: [{ index: 1, filename: 'Benchy.3mf', error: 'Not a valid 3MF' }],
  131. }),
  132. ),
  133. );
  134. const initialFiles = [
  135. new File(['one'], 'Benchy.3mf', { type: 'application/3mf' }),
  136. new File(['two'], 'Benchy.3mf', { type: 'application/3mf' }),
  137. ];
  138. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  139. fireEvent.click(screen.getByRole('button', { name: /Upload/i }));
  140. expect(await screen.findByText('Not a valid 3MF')).toBeInTheDocument();
  141. expect(document.querySelectorAll('.text-bambu-green.w-4')).toHaveLength(1);
  142. });
  143. it('shows each failed file its own error when two share a name', async () => {
  144. server.use(
  145. http.post('/api/v1/archives/upload-bulk', () =>
  146. HttpResponse.json({
  147. uploaded: 0,
  148. failed: 2,
  149. results: [],
  150. errors: [
  151. { index: 0, filename: 'Benchy.3mf', error: 'First failed' },
  152. { index: 1, filename: 'Benchy.3mf', error: 'Second failed' },
  153. ],
  154. }),
  155. ),
  156. );
  157. const initialFiles = [
  158. new File(['one'], 'Benchy.3mf', { type: 'application/3mf' }),
  159. new File(['two'], 'Benchy.3mf', { type: 'application/3mf' }),
  160. ];
  161. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  162. fireEvent.click(screen.getByRole('button', { name: /Upload/i }));
  163. expect(await screen.findByText('First failed')).toBeInTheDocument();
  164. expect(screen.getByText('Second failed')).toBeInTheDocument();
  165. });
  166. it('Upload button is disabled when no files are pending', async () => {
  167. render(<UploadModal onClose={mockOnClose} initialFiles={[]} />);
  168. const uploadButton = screen.getByRole('button', { name: /Upload/i });
  169. expect(uploadButton).toBeDisabled();
  170. });
  171. it('shows correct file count in Upload button', () => {
  172. const initialFiles = [
  173. new File(['content'], 'file1.3mf', { type: 'application/3mf' }),
  174. new File(['content'], 'file2.3mf', { type: 'application/3mf' }),
  175. new File(['content'], 'file3.3mf', { type: 'application/3mf' }),
  176. ];
  177. render(<UploadModal onClose={mockOnClose} initialFiles={initialFiles} />);
  178. expect(screen.getByRole('button', { name: /Upload \(3\)/i })).toBeInTheDocument();
  179. });
  180. });
  181. describe('close behavior', () => {
  182. it('calls onClose when Cancel button is clicked', () => {
  183. render(<UploadModal onClose={mockOnClose} />);
  184. const cancelButton = screen.getByRole('button', { name: 'Cancel' });
  185. fireEvent.click(cancelButton);
  186. expect(mockOnClose).toHaveBeenCalled();
  187. });
  188. it('calls onClose when X button is clicked', () => {
  189. render(<UploadModal onClose={mockOnClose} />);
  190. // Find the X button in the header
  191. const buttons = screen.getAllByRole('button');
  192. const closeButton = buttons.find(btn =>
  193. btn.querySelector('.lucide-x')
  194. );
  195. if (closeButton) {
  196. fireEvent.click(closeButton);
  197. expect(mockOnClose).toHaveBeenCalled();
  198. }
  199. });
  200. it('calls onClose when Escape key is pressed', () => {
  201. render(<UploadModal onClose={mockOnClose} />);
  202. fireEvent.keyDown(window, { key: 'Escape' });
  203. expect(mockOnClose).toHaveBeenCalled();
  204. });
  205. });
  206. describe('drag and drop', () => {
  207. it('highlights drop zone on drag over', () => {
  208. render(<UploadModal onClose={mockOnClose} />);
  209. const dropZone = screen.getByText('Drag & drop .3mf files here').closest('div');
  210. if (dropZone) {
  211. fireEvent.dragOver(dropZone, {
  212. dataTransfer: { files: [] },
  213. });
  214. // The drop zone should have the highlight class
  215. expect(dropZone.className).toContain('border-bambu-green');
  216. }
  217. });
  218. it('removes highlight on drag leave', () => {
  219. render(<UploadModal onClose={mockOnClose} />);
  220. const dropZone = screen.getByText('Drag & drop .3mf files here').closest('div');
  221. if (dropZone) {
  222. fireEvent.dragOver(dropZone, { dataTransfer: { files: [] } });
  223. fireEvent.dragLeave(dropZone, { dataTransfer: { files: [] } });
  224. // The drop zone should not have the highlight class
  225. expect(dropZone.className).not.toContain('bg-bambu-green');
  226. }
  227. });
  228. });
  229. describe('file size display', () => {
  230. it('shows file size in MB', () => {
  231. const file = new File(['x'.repeat(1048576)], 'large.3mf', { type: 'application/3mf' }); // 1 MB
  232. render(<UploadModal onClose={mockOnClose} initialFiles={[file]} />);
  233. expect(screen.getByText('1.0 MB')).toBeInTheDocument();
  234. });
  235. });
  236. });