ConfigureAmsSlotModal.test.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * Tests for the ConfigureAmsSlotModal component.
  3. */
  4. import { describe, it, expect, vi, beforeEach } from 'vitest';
  5. import { screen, fireEvent, waitFor } from '@testing-library/react';
  6. import { render } from '../utils';
  7. import { ConfigureAmsSlotModal } from '../../components/ConfigureAmsSlotModal';
  8. import { api } from '../../api/client';
  9. // Mock the API client
  10. vi.mock('../../api/client', () => ({
  11. api: {
  12. getCloudSettings: vi.fn(),
  13. getKProfiles: vi.fn(),
  14. configureAmsSlot: vi.fn(),
  15. getCloudSettingDetail: vi.fn(),
  16. saveSlotPreset: vi.fn(),
  17. getSettings: vi.fn().mockResolvedValue({}),
  18. updateSettings: vi.fn().mockResolvedValue({}),
  19. getLocalPresets: vi.fn(),
  20. getBuiltinFilaments: vi.fn(),
  21. searchColors: vi.fn(),
  22. getColorCatalog: vi.fn(),
  23. resetAmsSlot: vi.fn(),
  24. },
  25. }));
  26. const mockCloudSettings = {
  27. filament: [
  28. {
  29. setting_id: 'GFSL05_09',
  30. name: 'Bambu PLA Basic @BBL X1C',
  31. filament_id: 'GFL05',
  32. },
  33. {
  34. setting_id: 'PFUScd84f663d2c2ef',
  35. name: '# Overture Matte PLA @BBL H2D',
  36. filament_id: null,
  37. },
  38. ],
  39. };
  40. const mockKProfiles = {
  41. profiles: [
  42. {
  43. id: 1,
  44. name: 'PLA Basic',
  45. k_value: '0.020',
  46. filament_id: 'GFL05',
  47. setting_id: '',
  48. extruder_id: 1,
  49. cali_idx: 1,
  50. },
  51. ],
  52. };
  53. const defaultProps = {
  54. isOpen: true,
  55. onClose: vi.fn(),
  56. printerId: 1,
  57. slotInfo: {
  58. amsId: 0,
  59. trayId: 0,
  60. trayCount: 4,
  61. trayType: 'PLA',
  62. trayColor: 'FFFFFF',
  63. traySubBrands: 'PLA Basic',
  64. },
  65. nozzleDiameter: '0.4',
  66. onSuccess: vi.fn(),
  67. };
  68. describe('ConfigureAmsSlotModal', () => {
  69. beforeEach(() => {
  70. vi.clearAllMocks();
  71. // Mock scrollIntoView which is not available in jsdom
  72. Element.prototype.scrollIntoView = vi.fn();
  73. (api.getCloudSettings as ReturnType<typeof vi.fn>).mockResolvedValue(mockCloudSettings);
  74. (api.getKProfiles as ReturnType<typeof vi.fn>).mockResolvedValue(mockKProfiles);
  75. (api.configureAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  76. (api.saveSlotPreset as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true });
  77. (api.getLocalPresets as ReturnType<typeof vi.fn>).mockResolvedValue({ filament: [] });
  78. (api.getBuiltinFilaments as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  79. (api.searchColors as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  80. (api.getColorCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
  81. (api.resetAmsSlot as ReturnType<typeof vi.fn>).mockResolvedValue({ success: true, message: 'ok' });
  82. });
  83. it('renders nothing visible when closed', () => {
  84. render(<ConfigureAmsSlotModal {...defaultProps} isOpen={false} />);
  85. expect(screen.queryByText('Configure AMS Slot')).not.toBeInTheDocument();
  86. });
  87. it('renders modal when open', async () => {
  88. render(<ConfigureAmsSlotModal {...defaultProps} />);
  89. await waitFor(() => {
  90. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  91. });
  92. });
  93. it('displays basic color buttons', async () => {
  94. render(<ConfigureAmsSlotModal {...defaultProps} />);
  95. await waitFor(() => {
  96. // Check for basic color buttons by their title attribute
  97. expect(screen.getByTitle('White')).toBeInTheDocument();
  98. expect(screen.getByTitle('Black')).toBeInTheDocument();
  99. expect(screen.getByTitle('Red')).toBeInTheDocument();
  100. expect(screen.getByTitle('Blue')).toBeInTheDocument();
  101. expect(screen.getByTitle('Green')).toBeInTheDocument();
  102. expect(screen.getByTitle('Yellow')).toBeInTheDocument();
  103. expect(screen.getByTitle('Orange')).toBeInTheDocument();
  104. expect(screen.getByTitle('Gray')).toBeInTheDocument();
  105. });
  106. });
  107. it('does not show extended colors by default', async () => {
  108. render(<ConfigureAmsSlotModal {...defaultProps} />);
  109. await waitFor(() => {
  110. expect(screen.getByTitle('White')).toBeInTheDocument();
  111. });
  112. // Extended colors should not be visible initially
  113. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  114. expect(screen.queryByTitle('Purple')).not.toBeInTheDocument();
  115. expect(screen.queryByTitle('Coral')).not.toBeInTheDocument();
  116. });
  117. it('shows extended colors when expand button is clicked', async () => {
  118. render(<ConfigureAmsSlotModal {...defaultProps} />);
  119. await waitFor(() => {
  120. expect(screen.getByTitle('White')).toBeInTheDocument();
  121. });
  122. // Click the expand button (+ button)
  123. const expandButton = screen.getByTitle('Show more colors');
  124. fireEvent.click(expandButton);
  125. // Extended colors should now be visible
  126. await waitFor(() => {
  127. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  128. expect(screen.getByTitle('Purple')).toBeInTheDocument();
  129. expect(screen.getByTitle('Pink')).toBeInTheDocument();
  130. expect(screen.getByTitle('Brown')).toBeInTheDocument();
  131. expect(screen.getByTitle('Coral')).toBeInTheDocument();
  132. });
  133. });
  134. it('hides extended colors when collapse button is clicked', async () => {
  135. render(<ConfigureAmsSlotModal {...defaultProps} />);
  136. await waitFor(() => {
  137. expect(screen.getByTitle('White')).toBeInTheDocument();
  138. });
  139. // Click the expand button
  140. const expandButton = screen.getByTitle('Show more colors');
  141. fireEvent.click(expandButton);
  142. // Wait for extended colors to appear
  143. await waitFor(() => {
  144. expect(screen.getByTitle('Cyan')).toBeInTheDocument();
  145. });
  146. // Click the collapse button
  147. const collapseButton = screen.getByTitle('Show less colors');
  148. fireEvent.click(collapseButton);
  149. // Extended colors should be hidden again
  150. await waitFor(() => {
  151. expect(screen.queryByTitle('Cyan')).not.toBeInTheDocument();
  152. });
  153. });
  154. it('selects a color when color button is clicked', async () => {
  155. render(<ConfigureAmsSlotModal {...defaultProps} />);
  156. await waitFor(() => {
  157. expect(screen.getByTitle('Red')).toBeInTheDocument();
  158. });
  159. // Click the red color button
  160. const redButton = screen.getByTitle('Red');
  161. fireEvent.click(redButton);
  162. // The color input should now show "Red"
  163. const colorInput = screen.getByPlaceholderText(/Color name or hex/);
  164. expect(colorInput).toHaveValue('Red');
  165. });
  166. it('derives tray_info_idx from base_id when filament_id is null', async () => {
  167. // Mock the detail API to return base_id but no filament_id
  168. (api.getCloudSettingDetail as ReturnType<typeof vi.fn>).mockResolvedValue({
  169. filament_id: null,
  170. base_id: 'GFSL05_09',
  171. name: '# Overture Matte PLA @BBL H2D',
  172. });
  173. render(<ConfigureAmsSlotModal {...defaultProps} />);
  174. // Wait for presets to load
  175. await waitFor(() => {
  176. expect(api.getCloudSettings).toHaveBeenCalled();
  177. });
  178. // Select a user preset (one without filament_id)
  179. // Find and click the preset - this would require the preset to be in the list
  180. // The actual tray_info_idx derivation happens during the configure mutation
  181. });
  182. it('renders configure slot button', async () => {
  183. render(<ConfigureAmsSlotModal {...defaultProps} />);
  184. await waitFor(() => {
  185. expect(screen.getByText(/Configure AMS/)).toBeInTheDocument();
  186. });
  187. // Find the Configure Slot button
  188. const configureButton = screen.getByRole('button', { name: /Configure Slot/i });
  189. expect(configureButton).toBeInTheDocument();
  190. });
  191. it('filters presets by printer model', async () => {
  192. // Render with printerModel="H2D"
  193. render(<ConfigureAmsSlotModal {...defaultProps} printerModel="H2D" />);
  194. // Wait for presets to load - the H2D preset should be visible
  195. await waitFor(() => {
  196. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  197. });
  198. // The X1C preset should NOT be visible (filtered out by model)
  199. expect(screen.queryByText(/Bambu PLA Basic @BBL X1C/)).not.toBeInTheDocument();
  200. });
  201. it('shows current preset even when it does not match model filter', async () => {
  202. // Render with printerModel="H2D" but savedPresetId pointing to the X1C preset
  203. const slotInfo = {
  204. ...defaultProps.slotInfo,
  205. savedPresetId: 'GFSL05_09', // X1C preset
  206. };
  207. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} printerModel="H2D" />);
  208. await waitFor(() => {
  209. // Both should be visible - H2D matches model, X1C is saved preset
  210. // Use the full preset name to match the list item (not the "Filtering for" label)
  211. expect(screen.getByText('Bambu PLA Basic @BBL X1C')).toBeInTheDocument();
  212. expect(screen.getByText(/Overture Matte PLA/)).toBeInTheDocument();
  213. });
  214. });
  215. it('pre-selects saved preset when opening configured slot', async () => {
  216. const slotInfo = {
  217. ...defaultProps.slotInfo,
  218. savedPresetId: 'GFSL05_09',
  219. };
  220. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  221. await waitFor(() => {
  222. // The saved preset should have the selected style (green border)
  223. // Use the full preset name to avoid matching the "Filtering for" label
  224. const presetButton = screen.getByText('Bambu PLA Basic @BBL X1C').closest('button');
  225. expect(presetButton).toHaveClass('bg-bambu-green/20');
  226. });
  227. });
  228. it('pre-populates color from trayColor', async () => {
  229. const slotInfo = {
  230. ...defaultProps.slotInfo,
  231. trayColor: 'FF0000FF', // Red with alpha
  232. };
  233. render(<ConfigureAmsSlotModal {...defaultProps} slotInfo={slotInfo} />);
  234. await waitFor(() => {
  235. expect(screen.getByTitle('White')).toBeInTheDocument();
  236. });
  237. // The hex display should show the pre-populated color
  238. expect(screen.getByText('Hex: #FF0000', { exact: false })).toBeInTheDocument();
  239. });
  240. it('uses translated text for modal elements', async () => {
  241. render(<ConfigureAmsSlotModal {...defaultProps} />);
  242. await waitFor(() => {
  243. expect(screen.getByText('Configure AMS Slot')).toBeInTheDocument();
  244. expect(screen.getByText('Filament Profile')).toBeInTheDocument();
  245. });
  246. // Check footer buttons
  247. expect(screen.getByRole('button', { name: /Configure Slot/i })).toBeInTheDocument();
  248. expect(screen.getByRole('button', { name: /Cancel/i })).toBeInTheDocument();
  249. expect(screen.getByRole('button', { name: /Reset Slot/i })).toBeInTheDocument();
  250. });
  251. });