MaintenancePage.test.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Tests for the MaintenancePage component.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { screen, waitFor } from '@testing-library/react';
  6. import { render } from '../utils';
  7. import { MaintenancePage } from '../../pages/MaintenancePage';
  8. import { http, HttpResponse } from 'msw';
  9. import { server } from '../mocks/server';
  10. const mockPrinters = [
  11. {
  12. id: 1,
  13. name: 'X1 Carbon',
  14. model: 'X1C',
  15. serial_number: '00M09A350100001',
  16. },
  17. ];
  18. const mockMaintenanceTypes = [
  19. {
  20. id: 1,
  21. name: 'Clean Nozzle',
  22. description: 'Clean the printer nozzle',
  23. default_interval_hours: 50,
  24. applies_to_models: ['X1C', 'P1S'],
  25. },
  26. {
  27. id: 2,
  28. name: 'Lubricate Rods',
  29. description: 'Lubricate linear rods',
  30. default_interval_hours: 200,
  31. applies_to_models: ['X1C', 'P1S'],
  32. },
  33. ];
  34. const mockMaintenanceTasks = [
  35. {
  36. id: 1,
  37. printer_id: 1,
  38. maintenance_type_id: 1,
  39. maintenance_type_name: 'Clean Nozzle',
  40. interval_hours: 50,
  41. last_completed_at: '2024-01-01T00:00:00Z',
  42. next_due_at: '2024-01-03T00:00:00Z',
  43. hours_until_due: 10,
  44. is_due: false,
  45. notes: null,
  46. },
  47. {
  48. id: 2,
  49. printer_id: 1,
  50. maintenance_type_id: 2,
  51. maintenance_type_name: 'Lubricate Rods',
  52. interval_hours: 200,
  53. last_completed_at: '2023-12-01T00:00:00Z',
  54. next_due_at: '2023-12-15T00:00:00Z',
  55. hours_until_due: -100,
  56. is_due: true,
  57. notes: 'Use PTFE lubricant',
  58. },
  59. ];
  60. describe('MaintenancePage', () => {
  61. beforeEach(() => {
  62. server.use(
  63. http.get('/api/v1/printers/', () => {
  64. return HttpResponse.json(mockPrinters);
  65. }),
  66. http.get('/api/v1/maintenance/types', () => {
  67. return HttpResponse.json(mockMaintenanceTypes);
  68. }),
  69. http.get('/api/v1/maintenance/', () => {
  70. return HttpResponse.json(mockMaintenanceTasks);
  71. }),
  72. http.get('/api/v1/maintenance/overview', () => {
  73. // Overview is an array of printer summaries
  74. return HttpResponse.json([
  75. {
  76. printer_id: 1,
  77. printer_name: 'X1 Carbon',
  78. due_count: 1,
  79. warning_count: 0,
  80. total_print_hours: 100,
  81. maintenance_items: [
  82. {
  83. id: 1,
  84. maintenance_type_id: 1,
  85. maintenance_type_name: 'Clean Nozzle',
  86. interval_hours: 50,
  87. hours_since_last: 45,
  88. hours_until_due: 5,
  89. is_due: false,
  90. is_warning: false,
  91. },
  92. {
  93. id: 2,
  94. maintenance_type_id: 2,
  95. maintenance_type_name: 'Lubricate Rods',
  96. interval_hours: 200,
  97. hours_since_last: 250,
  98. hours_until_due: -50,
  99. is_due: true,
  100. is_warning: false,
  101. },
  102. ],
  103. },
  104. ]);
  105. }),
  106. http.post('/api/v1/maintenance/', async ({ request }) => {
  107. const body = await request.json() as { name: string };
  108. return HttpResponse.json({ id: 3, ...body });
  109. }),
  110. http.post('/api/v1/maintenance/:id/complete', () => {
  111. return HttpResponse.json({ success: true });
  112. })
  113. );
  114. });
  115. describe('rendering', () => {
  116. it('renders the page title', async () => {
  117. render(<MaintenancePage />);
  118. await waitFor(() => {
  119. expect(screen.getByText('Maintenance')).toBeInTheDocument();
  120. });
  121. });
  122. it('renders maintenance page content', async () => {
  123. render(<MaintenancePage />);
  124. await waitFor(() => {
  125. // Page should render with printer tabs or tasks
  126. expect(screen.getByText('Maintenance')).toBeInTheDocument();
  127. });
  128. });
  129. });
  130. describe('printer tabs', () => {
  131. it('shows printer tabs when printers exist', async () => {
  132. render(<MaintenancePage />);
  133. await waitFor(() => {
  134. // Should show printer name in tabs
  135. expect(screen.getByText('X1 Carbon')).toBeInTheDocument();
  136. });
  137. });
  138. });
  139. });