SpoolCsvImportModal.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, it, expect, vi, beforeEach } from 'vitest';
  2. import { screen, waitFor, fireEvent } from '@testing-library/react';
  3. import { render } from '../utils';
  4. import { SpoolCsvImportModal } from '../../components/SpoolCsvImportModal';
  5. import { api, type CsvImportPreview } from '../../api/client';
  6. vi.mock('../../api/client', () => ({
  7. api: {
  8. importSpoolsCsvPreview: vi.fn(),
  9. importSpoolsCsv: vi.fn(),
  10. getSettings: vi.fn().mockResolvedValue({}),
  11. getAuthStatus: vi.fn().mockResolvedValue({ auth_enabled: false }),
  12. },
  13. }));
  14. const preview: CsvImportPreview = {
  15. columns: ['material', 'brand', 'color_name', 'rgba'],
  16. total: 3,
  17. valid_count: 2,
  18. error_count: 1,
  19. skipped_count: 0,
  20. warnings: [],
  21. rows: [
  22. { row_number: 1, status: 'valid', reason: null, material: 'PLA', brand: 'Polymaker', color_name: 'White', rgba: 'ffffffff', resolved_color: false, cross_material_color: false, duplicate_of_existing: false },
  23. { row_number: 2, status: 'error', reason: 'material is required', material: null, brand: 'Polymaker', color_name: 'X', rgba: null, resolved_color: false, cross_material_color: false, duplicate_of_existing: false },
  24. { row_number: 3, status: 'valid', reason: null, material: 'PETG', brand: 'Brand', color_name: 'Jade', rgba: 'e8e8e8ff', resolved_color: true, cross_material_color: true, duplicate_of_existing: false },
  25. ],
  26. };
  27. function selectFile() {
  28. const input = document.querySelector('input[type="file"]') as HTMLInputElement;
  29. const file = new File(['material\nPLA\n'], 'inventory.csv', { type: 'text/csv' });
  30. fireEvent.change(input, { target: { files: [file] } });
  31. return file;
  32. }
  33. describe('SpoolCsvImportModal', () => {
  34. beforeEach(() => {
  35. vi.clearAllMocks();
  36. });
  37. it('shows the preview table with per-row status after a file is chosen', async () => {
  38. vi.mocked(api.importSpoolsCsvPreview).mockResolvedValue(preview);
  39. render(<SpoolCsvImportModal onClose={vi.fn()} onImported={vi.fn()} />);
  40. selectFile();
  41. await waitFor(() => expect(api.importSpoolsCsvPreview).toHaveBeenCalledOnce());
  42. // Summary counts surfaced.
  43. expect(await screen.findByText('2 valid')).toBeInTheDocument();
  44. expect(screen.getByText('1 error')).toBeInTheDocument();
  45. // Error reason rendered inline.
  46. expect(screen.getByText('material is required')).toBeInTheDocument();
  47. // Import button reflects the valid count.
  48. expect(screen.getByText('Import 2 valid rows')).toBeInTheDocument();
  49. });
  50. it('shows the id each spool keeps, and a dash for a new one (#3520)', async () => {
  51. vi.mocked(api.importSpoolsCsvPreview).mockResolvedValue({
  52. ...preview,
  53. rows: [
  54. { ...preview.rows[0], keeps_id: 417 },
  55. { ...preview.rows[2], keeps_id: null },
  56. ],
  57. });
  58. render(<SpoolCsvImportModal onClose={vi.fn()} onImported={vi.fn()} />);
  59. selectFile();
  60. expect(await screen.findByText('417')).toBeInTheDocument();
  61. const rows = screen.getAllByRole('row');
  62. // Header, then one row per CSV row; the id cell follows the row number.
  63. expect(rows[1].querySelectorAll('td')[1].textContent).toBe('417');
  64. expect(rows[2].querySelectorAll('td')[1].textContent).toBe('—');
  65. });
  66. it('imports only when there are valid rows and reports the created count', async () => {
  67. vi.mocked(api.importSpoolsCsvPreview).mockResolvedValue(preview);
  68. vi.mocked(api.importSpoolsCsv).mockResolvedValue({ created: 2, skipped: 0, errors: 1, error_rows: [] });
  69. const onImported = vi.fn();
  70. render(<SpoolCsvImportModal onClose={vi.fn()} onImported={onImported} />);
  71. selectFile();
  72. const importBtn = await screen.findByText('Import 2 valid rows');
  73. fireEvent.click(importBtn);
  74. await waitFor(() => expect(api.importSpoolsCsv).toHaveBeenCalledOnce());
  75. expect(onImported).toHaveBeenCalledWith(2);
  76. });
  77. it('disables import when no rows are valid', async () => {
  78. vi.mocked(api.importSpoolsCsvPreview).mockResolvedValue({
  79. ...preview,
  80. valid_count: 0,
  81. error_count: 1,
  82. rows: [preview.rows[1]],
  83. });
  84. render(<SpoolCsvImportModal onClose={vi.fn()} onImported={vi.fn()} />);
  85. selectFile();
  86. const noValid = await screen.findByText('No valid rows');
  87. expect(noValid.closest('button')).toBeDisabled();
  88. });
  89. });