PrintersPageExternalSpoolToggle.test.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Hiding the external spool from the printer card (#1782, reporter @Arn0uDz).
  3. *
  4. * The toggle lives in the filament section header next to the AMS Backup
  5. * badge. It is offered only when an AMS is present: on a printer that feeds
  6. * from the external spool alone, the external spool IS the filament section,
  7. * so hiding it would leave an empty row and no way to see the loaded filament.
  8. */
  9. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  10. import { screen, waitFor } from '@testing-library/react';
  11. import userEvent from '@testing-library/user-event';
  12. import { render } from '../utils';
  13. import { PrintersPage } from '../../pages/PrintersPage';
  14. import { http, HttpResponse } from 'msw';
  15. import { server } from '../mocks/server';
  16. const STORE_KEY = 'printerHiddenExternalSpools';
  17. const mockPrinter = {
  18. id: 1,
  19. name: 'X1C',
  20. ip_address: '192.168.1.100',
  21. serial_number: '01P00A000000001',
  22. access_code: '12345678',
  23. model: 'X1C',
  24. enabled: true,
  25. nozzle_diameter: 0.4,
  26. nozzle_type: 'stainless_steel',
  27. location: 'Workshop',
  28. auto_archive: true,
  29. created_at: '2024-01-01T00:00:00Z',
  30. updated_at: '2024-01-01T00:00:00Z',
  31. };
  32. const baseTray = {
  33. tray_color: 'FF0000FF',
  34. tray_type: 'PLA',
  35. tray_sub_brands: 'PLA Basic',
  36. tray_id_name: 'A00-R0',
  37. tray_info_idx: 'GFA00',
  38. remain: 80,
  39. k: 0.02,
  40. cali_idx: null,
  41. tag_uid: null,
  42. tray_uuid: null,
  43. nozzle_temp_min: 190,
  44. nozzle_temp_max: 230,
  45. drying_temp: null,
  46. drying_time: null,
  47. state: 3,
  48. };
  49. const amsUnit = {
  50. id: 0,
  51. humidity: 30,
  52. temp: 33,
  53. is_ams_ht: false,
  54. serial_number: 'AMS00',
  55. sw_ver: '03.00.21.29',
  56. dry_time: 0,
  57. dry_status: 0,
  58. dry_sub_status: 0,
  59. dry_sf_reason: [],
  60. module_type: 'n3f',
  61. tray: [0, 1, 2, 3].map((id) => ({ id, ...baseTray })),
  62. };
  63. function makeStatus({ withAms }: { withAms: boolean }) {
  64. return {
  65. connected: true,
  66. state: 'IDLE',
  67. progress: 0,
  68. layer_num: 0,
  69. total_layers: 0,
  70. temperatures: { nozzle: 25, bed: 25, chamber: 25 },
  71. remaining_time: 0,
  72. filename: null,
  73. wifi_signal: -29,
  74. speed_level: 2,
  75. supports_drying: true,
  76. drying_screen_only: false,
  77. ams: withAms ? [amsUnit] : [],
  78. vt_tray: [{ id: 254, ...baseTray, tray_type: 'PETG', tray_sub_brands: 'PETG HF' }],
  79. };
  80. }
  81. const WITH_AMS = makeStatus({ withAms: true });
  82. const WITHOUT_AMS = makeStatus({ withAms: false });
  83. const HIDE_TITLE = 'Hide external spool';
  84. const SHOW_TITLE = 'Show external spool';
  85. /** The external spool's own card is labelled with `printers.external`. */
  86. function externalSpoolCards() {
  87. return screen.queryAllByText('External');
  88. }
  89. let store: Record<string, string>;
  90. describe('PrintersPage — hide the external spool (#1782)', () => {
  91. beforeEach(() => {
  92. store = {};
  93. vi.mocked(localStorage.getItem).mockImplementation((key: string) => store[key] ?? null);
  94. vi.mocked(localStorage.setItem).mockImplementation((key: string, value: string) => {
  95. store[key] = String(value);
  96. });
  97. server.use(
  98. http.get('/api/v1/printers/', () => HttpResponse.json([mockPrinter])),
  99. http.get('/api/v1/queue/', () => HttpResponse.json([])),
  100. );
  101. });
  102. afterEach(() => {
  103. vi.mocked(localStorage.getItem).mockReset();
  104. vi.mocked(localStorage.setItem).mockReset();
  105. });
  106. it('hides the external spool when the toggle is clicked, and brings it back', async () => {
  107. const user = userEvent.setup();
  108. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(WITH_AMS)));
  109. render(<PrintersPage />);
  110. const toggle = await screen.findByTitle(HIDE_TITLE);
  111. expect(externalSpoolCards().length).toBeGreaterThan(0);
  112. await user.click(toggle);
  113. await waitFor(() => expect(externalSpoolCards()).toHaveLength(0));
  114. // The toggle itself stays put — it is the only way back.
  115. const restore = await screen.findByTitle(SHOW_TITLE);
  116. await user.click(restore);
  117. await waitFor(() => expect(externalSpoolCards().length).toBeGreaterThan(0));
  118. });
  119. it('persists the choice per printer', async () => {
  120. const user = userEvent.setup();
  121. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(WITH_AMS)));
  122. render(<PrintersPage />);
  123. await user.click(await screen.findByTitle(HIDE_TITLE));
  124. // Keyed by printer id, so a second printer's card is untouched.
  125. await waitFor(() => {
  126. expect(JSON.parse(store[STORE_KEY])).toEqual({ '1': true });
  127. });
  128. });
  129. it('starts hidden when the stored preference says so', async () => {
  130. store[STORE_KEY] = JSON.stringify({ '1': true });
  131. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(WITH_AMS)));
  132. render(<PrintersPage />);
  133. await screen.findByTitle(SHOW_TITLE);
  134. expect(externalSpoolCards()).toHaveLength(0);
  135. });
  136. it('does not offer the toggle on a printer with no AMS', async () => {
  137. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(WITHOUT_AMS)));
  138. render(<PrintersPage />);
  139. // The external spool is the whole filament section here, so it must stay.
  140. await waitFor(() => expect(externalSpoolCards().length).toBeGreaterThan(0));
  141. expect(screen.queryByTitle(HIDE_TITLE)).not.toBeInTheDocument();
  142. expect(screen.queryByTitle(SHOW_TITLE)).not.toBeInTheDocument();
  143. });
  144. it('ignores a stored preference once the printer has no AMS left', async () => {
  145. // The AMS was unplugged after the user hid the external spool. Honouring
  146. // the stored flag would blank the filament row with no control to undo it.
  147. store[STORE_KEY] = JSON.stringify({ '1': true });
  148. server.use(http.get('/api/v1/printers/:id/status', () => HttpResponse.json(WITHOUT_AMS)));
  149. render(<PrintersPage />);
  150. await waitFor(() => expect(externalSpoolCards().length).toBeGreaterThan(0));
  151. expect(screen.queryByTitle(SHOW_TITLE)).not.toBeInTheDocument();
  152. });
  153. });