sourceField.spec.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import SourceField from 'sentry/views/settings/components/dataScrubbing/modals/form/sourceField';
  3. import {
  4. binarySuggestions,
  5. unarySuggestions,
  6. valueSuggestions,
  7. } from 'sentry/views/settings/components/dataScrubbing/utils';
  8. describe('Source', function () {
  9. it('default render', function () {
  10. render(
  11. <SourceField
  12. isRegExMatchesSelected={false}
  13. suggestions={valueSuggestions}
  14. onChange={jest.fn()}
  15. value="$string"
  16. />
  17. );
  18. expect(screen.getByRole('textbox', {name: 'Source'})).toHaveValue('$string');
  19. });
  20. it('display defaultSuggestions if input is empty and focused', async function () {
  21. render(
  22. <SourceField
  23. isRegExMatchesSelected={false}
  24. suggestions={valueSuggestions}
  25. onChange={jest.fn()}
  26. value=""
  27. />
  28. );
  29. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  30. const suggestions = screen.getAllByRole('listitem');
  31. // [...defaultSuggestions, ...unaryOperatorSuggestions].length === 18
  32. expect(suggestions).toHaveLength(18);
  33. });
  34. it('display defaultSuggestions if input is empty, focused and has length 3', async function () {
  35. render(
  36. <SourceField
  37. isRegExMatchesSelected={false}
  38. suggestions={valueSuggestions}
  39. onChange={jest.fn()}
  40. value=" "
  41. />
  42. );
  43. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  44. const suggestions = screen.getAllByRole('listitem');
  45. // [...defaultSuggestions, ...unaryOperatorSuggestions].length === 18
  46. expect(suggestions).toHaveLength(18);
  47. });
  48. it('display binaryOperatorSuggestions if penultimateFieldValue has type string', async function () {
  49. render(
  50. <SourceField
  51. isRegExMatchesSelected={false}
  52. suggestions={valueSuggestions}
  53. onChange={jest.fn()}
  54. value="foo "
  55. />
  56. );
  57. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  58. const suggestions = screen.getAllByRole('listitem');
  59. // binaryOperatorSuggestions.length === 2
  60. expect(suggestions).toHaveLength(2);
  61. // &&
  62. expect(suggestions[0]).toHaveTextContent(binarySuggestions[0].value);
  63. // ||
  64. expect(suggestions[1]).toHaveTextContent(binarySuggestions[1].value);
  65. });
  66. it('display defaultSuggestions + unaryOperatorSuggestions, if penultimateFieldValue has type binary', async function () {
  67. render(
  68. <SourceField
  69. isRegExMatchesSelected={false}
  70. suggestions={valueSuggestions}
  71. onChange={jest.fn()}
  72. value="foo && "
  73. />
  74. );
  75. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  76. const suggestions = screen.getAllByRole('listitem');
  77. // [...defaultSuggestions, ...unaryOperatorSuggestions].length === 18
  78. expect(suggestions).toHaveLength(18);
  79. // !
  80. expect(suggestions[17]).toHaveTextContent(unarySuggestions[0].value);
  81. });
  82. it('display binaryOperatorSuggestions if penultimateFieldValue has type value', async function () {
  83. render(
  84. <SourceField
  85. isRegExMatchesSelected={false}
  86. suggestions={valueSuggestions}
  87. onChange={jest.fn()}
  88. value="foo && $string "
  89. />
  90. );
  91. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  92. const suggestions = screen.getAllByRole('listitem');
  93. // binaryOperatorSuggestions.length === 2
  94. expect(suggestions).toHaveLength(2);
  95. // &&
  96. expect(suggestions[0]).toHaveTextContent(binarySuggestions[0].value);
  97. // ||
  98. expect(suggestions[1]).toHaveTextContent(binarySuggestions[1].value);
  99. });
  100. it('display binaryOperatorSuggestions if penultimateFieldValue is of typeof Array', async () => {
  101. render(
  102. <SourceField
  103. isRegExMatchesSelected={false}
  104. suggestions={valueSuggestions}
  105. onChange={jest.fn()}
  106. value="foo && !$string "
  107. />
  108. );
  109. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  110. const suggestions = screen.getAllByRole('listitem');
  111. // binaryOperatorSuggestions.length === 2
  112. expect(suggestions).toHaveLength(2);
  113. // &&
  114. expect(suggestions[0]).toHaveTextContent(binarySuggestions[0].value);
  115. // ||
  116. expect(suggestions[1]).toHaveTextContent(binarySuggestions[1].value);
  117. });
  118. it('display defaultSuggestions if penultimateFieldValue has type unary', async () => {
  119. render(
  120. <SourceField
  121. isRegExMatchesSelected={false}
  122. suggestions={valueSuggestions}
  123. onChange={jest.fn()}
  124. value="foo && !"
  125. />
  126. );
  127. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  128. const suggestions = screen.getAllByRole('listitem');
  129. // defaultSuggestions.length === 17
  130. expect(suggestions).toHaveLength(17);
  131. // everywhere
  132. expect(suggestions[0]).toHaveTextContent(
  133. `${valueSuggestions[0].value}(${valueSuggestions[0].description})`
  134. );
  135. });
  136. it('click on a suggestion should be possible', async function () {
  137. const handleOnChange = jest.fn();
  138. render(
  139. <SourceField
  140. isRegExMatchesSelected={false}
  141. suggestions={valueSuggestions}
  142. onChange={handleOnChange}
  143. value="foo && "
  144. />
  145. );
  146. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  147. const suggestions = screen.getAllByRole('listitem');
  148. await userEvent.click(suggestions[1]);
  149. expect(handleOnChange).toHaveBeenCalledWith('foo && password');
  150. });
  151. it('suggestions keyDown and keyUp should work', async function () {
  152. const handleOnChange = jest.fn();
  153. Element.prototype.scrollIntoView = jest.fn();
  154. render(
  155. <SourceField
  156. isRegExMatchesSelected={false}
  157. suggestions={valueSuggestions}
  158. onChange={handleOnChange}
  159. value="foo "
  160. />
  161. );
  162. // makes showSuggestions === true
  163. await userEvent.click(screen.getByRole('textbox', {name: 'Source'}));
  164. const suggestions = screen.getAllByRole('listitem');
  165. expect(suggestions).toHaveLength(2);
  166. await userEvent.keyboard('{ArrowDown}{Enter}');
  167. expect(handleOnChange).toHaveBeenNthCalledWith(1, 'foo ||');
  168. await userEvent.type(screen.getByRole('textbox', {name: 'Source'}), ' ');
  169. expect(handleOnChange).toHaveBeenNthCalledWith(2, 'foo ');
  170. await userEvent.keyboard('{ArrowDown}{ArrowUp}{Enter}');
  171. expect(handleOnChange).toHaveBeenNthCalledWith(3, 'foo &&');
  172. });
  173. });