VirtualPrinterDiagnosticModal.test.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Tests for the VirtualPrinterDiagnosticModal component.
  3. */
  4. import { describe, it, expect, vi } from 'vitest';
  5. import { screen, waitFor } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { http, HttpResponse } from 'msw';
  8. import { server } from '../mocks/server';
  9. import { render } from '../utils';
  10. import { VirtualPrinterDiagnosticModal } from '../../components/VirtualPrinterDiagnosticModal';
  11. import type { VPDiagnosticResult } from '../../api/client';
  12. const problemResult: VPDiagnosticResult = {
  13. vp_id: 3,
  14. vp_name: 'Garage VP',
  15. mode: 'archive',
  16. overall: 'problems',
  17. checks: [
  18. { id: 'enabled', status: 'pass', params: {} },
  19. { id: 'running', status: 'fail', params: {} },
  20. { id: 'port_mqtt', status: 'fail', params: { port: 8883 } },
  21. ],
  22. };
  23. /** Stub the diagnostic endpoint and count how often it is hit. */
  24. function setupDiagnostic(result: VPDiagnosticResult): { calls: () => number } {
  25. let count = 0;
  26. server.use(
  27. http.get('*/virtual-printers/:id/diagnostic', () => {
  28. count += 1;
  29. return HttpResponse.json(result);
  30. }),
  31. );
  32. return { calls: () => count };
  33. }
  34. describe('VirtualPrinterDiagnosticModal', () => {
  35. it('runs the diagnostic on mount and renders the checks', async () => {
  36. const probe = setupDiagnostic(problemResult);
  37. render(<VirtualPrinterDiagnosticModal vpId={3} vpName="Garage VP" onClose={() => {}} />);
  38. expect(await screen.findByText(/Found problems that explain/)).toBeInTheDocument();
  39. expect(probe.calls()).toBe(1);
  40. // Per-check titles render; the port param is interpolated into the title.
  41. expect(screen.getByText('Services running')).toBeInTheDocument();
  42. expect(screen.getByText('Control service (port 8883)')).toBeInTheDocument();
  43. });
  44. it('re-runs the diagnostic when "Run again" is clicked', async () => {
  45. const probe = setupDiagnostic(problemResult);
  46. const user = userEvent.setup();
  47. render(<VirtualPrinterDiagnosticModal vpId={3} vpName="Garage VP" onClose={() => {}} />);
  48. await screen.findByText(/Found problems that explain/);
  49. expect(probe.calls()).toBe(1);
  50. await user.click(screen.getByText('Run again'));
  51. await waitFor(() => expect(probe.calls()).toBe(2));
  52. });
  53. it('calls onClose when the Close button is clicked', async () => {
  54. setupDiagnostic({ ...problemResult, overall: 'ok' });
  55. const onClose = vi.fn();
  56. const user = userEvent.setup();
  57. render(<VirtualPrinterDiagnosticModal vpId={3} vpName="Garage VP" onClose={onClose} />);
  58. await screen.findByText(/set up correctly/);
  59. await user.click(screen.getByText('Close'));
  60. expect(onClose).toHaveBeenCalled();
  61. });
  62. });