AddPrinterPreflight.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Tests for the Add-Printer setup-time pre-flight.
  3. *
  4. * On save, the modal runs the connection diagnostic; if any check fails it
  5. * warns (rather than blocks) before the printer is added.
  6. */
  7. import { describe, it, expect, beforeEach } from 'vitest';
  8. import { screen, waitFor } from '@testing-library/react';
  9. import userEvent from '@testing-library/user-event';
  10. import { render } from '../utils';
  11. import { PrintersPage } from '../../pages/PrintersPage';
  12. import { http, HttpResponse } from 'msw';
  13. import { server } from '../mocks/server';
  14. describe('AddPrinterModal pre-flight', () => {
  15. beforeEach(() => {
  16. server.use(
  17. http.get('/api/v1/printers/', () => HttpResponse.json([])),
  18. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  19. http.get('/api/v1/discovery/info', () =>
  20. HttpResponse.json({ is_docker: false, ssdp_running: false, scan_running: false, subnets: [] }),
  21. ),
  22. );
  23. });
  24. it('warns instead of saving when a connection check fails', async () => {
  25. const user = userEvent.setup();
  26. server.use(
  27. http.post('/api/v1/printers/diagnostic', () =>
  28. HttpResponse.json({
  29. printer_id: null,
  30. ip_address: '192.168.1.55',
  31. overall: 'problems',
  32. checks: [{ id: 'developer_mode', status: 'fail', params: {} }],
  33. }),
  34. ),
  35. );
  36. render(<PrintersPage />);
  37. await user.click(await screen.findByText(/add printer/i));
  38. await user.type(await screen.findByPlaceholderText('My Printer'), 'Test Printer');
  39. await user.type(screen.getByPlaceholderText('192.168.1.100 or printer.local'), '192.168.1.55');
  40. await user.type(screen.getByPlaceholderText('01P00A000000000'), '01P00A000000000');
  41. await user.type(screen.getByPlaceholderText('From printer settings'), '12345678');
  42. const submit = screen
  43. .getAllByRole('button', { name: /add printer/i })
  44. .find((b) => b.getAttribute('type') === 'submit')!;
  45. await user.click(submit);
  46. // The failed check surfaces a warning with a "save anyway" escape hatch.
  47. expect(await screen.findByText(/Some connection checks failed/i)).toBeInTheDocument();
  48. expect(screen.getByRole('button', { name: /save anyway/i })).toBeInTheDocument();
  49. expect(screen.getByText(/LAN Developer Mode/i)).toBeInTheDocument();
  50. });
  51. it('saves directly when all connection checks pass', async () => {
  52. const user = userEvent.setup();
  53. let created = false;
  54. server.use(
  55. http.post('/api/v1/printers/diagnostic', () =>
  56. HttpResponse.json({
  57. printer_id: null,
  58. ip_address: '192.168.1.55',
  59. overall: 'ok',
  60. checks: [{ id: 'developer_mode', status: 'pass', params: {} }],
  61. }),
  62. ),
  63. http.post('/api/v1/printers/', async () => {
  64. created = true;
  65. return HttpResponse.json({ id: 9, name: 'Test Printer' });
  66. }),
  67. );
  68. render(<PrintersPage />);
  69. await user.click(await screen.findByText(/add printer/i));
  70. await user.type(await screen.findByPlaceholderText('My Printer'), 'Test Printer');
  71. await user.type(screen.getByPlaceholderText('192.168.1.100 or printer.local'), '192.168.1.55');
  72. await user.type(screen.getByPlaceholderText('01P00A000000000'), '01P00A000000000');
  73. await user.type(screen.getByPlaceholderText('From printer settings'), '12345678');
  74. const submit = screen
  75. .getAllByRole('button', { name: /add printer/i })
  76. .find((b) => b.getAttribute('type') === 'submit')!;
  77. await user.click(submit);
  78. await waitFor(() => expect(created).toBe(true));
  79. expect(screen.queryByText(/Some connection checks failed/i)).not.toBeInTheDocument();
  80. });
  81. });