sourceField.spec.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  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. function renderComponent({
  9. value = '$string',
  10. onChange = jest.fn(),
  11. ...props
  12. }: Partial<SourceField['props']>) {
  13. return mountWithTheme(
  14. <SourceField
  15. isRegExMatchesSelected={false}
  16. suggestions={valueSuggestions}
  17. onChange={onChange}
  18. value={value}
  19. {...props}
  20. />
  21. );
  22. }
  23. describe('Source', () => {
  24. it('default render', () => {
  25. const wrapper = renderComponent({});
  26. expect(wrapper.find('input').prop('value')).toBe('$string');
  27. });
  28. it('display defaultSuggestions if input is empty and focused', () => {
  29. const wrapper = renderComponent({value: ''});
  30. wrapper.find('input').simulate('focus');
  31. const suggestions = wrapper
  32. .find('[data-test-id="source-suggestions"]')
  33. .hostNodes()
  34. .children();
  35. // [...defaultSuggestions, ...unaryOperatorSuggestions].length === 18
  36. expect(suggestions).toHaveLength(18);
  37. });
  38. it('display defaultSuggestions if input is empty, focused and has length 3', () => {
  39. const wrapper = renderComponent({value: ' '});
  40. wrapper.find('input').simulate('focus');
  41. const suggestions = wrapper
  42. .find('[data-test-id="source-suggestions"]')
  43. .hostNodes()
  44. .children();
  45. // [...defaultSuggestions, ...unaryOperatorSuggestions].length === 18
  46. expect(suggestions).toHaveLength(18);
  47. });
  48. it('display binaryOperatorSuggestions if penultimateFieldValue has type string', () => {
  49. const wrapper = renderComponent({value: 'foo '});
  50. wrapper.find('input').simulate('focus');
  51. const suggestions = wrapper
  52. .find('[data-test-id="source-suggestions"]')
  53. .hostNodes()
  54. .children();
  55. // binaryOperatorSuggestions.length === 2
  56. expect(suggestions).toHaveLength(2);
  57. // &&
  58. expect(suggestions.at(0).text()).toEqual(binarySuggestions[0].value);
  59. // ||
  60. expect(suggestions.at(1).text()).toEqual(binarySuggestions[1].value);
  61. });
  62. it('display defaultSuggestions + unaryOperatorSuggestions, if penultimateFieldValue has type binary', () => {
  63. const wrapper = renderComponent({value: 'foo && '});
  64. wrapper.find('input').simulate('focus');
  65. const suggestions = wrapper
  66. .find('[data-test-id="source-suggestions"]')
  67. .hostNodes()
  68. .children();
  69. // [...defaultSuggestions, ...unaryOperatorSuggestions].length === 18
  70. expect(suggestions).toHaveLength(18);
  71. // !
  72. expect(suggestions.at(17).text()).toEqual(unarySuggestions[0].value);
  73. });
  74. it('display binaryOperatorSuggestions if penultimateFieldValue has type value', () => {
  75. const wrapper = renderComponent({value: 'foo && $string '});
  76. wrapper.find('input').simulate('focus');
  77. const suggestions = wrapper
  78. .find('[data-test-id="source-suggestions"]')
  79. .hostNodes()
  80. .children();
  81. // binaryOperatorSuggestions.length === 2
  82. expect(suggestions).toHaveLength(2);
  83. // &&
  84. expect(suggestions.at(0).text()).toEqual(binarySuggestions[0].value);
  85. // ||
  86. expect(suggestions.at(1).text()).toEqual(binarySuggestions[1].value);
  87. });
  88. it('display binaryOperatorSuggestions if penultimateFieldValue is of typeof Array', () => {
  89. const wrapper = renderComponent({value: 'foo && !$string '});
  90. wrapper.find('input').simulate('focus');
  91. const suggestions = wrapper
  92. .find('[data-test-id="source-suggestions"]')
  93. .hostNodes()
  94. .children();
  95. // binaryOperatorSuggestions.length === 2
  96. expect(suggestions).toHaveLength(2);
  97. // &&
  98. expect(suggestions.at(0).text()).toEqual(binarySuggestions[0].value);
  99. // ||
  100. expect(suggestions.at(1).text()).toEqual(binarySuggestions[1].value);
  101. });
  102. it('display defaultSuggestions if penultimateFieldValue has type unary', () => {
  103. const wrapper = renderComponent({value: 'foo && !'});
  104. wrapper.find('input').simulate('focus');
  105. const suggestions = wrapper
  106. .find('[data-test-id="source-suggestions"]')
  107. .hostNodes()
  108. .children();
  109. // defaultSuggestions.length === 17
  110. expect(suggestions).toHaveLength(17);
  111. // everywhere
  112. expect(suggestions.at(0).text()).toEqual(
  113. `${valueSuggestions[0].value}(${valueSuggestions[0].description})`
  114. );
  115. });
  116. it('click on a suggestion should be possible', () => {
  117. const handleOnChange = jest.fn();
  118. const wrapper = renderComponent({value: 'foo && ', onChange: handleOnChange});
  119. // makes showSuggestions === true
  120. wrapper.find('input').simulate('focus');
  121. const suggestions = wrapper
  122. .find('[data-test-id="source-suggestions"]')
  123. .hostNodes()
  124. .children();
  125. suggestions.at(1).simulate('click');
  126. expect(wrapper.state().fieldValues[2].value).toBe(valueSuggestions[1].value);
  127. });
  128. it('suggestions keyDown and keyUp should work', () => {
  129. const handleOnChange = jest.fn();
  130. Element.prototype.scrollIntoView = jest.fn();
  131. const wrapper = renderComponent({value: 'foo ', onChange: handleOnChange});
  132. const input = wrapper.find('input');
  133. // makes showSuggestions === true
  134. input.simulate('focus');
  135. const suggestions = wrapper
  136. .find('[data-test-id="source-suggestions"]')
  137. .hostNodes()
  138. .children();
  139. expect(suggestions).toHaveLength(2);
  140. expect(suggestions.at(0).prop('active')).toBe(true);
  141. input.simulate('keyDown', {keyCode: 40});
  142. expect(wrapper.state().activeSuggestion).toBe(1);
  143. input.simulate('keyDown', {keyCode: 13});
  144. expect(wrapper.state().activeSuggestion).toBe(0);
  145. expect(wrapper.state().fieldValues[1].value).toBe('||');
  146. expect(handleOnChange).toHaveBeenCalledWith('foo ||');
  147. input.simulate('change', {target: {value: 'foo || '}});
  148. input
  149. .simulate('keyDown', {keyCode: 40})
  150. .simulate('keyDown', {keyCode: 40})
  151. .simulate('keyDown', {keyCode: 38});
  152. expect(wrapper.state().activeSuggestion).toBe(1);
  153. });
  154. });