happ-settings-tun-mode.test.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it, vi } from 'vitest';
  2. import { fireEvent } from '@testing-library/react';
  3. import HappSettingsContent from '@/pages/settings/HappSettingsContent';
  4. import { AllSetting } from '@/models/setting';
  5. import { renderWithProviders } from './test-utils';
  6. function openTab(name: string) {
  7. const tab = Array.from(document.querySelectorAll('.ant-tabs-tab')).find((t) =>
  8. (t.textContent ?? '').includes(name),
  9. );
  10. if (!tab) throw new Error(`tab '${name}' not found`);
  11. fireEvent.click(tab);
  12. }
  13. function selectFor(title: string): HTMLElement {
  14. const row = Array.from(document.querySelectorAll('.ant-select')).find((s) =>
  15. (s.closest('li,div[class*="setting"]')?.textContent ?? '').includes(title),
  16. );
  17. if (!row) throw new Error(`select for '${title}' not found`);
  18. return row as HTMLElement;
  19. }
  20. function clickOption(text: string) {
  21. const option = Array.from(document.querySelectorAll('.ant-select-item-option')).find(
  22. (o) => (o.textContent ?? '').trim() === text,
  23. );
  24. if (!option) throw new Error(`option '${text}' not found`);
  25. fireEvent.click(option);
  26. }
  27. describe('Happ TUN Mode select', () => {
  28. // happ.su documents tun-mode as system|gvisor only, so the Default entry has
  29. // to mean "send no header", the same state a fresh panel ships with.
  30. it('stores the empty value for Default so no Tun-Mode header is emitted', () => {
  31. const updateSetting = vi.fn();
  32. const allSetting = new AllSetting();
  33. allSetting.subHappTunMode = 'gvisor';
  34. renderWithProviders(
  35. <HappSettingsContent
  36. allSetting={allSetting}
  37. updateSetting={updateSetting}
  38. isMobile={false}
  39. remoteSourceBadge={() => null}
  40. />,
  41. );
  42. openTab('Network');
  43. const select = selectFor('TUN Mode');
  44. fireEvent.mouseDown(select.querySelector('.ant-select-selector') ?? select);
  45. clickOption('Default');
  46. expect(updateSetting).toHaveBeenCalledWith({ subHappTunMode: '' });
  47. });
  48. it('labels the unset state Default rather than leaving the control blank', () => {
  49. renderWithProviders(
  50. <HappSettingsContent
  51. allSetting={new AllSetting()}
  52. updateSetting={vi.fn()}
  53. isMobile={false}
  54. remoteSourceBadge={() => null}
  55. />,
  56. );
  57. openTab('Network');
  58. const select = selectFor('TUN Mode');
  59. expect(select.querySelector('.ant-select-content')?.textContent).toBe('Default');
  60. });
  61. });