SettingsPage.test.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Tests for the SettingsPage component.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { screen, waitFor } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import { render } from '../utils';
  8. import { SettingsPage } from '../../pages/SettingsPage';
  9. import { http, HttpResponse } from 'msw';
  10. import { server } from '../mocks/server';
  11. const mockSettings = {
  12. auto_archive: true,
  13. save_thumbnails: true,
  14. capture_finish_photo: true,
  15. default_filament_cost: 25.0,
  16. currency: 'USD',
  17. ams_humidity_good: 40,
  18. ams_humidity_fair: 60,
  19. ams_temp_good: 30,
  20. ams_temp_fair: 35,
  21. time_format: 'system',
  22. date_format: 'system',
  23. mqtt_enabled: false,
  24. mqtt_host: '',
  25. mqtt_port: 1883,
  26. spoolman_enabled: false,
  27. spoolman_url: '',
  28. ha_enabled: false,
  29. ha_url: '',
  30. ha_token: '',
  31. check_updates: false,
  32. check_printer_firmware: false,
  33. };
  34. describe('SettingsPage', () => {
  35. beforeEach(() => {
  36. server.use(
  37. http.get('/api/v1/settings/', () => {
  38. return HttpResponse.json(mockSettings);
  39. }),
  40. http.patch('/api/v1/settings/', async ({ request }) => {
  41. const body = await request.json();
  42. return HttpResponse.json({ ...mockSettings, ...body });
  43. }),
  44. http.get('/api/v1/printers/', () => {
  45. return HttpResponse.json([]);
  46. }),
  47. http.get('/api/v1/smart-plugs/', () => {
  48. return HttpResponse.json([]);
  49. }),
  50. http.get('/api/v1/notifications/', () => {
  51. return HttpResponse.json([]);
  52. }),
  53. http.get('/api/v1/api-keys/', () => {
  54. return HttpResponse.json([]);
  55. }),
  56. http.get('/api/v1/mqtt/status', () => {
  57. return HttpResponse.json({ enabled: false });
  58. }),
  59. http.get('/api/v1/virtual-printer/status', () => {
  60. return HttpResponse.json({ running: false });
  61. }),
  62. http.get('/api/v1/auth/status', () => {
  63. return HttpResponse.json({ auth_enabled: false, requires_setup: false });
  64. })
  65. );
  66. });
  67. describe('rendering', () => {
  68. it('renders the page title', async () => {
  69. render(<SettingsPage />);
  70. await waitFor(() => {
  71. // Use role-based query to avoid conflicts with dropdown options
  72. expect(screen.getByRole('heading', { name: 'Settings' })).toBeInTheDocument();
  73. });
  74. });
  75. it('shows settings tabs', async () => {
  76. render(<SettingsPage />);
  77. await waitFor(() => {
  78. // Use getAllByText since "General" appears both as tab and section heading
  79. expect(screen.getAllByText('General').length).toBeGreaterThan(0);
  80. expect(screen.getByText('Smart Plugs')).toBeInTheDocument();
  81. expect(screen.getByText('Notifications')).toBeInTheDocument();
  82. expect(screen.getAllByText('Filament').length).toBeGreaterThan(0);
  83. expect(screen.getByText('Network')).toBeInTheDocument();
  84. expect(screen.getByText('API Keys')).toBeInTheDocument();
  85. });
  86. });
  87. });
  88. describe('general settings', () => {
  89. it('shows date format setting', async () => {
  90. render(<SettingsPage />);
  91. await waitFor(() => {
  92. expect(screen.getByText('Date Format')).toBeInTheDocument();
  93. });
  94. });
  95. it('shows time format setting', async () => {
  96. render(<SettingsPage />);
  97. await waitFor(() => {
  98. expect(screen.getByText('Time Format')).toBeInTheDocument();
  99. });
  100. });
  101. it('shows default printer setting', async () => {
  102. render(<SettingsPage />);
  103. await waitFor(() => {
  104. expect(screen.getByText('Default Printer')).toBeInTheDocument();
  105. });
  106. });
  107. it('shows preferred slicer setting', async () => {
  108. render(<SettingsPage />);
  109. await waitFor(() => {
  110. expect(screen.getByText('Preferred Slicer')).toBeInTheDocument();
  111. });
  112. });
  113. it('shows slicer dropdown with both options', async () => {
  114. render(<SettingsPage />);
  115. await waitFor(() => {
  116. const slicerSelect = screen.getAllByDisplayValue('Bambu Studio');
  117. expect(slicerSelect.length).toBeGreaterThan(0);
  118. });
  119. });
  120. it('shows appearance section', async () => {
  121. render(<SettingsPage />);
  122. await waitFor(() => {
  123. expect(screen.getByText('Appearance')).toBeInTheDocument();
  124. });
  125. });
  126. it('shows updates section with firmware toggle', async () => {
  127. render(<SettingsPage />);
  128. await waitFor(() => {
  129. expect(screen.getByText('Updates')).toBeInTheDocument();
  130. expect(screen.getByText('Check for updates')).toBeInTheDocument();
  131. expect(screen.getByText('Check printer firmware')).toBeInTheDocument();
  132. });
  133. });
  134. });
  135. describe('tabs navigation', () => {
  136. it('can switch to Network tab', async () => {
  137. const user = userEvent.setup();
  138. render(<SettingsPage />);
  139. // Wait for settings to load first
  140. await waitFor(() => {
  141. expect(screen.getByText('Date Format')).toBeInTheDocument();
  142. });
  143. await user.click(screen.getByText('Network'));
  144. await waitFor(() => {
  145. // Network tab contains MQTT Publishing section
  146. expect(screen.getByText('MQTT Publishing')).toBeInTheDocument();
  147. });
  148. });
  149. it('can switch to Smart Plugs tab', async () => {
  150. const user = userEvent.setup();
  151. render(<SettingsPage />);
  152. await waitFor(() => {
  153. expect(screen.getByText('Smart Plugs')).toBeInTheDocument();
  154. });
  155. await user.click(screen.getByText('Smart Plugs'));
  156. await waitFor(() => {
  157. expect(screen.getByText('Add Smart Plug')).toBeInTheDocument();
  158. });
  159. });
  160. it('can switch to Notifications tab', async () => {
  161. const user = userEvent.setup();
  162. render(<SettingsPage />);
  163. await waitFor(() => {
  164. expect(screen.getByText('Notifications')).toBeInTheDocument();
  165. });
  166. await user.click(screen.getByText('Notifications'));
  167. await waitFor(() => {
  168. expect(screen.getByText('Add Provider')).toBeInTheDocument();
  169. });
  170. });
  171. it('can switch to Filament tab', async () => {
  172. const user = userEvent.setup();
  173. render(<SettingsPage />);
  174. await waitFor(() => {
  175. expect(screen.getAllByText('Filament').length).toBeGreaterThan(0);
  176. });
  177. await user.click(screen.getAllByText('Filament')[0]);
  178. await waitFor(() => {
  179. expect(screen.getByText('AMS Display Thresholds')).toBeInTheDocument();
  180. });
  181. });
  182. });
  183. describe('API Keys tab', () => {
  184. it('can switch to API Keys tab', async () => {
  185. const user = userEvent.setup();
  186. render(<SettingsPage />);
  187. await waitFor(() => {
  188. expect(screen.getByText('API Keys')).toBeInTheDocument();
  189. });
  190. await user.click(screen.getByText('API Keys'));
  191. await waitFor(() => {
  192. // Button text is "Create Key"
  193. expect(screen.getByText('Create Key')).toBeInTheDocument();
  194. });
  195. });
  196. });
  197. });