rule-form-preserve-fields.test.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { afterEach, describe, it, expect, vi } from 'vitest';
  2. import { fireEvent, screen, waitFor } from '@testing-library/react';
  3. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  4. import RuleFormModal from '@/pages/xray/routing/RuleFormModal';
  5. import { keys } from '@/api/queryKeys';
  6. import { HttpUtil, Msg } from '@/utils';
  7. import { chooseSelectOption, renderWithProviders } from './test-utils';
  8. describe('RuleFormModal edit preserves unsurfaced fields', () => {
  9. afterEach(() => vi.restoreAllMocks());
  10. it('keeps a field the form does not surface (ruleTag) when saving an edit', () => {
  11. vi.spyOn(HttpUtil, 'get').mockResolvedValue(new Msg(true, '', []));
  12. const onConfirm = vi.fn();
  13. const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  14. renderWithProviders(
  15. <QueryClientProvider client={queryClient}>
  16. <RuleFormModal
  17. open
  18. rule={{ type: 'field', outboundTag: 'block', ruleTag: 'my-tag', enabled: true }}
  19. inboundTags={[]}
  20. outboundTags={['block']}
  21. balancerTags={[]}
  22. onClose={vi.fn()}
  23. onConfirm={onConfirm}
  24. />
  25. </QueryClientProvider>,
  26. );
  27. fireEvent.click(screen.getByRole('button', { name: 'Save Changes' }));
  28. expect(onConfirm).toHaveBeenCalledTimes(1);
  29. expect(onConfirm.mock.calls[0][0]).toMatchObject({ ruleTag: 'my-tag' });
  30. });
  31. it('selects existing clients for the user criterion', async () => {
  32. vi.spyOn(HttpUtil, 'get').mockResolvedValue(
  33. new Msg(true, '', [
  34. { id: 1, email: '[email protected]' },
  35. { id: 2, email: '[email protected]' },
  36. ]),
  37. );
  38. const onConfirm = vi.fn();
  39. const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  40. renderWithProviders(
  41. <RuleFormModal
  42. open
  43. rule={null}
  44. inboundTags={[]}
  45. outboundTags={['direct']}
  46. balancerTags={[]}
  47. onClose={vi.fn()}
  48. onConfirm={onConfirm}
  49. />,
  50. { queryClient },
  51. );
  52. await waitFor(() =>
  53. expect(HttpUtil.get).toHaveBeenCalledWith('/panel/api/clients/list', undefined, {
  54. silent: true,
  55. }),
  56. );
  57. await waitFor(() => expect(queryClient.getQueryData(keys.clients.all())).toHaveLength(2));
  58. const userField = screen.getByLabelText('User');
  59. expect(screen.getByText('Select users')).toBeTruthy();
  60. chooseSelectOption(userField.id, '[email protected]');
  61. chooseSelectOption(userField.id, '[email protected]');
  62. fireEvent.click(screen.getByRole('button', { name: 'Create' }));
  63. expect(onConfirm).toHaveBeenCalledWith(
  64. expect.objectContaining({ user: ['[email protected]', '[email protected]'] }),
  65. );
  66. });
  67. it('preserves a saved user that is no longer in the client list', async () => {
  68. vi.spyOn(HttpUtil, 'get').mockResolvedValue(
  69. new Msg(true, '', [{ id: 1, email: '[email protected]' }]),
  70. );
  71. const onConfirm = vi.fn();
  72. renderWithProviders(
  73. <RuleFormModal
  74. open
  75. rule={{ type: 'field', user: ['[email protected]'], outboundTag: 'direct' }}
  76. inboundTags={[]}
  77. outboundTags={['direct']}
  78. balancerTags={[]}
  79. onClose={vi.fn()}
  80. onConfirm={onConfirm}
  81. />,
  82. );
  83. await waitFor(() => expect(screen.getByText('[email protected]')).toBeTruthy());
  84. fireEvent.click(screen.getByRole('button', { name: 'Save Changes' }));
  85. expect(onConfirm).toHaveBeenCalledWith(
  86. expect.objectContaining({ user: ['[email protected]'] }),
  87. );
  88. });
  89. it('accepts a custom user identifier that is not a client record', async () => {
  90. vi.spyOn(HttpUtil, 'get').mockResolvedValue(
  91. new Msg(true, '', [{ id: 1, email: '[email protected]' }]),
  92. );
  93. const onConfirm = vi.fn();
  94. renderWithProviders(
  95. <RuleFormModal
  96. open
  97. rule={null}
  98. inboundTags={[]}
  99. outboundTags={['direct']}
  100. balancerTags={[]}
  101. onClose={vi.fn()}
  102. onConfirm={onConfirm}
  103. />,
  104. );
  105. const userField = screen.getByLabelText('User');
  106. fireEvent.change(userField, { target: { value: 'office-proxy,' } });
  107. fireEvent.click(screen.getByRole('button', { name: 'Create' }));
  108. expect(onConfirm).toHaveBeenCalledWith(expect.objectContaining({ user: ['office-proxy'] }));
  109. });
  110. });