EditArchiveModal.test.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Tests for the EditArchiveModal component.
  3. */
  4. import { describe, it, expect, vi, beforeEach } from 'vitest';
  5. import { screen, waitFor } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { render } from '../utils';
  8. import { EditArchiveModal } from '../../components/EditArchiveModal';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. const mockArchive = {
  12. id: 1,
  13. filename: 'benchy.gcode.3mf',
  14. print_name: 'Benchy',
  15. printer_id: 1,
  16. printer_name: 'X1 Carbon',
  17. notes: 'Test notes',
  18. rating: 4,
  19. project_id: null,
  20. tags: 'test,calibration',
  21. };
  22. const mockProjects = [
  23. { id: 1, name: 'Functional Parts', color: '#00ae42' },
  24. { id: 2, name: 'Art', color: '#ff5500' },
  25. ];
  26. describe('EditArchiveModal', () => {
  27. const mockOnClose = vi.fn();
  28. const mockOnSave = vi.fn();
  29. beforeEach(() => {
  30. vi.clearAllMocks();
  31. server.use(
  32. http.get('/api/v1/projects/', () => {
  33. return HttpResponse.json(mockProjects);
  34. }),
  35. http.get('/api/v1/archives/tags', () => {
  36. return HttpResponse.json([
  37. { name: 'test', count: 2 },
  38. { name: 'calibration', count: 1 },
  39. { name: 'functional', count: 3 },
  40. ]);
  41. }),
  42. http.patch('/api/v1/archives/:id', async ({ request }) => {
  43. const body = await request.json();
  44. return HttpResponse.json({ ...mockArchive, ...body });
  45. })
  46. );
  47. });
  48. describe('rendering', () => {
  49. it('renders the modal title', () => {
  50. render(
  51. <EditArchiveModal
  52. archive={mockArchive}
  53. onClose={mockOnClose}
  54. onSave={mockOnSave}
  55. />
  56. );
  57. expect(screen.getByText(/edit/i)).toBeInTheDocument();
  58. });
  59. it('shows print name field', async () => {
  60. render(
  61. <EditArchiveModal
  62. archive={mockArchive}
  63. onClose={mockOnClose}
  64. onSave={mockOnSave}
  65. />
  66. );
  67. await waitFor(() => {
  68. // Name field should be present
  69. const nameInput = screen.getByDisplayValue('Benchy');
  70. expect(nameInput).toBeInTheDocument();
  71. });
  72. });
  73. it('shows notes field', async () => {
  74. render(
  75. <EditArchiveModal
  76. archive={mockArchive}
  77. onClose={mockOnClose}
  78. onSave={mockOnSave}
  79. />
  80. );
  81. await waitFor(() => {
  82. const notesField = screen.getByDisplayValue('Test notes');
  83. expect(notesField).toBeInTheDocument();
  84. });
  85. });
  86. it('shows rating selector', async () => {
  87. render(
  88. <EditArchiveModal
  89. archive={mockArchive}
  90. onClose={mockOnClose}
  91. onSave={mockOnSave}
  92. />
  93. );
  94. await waitFor(() => {
  95. // Rating may be shown as stars or dropdown
  96. expect(screen.getByText(/edit/i)).toBeInTheDocument();
  97. });
  98. });
  99. it('shows project selector', async () => {
  100. render(
  101. <EditArchiveModal
  102. archive={mockArchive}
  103. onClose={mockOnClose}
  104. onSave={mockOnSave}
  105. />
  106. );
  107. await waitFor(() => {
  108. // Project section should be present
  109. expect(screen.getByText(/edit/i)).toBeInTheDocument();
  110. });
  111. });
  112. it('shows tags input', () => {
  113. render(
  114. <EditArchiveModal
  115. archive={mockArchive}
  116. onClose={mockOnClose}
  117. onSave={mockOnSave}
  118. />
  119. );
  120. expect(screen.getByText(/tags/i)).toBeInTheDocument();
  121. });
  122. });
  123. describe('existing values', () => {
  124. it('shows existing tags', () => {
  125. render(
  126. <EditArchiveModal
  127. archive={mockArchive}
  128. onClose={mockOnClose}
  129. onSave={mockOnSave}
  130. />
  131. );
  132. expect(screen.getByText('test')).toBeInTheDocument();
  133. expect(screen.getByText('calibration')).toBeInTheDocument();
  134. });
  135. });
  136. describe('actions', () => {
  137. it('has save button', () => {
  138. render(
  139. <EditArchiveModal
  140. archive={mockArchive}
  141. onClose={mockOnClose}
  142. onSave={mockOnSave}
  143. />
  144. );
  145. expect(screen.getByRole('button', { name: /save/i })).toBeInTheDocument();
  146. });
  147. it('has cancel button', () => {
  148. render(
  149. <EditArchiveModal
  150. archive={mockArchive}
  151. onClose={mockOnClose}
  152. onSave={mockOnSave}
  153. />
  154. );
  155. expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument();
  156. });
  157. it('calls onClose when cancel is clicked', async () => {
  158. const user = userEvent.setup();
  159. render(
  160. <EditArchiveModal
  161. archive={mockArchive}
  162. onClose={mockOnClose}
  163. onSave={mockOnSave}
  164. />
  165. );
  166. await user.click(screen.getByRole('button', { name: /cancel/i }));
  167. expect(mockOnClose).toHaveBeenCalled();
  168. });
  169. it('can edit print name', async () => {
  170. const user = userEvent.setup();
  171. render(
  172. <EditArchiveModal
  173. archive={mockArchive}
  174. onClose={mockOnClose}
  175. onSave={mockOnSave}
  176. />
  177. );
  178. const nameInput = screen.getByDisplayValue('Benchy');
  179. await user.clear(nameInput);
  180. await user.type(nameInput, 'New Name');
  181. expect(nameInput).toHaveValue('New Name');
  182. });
  183. });
  184. describe('failure_reason vocabulary (#1687 follow-up)', () => {
  185. // The Stats page's Failure Analysis widget groups by the raw column value.
  186. // Before this fix this modal saved the translated label, so a language
  187. // switch fragmented historical buckets and any round-trip through the
  188. // new PATCH /print-log endpoint (which validates against camelCase keys)
  189. // would reject the value. The dropdown now saves the key.
  190. const failedArchive = { ...mockArchive, status: 'failed', failure_reason: 'filamentRunout' };
  191. const legacyArchive = { ...mockArchive, status: 'failed', failure_reason: 'Filament runout' };
  192. it('preselects the option when the stored value is already a camelCase key', () => {
  193. render(<EditArchiveModal archive={failedArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  194. const select = screen.getByLabelText(/failure reason/i) as HTMLSelectElement;
  195. expect(select.value).toBe('filamentRunout');
  196. });
  197. it('reverse-looks-up a legacy translated value back to its key', () => {
  198. render(<EditArchiveModal archive={legacyArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  199. const select = screen.getByLabelText(/failure reason/i) as HTMLSelectElement;
  200. expect(select.value).toBe('filamentRunout');
  201. });
  202. it('sends the camelCase key on save, not the translated label', async () => {
  203. const user = userEvent.setup();
  204. let patched: { failure_reason?: string } | undefined;
  205. server.use(
  206. http.patch('/api/v1/archives/:id', async ({ request }) => {
  207. patched = (await request.json()) as { failure_reason?: string };
  208. return HttpResponse.json({ ...failedArchive, ...patched });
  209. }),
  210. );
  211. render(<EditArchiveModal archive={failedArchive} onClose={mockOnClose} onSave={mockOnSave} />);
  212. const select = screen.getByLabelText(/failure reason/i);
  213. await user.selectOptions(select, 'cloggedNozzle');
  214. await user.click(screen.getByRole('button', { name: /save/i }));
  215. await waitFor(() => {
  216. expect(patched?.failure_reason).toBe('cloggedNozzle');
  217. });
  218. });
  219. });
  220. });