add.spec.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {DataScrubbingRelayPiiConfig} from 'sentry-fixture/dataScrubbingRelayPiiConfig';
  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 Add from 'sentry/views/settings/components/dataScrubbing/modals/add';
  11. import {MethodType, RuleType} from 'sentry/views/settings/components/dataScrubbing/types';
  12. import {
  13. getMethodLabel,
  14. getRuleLabel,
  15. } from 'sentry/views/settings/components/dataScrubbing/utils';
  16. const relayPiiConfig = DataScrubbingRelayPiiConfig();
  17. const stringRelayPiiConfig = JSON.stringify(relayPiiConfig);
  18. const organizationSlug = 'sentry';
  19. const rules = convertRelayPiiConfig(stringRelayPiiConfig);
  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. await userEvent.hover(screen.getAllByTestId('more-information')[0]);
  48. expect(await screen.findByText('What to do')).toBeInTheDocument();
  49. await 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. await 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. await 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. await 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. await 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. await userEvent.click(screen.getByText(getMethodLabel(MethodType.MASK).label));
  105. await userEvent.keyboard('{arrowdown}{arrowdown}{enter}');
  106. expect(screen.getByText('Custom Placeholder (Optional)')).toBeInTheDocument();
  107. expect(screen.getByPlaceholderText('[Filtered]')).toBeInTheDocument();
  108. await 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. await userEvent.click(screen.getByText(getRuleLabel(RuleType.CREDITCARD)));
  131. await 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. await 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. await userEvent.click(
  168. screen.getByRole('button', {name: 'Use event ID for auto-completion'})
  169. );
  170. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  171. expect(screen.getAllByRole('listitem')).toHaveLength(18);
  172. expect(screen.getByText('Event ID (Optional)')).toBeInTheDocument();
  173. await userEvent.type(
  174. screen.getByPlaceholderText('XXXXXXXXXXXXXX'),
  175. `${eventId}{enter}`
  176. );
  177. expect(await screen.findByTestId('icon-check-mark')).toBeInTheDocument();
  178. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  179. expect(screen.getAllByRole('listitem')).toHaveLength(3);
  180. });
  181. });