outbounds-loopback-index.test.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, it, expect, vi } from 'vitest';
  2. import { fireEvent } from '@testing-library/react';
  3. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  4. import OutboundsTab from '@/pages/xray/outbounds/OutboundsTab';
  5. import type { XraySettingsValue } from '@/hooks/useXraySetting';
  6. import { renderWithProviders } from './test-utils';
  7. function settingsWithHiddenLoopback(): XraySettingsValue {
  8. return {
  9. outbounds: [
  10. { tag: 'proxy-a', protocol: 'vmess' },
  11. { tag: '_bl_bal1', protocol: 'loopback' },
  12. { tag: 'proxy-b', protocol: 'vmess' },
  13. ],
  14. } as unknown as XraySettingsValue;
  15. }
  16. describe('OutboundsTab hidden-loopback index mapping', () => {
  17. it('probes the outbound at its real array index, not the positional row index', () => {
  18. const onTest = vi.fn();
  19. const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  20. renderWithProviders(
  21. <QueryClientProvider client={queryClient}>
  22. <OutboundsTab
  23. templateSettings={settingsWithHiddenLoopback()}
  24. setTemplateSettings={vi.fn()}
  25. outboundsTraffic={[]}
  26. outboundTestStates={{}}
  27. subscriptionTestStates={{}}
  28. testingAll={false}
  29. inboundTags={[]}
  30. isMobile={false}
  31. onResetTraffic={vi.fn()}
  32. onTest={onTest}
  33. onTestSubscription={vi.fn()}
  34. onTestAll={vi.fn()}
  35. onShowWarp={vi.fn()}
  36. onShowNord={vi.fn()}
  37. />
  38. </QueryClientProvider>,
  39. );
  40. const tbody = document.querySelector('.ant-table-tbody');
  41. const tableRows = tbody?.querySelectorAll('tr.ant-table-row') ?? [];
  42. expect(tableRows.length).toBe(2);
  43. const checkButton = tableRows[1].querySelector('button[aria-label="Check"]') as HTMLButtonElement;
  44. fireEvent.click(checkButton);
  45. expect(onTest).toHaveBeenCalledTimes(1);
  46. expect(onTest.mock.calls[0][0]).toBe(2);
  47. });
  48. it('probes the real array index from the mobile card list as well', () => {
  49. const onTest = vi.fn();
  50. const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  51. renderWithProviders(
  52. <QueryClientProvider client={queryClient}>
  53. <OutboundsTab
  54. templateSettings={settingsWithHiddenLoopback()}
  55. setTemplateSettings={vi.fn()}
  56. outboundsTraffic={[]}
  57. outboundTestStates={{}}
  58. subscriptionTestStates={{}}
  59. testingAll={false}
  60. inboundTags={[]}
  61. isMobile
  62. onResetTraffic={vi.fn()}
  63. onTest={onTest}
  64. onTestSubscription={vi.fn()}
  65. onTestAll={vi.fn()}
  66. onShowWarp={vi.fn()}
  67. onShowNord={vi.fn()}
  68. />
  69. </QueryClientProvider>,
  70. );
  71. const cards = document.querySelectorAll('.outbound-card');
  72. expect(cards.length).toBe(2);
  73. const checkButton = cards[1].querySelector('button[aria-label="Check"]') as HTMLButtonElement;
  74. fireEvent.click(checkButton);
  75. expect(onTest).toHaveBeenCalledTimes(1);
  76. expect(onTest.mock.calls[0][0]).toBe(2);
  77. });
  78. });