add.spec.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {
  3. makeClosableHeader,
  4. makeCloseButton,
  5. ModalBody,
  6. ModalFooter,
  7. } from 'sentry/components/globalModal/components';
  8. import {convertRelayPiiConfig} from 'sentry/views/settings/components/dataScrubbing/convertRelayPiiConfig';
  9. import Add from 'sentry/views/settings/components/dataScrubbing/modals/add';
  10. import {MethodType, RuleType} from 'sentry/views/settings/components/dataScrubbing/types';
  11. import {
  12. getMethodLabel,
  13. getRuleLabel,
  14. } from 'sentry/views/settings/components/dataScrubbing/utils';
  15. const relayPiiConfig = TestStubs.DataScrubbingRelayPiiConfig();
  16. const stringRelayPiiConfig = JSON.stringify(relayPiiConfig);
  17. const organizationSlug = 'sentry';
  18. const convertedRules = convertRelayPiiConfig(stringRelayPiiConfig);
  19. const rules = convertedRules;
  20. const successfullySaved = jest.fn();
  21. const projectId = 'foo';
  22. const endpoint = `/projects/${organizationSlug}/${projectId}/`;
  23. const api = new MockApiClient();
  24. describe('Add Modal', function () {
  25. it('open Add Rule Modal', async function () {
  26. const handleCloseModal = jest.fn();
  27. render(
  28. <Add
  29. Header={makeClosableHeader(handleCloseModal)}
  30. Body={ModalBody}
  31. Footer={ModalFooter}
  32. closeModal={handleCloseModal}
  33. CloseButton={makeCloseButton(handleCloseModal)}
  34. projectId={projectId}
  35. savedRules={rules}
  36. api={api}
  37. endpoint={endpoint}
  38. orgSlug={organizationSlug}
  39. onSubmitSuccess={successfullySaved}
  40. />
  41. );
  42. expect(
  43. screen.getByRole('heading', {name: 'Add an advanced data scrubbing rule'})
  44. ).toBeInTheDocument();
  45. // Method Field
  46. expect(screen.getByText('Method')).toBeInTheDocument();
  47. userEvent.hover(screen.getAllByTestId('more-information')[0]);
  48. expect(await screen.findByText('What to do')).toBeInTheDocument();
  49. userEvent.click(screen.getByText(getMethodLabel(MethodType.MASK).label));
  50. Object.values(MethodType).forEach(method => {
  51. if (method === MethodType.MASK) {
  52. return;
  53. }
  54. expect(screen.getByText(getMethodLabel(method).label)).toBeInTheDocument();
  55. });
  56. // Type Field
  57. expect(screen.getByText('Data Type')).toBeInTheDocument();
  58. userEvent.hover(screen.getAllByTestId('more-information')[1]);
  59. expect(
  60. await screen.findByText(
  61. 'What to look for. Use an existing pattern or define your own using regular expressions.'
  62. )
  63. ).toBeInTheDocument();
  64. userEvent.click(screen.getByText(getRuleLabel(RuleType.CREDITCARD)));
  65. Object.values(RuleType).forEach(rule => {
  66. if (rule === RuleType.CREDITCARD) {
  67. return;
  68. }
  69. expect(screen.getByText(getRuleLabel(rule))).toBeInTheDocument();
  70. });
  71. // Event ID
  72. expect(
  73. screen.getByRole('button', {name: 'Use event ID for auto-completion'})
  74. ).toBeInTheDocument();
  75. // Source Field
  76. screen.getByRole('textbox', {name: 'Source'});
  77. userEvent.hover(screen.getAllByTestId('more-information')[2]);
  78. expect(
  79. await screen.findByText(
  80. 'Where to look. In the simplest case this can be an attribute name.'
  81. )
  82. ).toBeInTheDocument();
  83. // Close Modal
  84. userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  85. expect(handleCloseModal).toHaveBeenCalled();
  86. });
  87. it('Display placeholder field', async function () {
  88. render(
  89. <Add
  90. Header={makeClosableHeader(jest.fn())}
  91. Body={ModalBody}
  92. Footer={ModalFooter}
  93. closeModal={jest.fn()}
  94. CloseButton={makeCloseButton(jest.fn())}
  95. projectId={projectId}
  96. savedRules={rules}
  97. api={api}
  98. endpoint={endpoint}
  99. orgSlug={organizationSlug}
  100. onSubmitSuccess={successfullySaved}
  101. />
  102. );
  103. expect(screen.queryByText('Custom Placeholder (Optional)')).not.toBeInTheDocument();
  104. userEvent.click(screen.getByText(getMethodLabel(MethodType.MASK).label));
  105. userEvent.keyboard('{arrowdown}{arrowdown}{enter}');
  106. expect(screen.getByText('Custom Placeholder (Optional)')).toBeInTheDocument();
  107. expect(screen.getByPlaceholderText('[Filtered]')).toBeInTheDocument();
  108. userEvent.hover(screen.getAllByTestId('more-information')[1]);
  109. expect(
  110. await screen.findByText('It will replace the default placeholder [Filtered]')
  111. ).toBeInTheDocument();
  112. });
  113. it('Display regex field', async function () {
  114. render(
  115. <Add
  116. Header={makeClosableHeader(jest.fn())}
  117. Body={ModalBody}
  118. Footer={ModalFooter}
  119. closeModal={jest.fn()}
  120. CloseButton={makeCloseButton(jest.fn())}
  121. projectId={projectId}
  122. savedRules={rules}
  123. api={api}
  124. endpoint={endpoint}
  125. orgSlug={organizationSlug}
  126. onSubmitSuccess={successfullySaved}
  127. />
  128. );
  129. expect(screen.queryByText('Regex matches')).not.toBeInTheDocument();
  130. userEvent.click(screen.getByText(getRuleLabel(RuleType.CREDITCARD)));
  131. userEvent.keyboard(
  132. '{arrowdown}{arrowdown}{arrowdown}{arrowdown}{arrowdown}{arrowdown}{enter}'
  133. );
  134. expect(screen.getAllByText('Regex matches')).toHaveLength(2);
  135. expect(screen.getByPlaceholderText('[a-zA-Z0-9]+')).toBeInTheDocument();
  136. userEvent.hover(screen.getAllByTestId('more-information')[2]);
  137. expect(
  138. await screen.findByText('Custom regular expression (see documentation)')
  139. ).toBeInTheDocument();
  140. });
  141. it('Display Event Id', async function () {
  142. const eventId = '12345678901234567890123456789012';
  143. MockApiClient.addMockResponse({
  144. url: `/organizations/${organizationSlug}/data-scrubbing-selector-suggestions/`,
  145. body: {
  146. suggestions: [
  147. {type: 'value', examples: ['34359738368'], value: "extra.'system.cpu.memory'"},
  148. {type: 'value', value: '$frame.abs_path'},
  149. ],
  150. },
  151. });
  152. render(
  153. <Add
  154. Header={makeClosableHeader(jest.fn())}
  155. Body={ModalBody}
  156. Footer={ModalFooter}
  157. closeModal={jest.fn()}
  158. CloseButton={makeCloseButton(jest.fn())}
  159. projectId={projectId}
  160. savedRules={rules}
  161. api={api}
  162. endpoint={endpoint}
  163. orgSlug={organizationSlug}
  164. onSubmitSuccess={successfullySaved}
  165. />
  166. );
  167. userEvent.click(
  168. screen.getByRole('button', {name: 'Use event ID for auto-completion'})
  169. );
  170. userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  171. expect(screen.getAllByRole('listitem')).toHaveLength(18);
  172. expect(screen.getByText('Event ID (Optional)')).toBeInTheDocument();
  173. userEvent.type(screen.getByPlaceholderText('XXXXXXXXXXXXXX'), `${eventId}{enter}`);
  174. expect(await screen.findByTestId('icon-check-mark')).toBeInTheDocument();
  175. userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  176. expect(screen.getAllByRole('listitem')).toHaveLength(3);
  177. });
  178. });