BulkPrinterToolbarWiring.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * BulkPrinterToolbar wiring: which action the toolbar reports, which state the
  3. * select-by-state dropdown reports, and the permission gates on the destructive
  4. * buttons (printers:control for stop/pause/resume/clear HMS,
  5. * printers:clear_plate for Clear Bed).
  6. *
  7. * The toolbar reads printer statuses straight from the query cache, so each
  8. * test seeds the cache it renders with.
  9. */
  10. import { describe, it, expect, vi, beforeEach } from 'vitest';
  11. import { render, screen, waitFor } from '@testing-library/react';
  12. import userEvent from '@testing-library/user-event';
  13. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  14. import type { Printer } from '../../api/client';
  15. const permissions = { granted: [] as string[] };
  16. const mockUseAuth = {
  17. user: { id: 1, username: 'operator', permissions: [] as string[] },
  18. authEnabled: true,
  19. loading: false,
  20. isAdmin: false,
  21. login: vi.fn(),
  22. loginWithToken: vi.fn(),
  23. logout: vi.fn(),
  24. refreshAuth: vi.fn(),
  25. hasPermission: vi.fn((permission: string) => permissions.granted.includes(permission)),
  26. hasAnyPermission: vi.fn(() => true),
  27. canModify: vi.fn(() => true),
  28. };
  29. vi.mock('../../contexts/AuthContext', async (importOriginal) => {
  30. const actual = await importOriginal<typeof import('../../contexts/AuthContext')>();
  31. return { ...actual, useAuth: () => mockUseAuth };
  32. });
  33. import { BulkPrinterToolbar } from '../../components/BulkPrinterToolbar';
  34. const printer = (id: number, location: string | null = null) =>
  35. ({ id, name: `P${id}`, model: 'X1C', location }) as unknown as Printer;
  36. const printers = [printer(1, 'Lab'), printer(2, 'Lab'), printer(3, 'Shed')];
  37. // 1 printing, 2 paused (awaiting plate clear), 3 idle.
  38. function seededClient() {
  39. const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  40. qc.setQueryData(['printerStatus', 1], { connected: true, state: 'RUNNING' });
  41. qc.setQueryData(['printerStatus', 2], { connected: true, state: 'PAUSE', awaiting_plate_clear: true });
  42. qc.setQueryData(['printerStatus', 3], { connected: true, state: 'IDLE' });
  43. return qc;
  44. }
  45. function renderToolbar(selected: number[] = [1, 2]) {
  46. const props = {
  47. selectedIds: new Set(selected),
  48. printers,
  49. onClose: vi.fn(),
  50. onSelectAll: vi.fn(),
  51. onSelectByLocation: vi.fn(),
  52. onSelectByState: vi.fn(),
  53. onAction: vi.fn(),
  54. actionPending: false,
  55. };
  56. render(
  57. <QueryClientProvider client={seededClient()}>
  58. <BulkPrinterToolbar {...props} />
  59. </QueryClientProvider>,
  60. );
  61. return props;
  62. }
  63. const btn = (name: string) => screen.getByRole('button', { name });
  64. describe('BulkPrinterToolbar wiring', () => {
  65. beforeEach(() => {
  66. permissions.granted = ['printers:control', 'printers:clear_plate'];
  67. vi.clearAllMocks();
  68. });
  69. it.each([
  70. ['Stop', 'stop'],
  71. ['Pause', 'pause'],
  72. ['Resume', 'resume'],
  73. ['Clear Bed', 'clearPlate'],
  74. ])('clicking %s reports action %s', async (label, action) => {
  75. const user = userEvent.setup();
  76. const props = renderToolbar();
  77. await user.click(btn(label));
  78. expect(props.onAction).toHaveBeenCalledTimes(1);
  79. expect(props.onAction).toHaveBeenCalledWith(action);
  80. });
  81. it('disables Stop, Pause and Resume without printers:control', async () => {
  82. permissions.granted = ['printers:clear_plate'];
  83. const user = userEvent.setup();
  84. const props = renderToolbar();
  85. for (const label of ['Stop', 'Pause', 'Resume', 'Clear Notifications']) {
  86. expect(btn(label)).toBeDisabled();
  87. expect(btn(label)).toHaveAttribute('title', 'You do not have permission to control printers');
  88. await user.click(btn(label));
  89. }
  90. // Clear Bed has its own permission and stays usable.
  91. expect(btn('Clear Bed')).toBeEnabled();
  92. expect(props.onAction).not.toHaveBeenCalled();
  93. });
  94. it('disables Clear Bed without printers:clear_plate even with control', async () => {
  95. permissions.granted = ['printers:control'];
  96. const user = userEvent.setup();
  97. const props = renderToolbar();
  98. expect(btn('Clear Bed')).toBeDisabled();
  99. await user.click(btn('Clear Bed'));
  100. expect(props.onAction).not.toHaveBeenCalled();
  101. expect(btn('Stop')).toBeEnabled();
  102. });
  103. it('disables actions none of the selected printers can take', () => {
  104. // Only the idle printer is selected: nothing to stop, pause, resume or clear.
  105. renderToolbar([3]);
  106. for (const label of ['Stop', 'Pause', 'Resume', 'Clear Bed']) {
  107. expect(btn(label)).toBeDisabled();
  108. }
  109. });
  110. it('disables every action while one is pending', () => {
  111. const props = {
  112. selectedIds: new Set([1, 2]),
  113. printers,
  114. onClose: vi.fn(),
  115. onSelectAll: vi.fn(),
  116. onSelectByLocation: vi.fn(),
  117. onSelectByState: vi.fn(),
  118. onAction: vi.fn(),
  119. actionPending: true,
  120. };
  121. render(
  122. <QueryClientProvider client={seededClient()}>
  123. <BulkPrinterToolbar {...props} />
  124. </QueryClientProvider>,
  125. );
  126. for (const label of ['Stop', 'Pause', 'Resume', 'Clear Bed']) {
  127. expect(btn(label)).toBeDisabled();
  128. }
  129. });
  130. it('select by state reports the picked state', async () => {
  131. const user = userEvent.setup();
  132. const props = renderToolbar();
  133. await user.click(btn('Select by State'));
  134. // Only states with printers in them are offered.
  135. expect(screen.queryByRole('button', { name: /Offline/ })).toBeNull();
  136. await user.click(screen.getByRole('button', { name: /Paused/ }));
  137. expect(props.onSelectByState).toHaveBeenCalledTimes(1);
  138. expect(props.onSelectByState).toHaveBeenCalledWith('paused');
  139. // The dropdown closes after a pick.
  140. await waitFor(() => expect(screen.queryByRole('button', { name: /Paused/ })).toBeNull());
  141. });
  142. it('select by location reports the picked location', async () => {
  143. const user = userEvent.setup();
  144. const props = renderToolbar();
  145. await user.click(btn('Select by Location'));
  146. await user.click(screen.getByRole('button', { name: 'Shed' }));
  147. expect(props.onSelectByLocation).toHaveBeenCalledWith('Shed');
  148. expect(props.onSelectByState).not.toHaveBeenCalled();
  149. });
  150. });