edit.spec.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import selectEvent from 'react-select-event';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {
  4. makeClosableHeader,
  5. makeCloseButton,
  6. ModalBody,
  7. ModalFooter,
  8. } from 'sentry/components/globalModal/components';
  9. import {convertRelayPiiConfig} from 'sentry/views/settings/components/dataScrubbing/convertRelayPiiConfig';
  10. import Edit from 'sentry/views/settings/components/dataScrubbing/modals/edit';
  11. import submitRules from 'sentry/views/settings/components/dataScrubbing/submitRules';
  12. import {MethodType, RuleType} from 'sentry/views/settings/components/dataScrubbing/types';
  13. import {
  14. getMethodLabel,
  15. getRuleLabel,
  16. valueSuggestions,
  17. } from 'sentry/views/settings/components/dataScrubbing/utils';
  18. const relayPiiConfig = TestStubs.DataScrubbingRelayPiiConfig();
  19. const stringRelayPiiConfig = JSON.stringify(relayPiiConfig);
  20. const organizationSlug = 'sentry';
  21. const convertedRules = convertRelayPiiConfig(stringRelayPiiConfig);
  22. const rules = convertedRules;
  23. const rule = rules[2];
  24. const projectId = 'foo';
  25. const endpoint = `/projects/${organizationSlug}/${projectId}/`;
  26. const api = new MockApiClient();
  27. jest.mock('sentry/views/settings/components/dataScrubbing/submitRules');
  28. describe('Edit Modal', function () {
  29. it('open Edit Rule Modal', async function () {
  30. const handleCloseModal = jest.fn();
  31. render(
  32. <Edit
  33. Body={ModalBody}
  34. closeModal={handleCloseModal}
  35. CloseButton={makeCloseButton(jest.fn())}
  36. Header={makeClosableHeader(jest.fn())}
  37. Footer={ModalFooter}
  38. projectId={projectId}
  39. savedRules={rules}
  40. api={api}
  41. endpoint={endpoint}
  42. orgSlug={organizationSlug}
  43. onSubmitSuccess={jest.fn()}
  44. rule={rule}
  45. />
  46. );
  47. expect(
  48. screen.getByRole('heading', {name: 'Edit an advanced data scrubbing rule'})
  49. ).toBeInTheDocument();
  50. // Method Field
  51. expect(screen.getByText('Method')).toBeInTheDocument();
  52. await userEvent.hover(screen.getAllByTestId('more-information')[0]);
  53. expect(await screen.findByText('What to do')).toBeInTheDocument();
  54. await userEvent.click(screen.getByText('Replace'));
  55. Object.values(MethodType)
  56. .filter(method => method !== MethodType.REPLACE)
  57. .forEach(method => {
  58. expect(screen.getByText(getMethodLabel(method).label)).toBeInTheDocument();
  59. });
  60. // Placeholder Field
  61. expect(screen.getByText('Custom Placeholder (Optional)')).toBeInTheDocument();
  62. await userEvent.hover(screen.getAllByTestId('more-information')[1]);
  63. expect(
  64. await screen.findByText('It will replace the default placeholder [Filtered]')
  65. ).toBeInTheDocument();
  66. expect(screen.getByPlaceholderText('[Filtered]')).toBeInTheDocument();
  67. // Type Field
  68. expect(screen.getByText('Data Type')).toBeInTheDocument();
  69. await userEvent.hover(screen.getAllByTestId('more-information')[2]);
  70. expect(
  71. await screen.findByText(
  72. 'What to look for. Use an existing pattern or define your own using regular expressions.'
  73. )
  74. ).toBeInTheDocument();
  75. await userEvent.click(screen.getAllByText('Regex matches')[0]);
  76. Object.values(RuleType)
  77. .filter(ruleType => ruleType !== RuleType.PATTERN)
  78. .forEach(ruleType => {
  79. expect(screen.getByText(getRuleLabel(ruleType))).toBeInTheDocument();
  80. });
  81. await userEvent.click(screen.getAllByText('Regex matches')[0]);
  82. // Regex matches Field
  83. expect(screen.getAllByText('Regex matches')).toHaveLength(2);
  84. await userEvent.hover(screen.getAllByTestId('more-information')[3]);
  85. expect(
  86. await screen.findByText('Custom regular expression (see documentation)')
  87. ).toBeInTheDocument();
  88. expect(screen.getByRole('textbox', {name: 'Regex matches'})).toHaveAttribute(
  89. 'placeholder',
  90. '[a-zA-Z0-9]+'
  91. );
  92. // Event ID
  93. expect(
  94. screen.getByRole('button', {name: 'Use event ID for auto-completion'})
  95. ).toBeInTheDocument();
  96. // Source Field
  97. expect(screen.getByText('Source')).toBeInTheDocument();
  98. await userEvent.hover(screen.getAllByTestId('more-information')[4]);
  99. expect(
  100. await screen.findByText(
  101. 'Where to look. In the simplest case this can be an attribute name.'
  102. )
  103. ).toBeInTheDocument();
  104. expect(screen.getByRole('textbox', {name: 'Source'})).toHaveAttribute(
  105. 'placeholder',
  106. 'Enter a custom attribute, variable or header name'
  107. );
  108. // Close Modal
  109. await userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  110. expect(handleCloseModal).toHaveBeenCalled();
  111. });
  112. it('edit Rule Modal', async function () {
  113. render(
  114. <Edit
  115. Body={ModalBody}
  116. closeModal={jest.fn()}
  117. CloseButton={makeCloseButton(jest.fn())}
  118. Header={makeClosableHeader(jest.fn())}
  119. Footer={ModalFooter}
  120. projectId={projectId}
  121. savedRules={rules}
  122. api={api}
  123. endpoint={endpoint}
  124. orgSlug={organizationSlug}
  125. onSubmitSuccess={jest.fn()}
  126. rule={rule}
  127. />
  128. );
  129. // Method Field
  130. await selectEvent.select(screen.getByText('Replace'), 'Mask');
  131. // Placeholder Field should be now hidden
  132. expect(screen.queryByText('Custom Placeholder (Optional)')).not.toBeInTheDocument();
  133. // Type Field
  134. await selectEvent.select(screen.getAllByText('Regex matches')[0], 'Anything');
  135. // Regex Field should be now hidden
  136. expect(screen.queryByText('Regex matches')).not.toBeInTheDocument();
  137. // Source Field
  138. await userEvent.clear(screen.getByRole('textbox', {name: 'Source'}));
  139. await userEvent.type(
  140. screen.getByRole('textbox', {name: 'Source'}),
  141. valueSuggestions[2].value
  142. );
  143. // Save rule
  144. await userEvent.click(screen.getByRole('button', {name: 'Save Rule'}));
  145. expect(submitRules).toHaveBeenCalledWith(api, endpoint, [
  146. {
  147. id: 0,
  148. method: 'replace',
  149. type: 'password',
  150. source: 'password',
  151. placeholder: 'Scrubbed',
  152. },
  153. {id: 1, method: 'mask', type: 'creditcard', source: '$message'},
  154. {
  155. id: 2,
  156. method: 'mask',
  157. pattern: '',
  158. placeholder: '',
  159. type: 'anything',
  160. source: valueSuggestions[2].value,
  161. },
  162. ]);
  163. });
  164. });