editableText.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {createListeners} from 'sentry-test/createListeners';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {act} from 'sentry-test/reactTestingLibrary';
  4. import EditableText from 'sentry/components/editableText';
  5. const currentValue = 'foo';
  6. function renderedComponent(onChange: () => void, newValue = 'bar', maxLength?: number) {
  7. const wrapper = mountWithTheme(
  8. <EditableText value={currentValue} onChange={onChange} maxLength={maxLength} />
  9. );
  10. let label = wrapper.find('Label');
  11. expect(label.text()).toEqual(currentValue);
  12. let inputWrapper = wrapper.find('InputWrapper');
  13. expect(inputWrapper.length).toEqual(0);
  14. const styledIconEdit = wrapper.find('IconEdit');
  15. expect(styledIconEdit.length).toEqual(1);
  16. label.simulate('click');
  17. label = wrapper.find('Label');
  18. expect(inputWrapper.length).toEqual(0);
  19. inputWrapper = wrapper.find('InputWrapper');
  20. expect(inputWrapper.length).toEqual(1);
  21. const styledInput = wrapper.find('StyledInput');
  22. expect(styledInput.length).toEqual(1);
  23. styledInput.simulate('change', {target: {value: newValue}});
  24. const inputLabel = wrapper.find('InputLabel');
  25. expect(inputLabel.text()).toEqual(newValue);
  26. return wrapper;
  27. }
  28. describe('EditableText', function () {
  29. const newValue = 'bar';
  30. it('edit value and click outside of the component', function () {
  31. const fireEvent = createListeners('document');
  32. const handleChange = jest.fn();
  33. const wrapper = renderedComponent(handleChange);
  34. act(() => {
  35. // Click outside of the component
  36. fireEvent.mouseDown(document.body);
  37. });
  38. expect(handleChange).toHaveBeenCalled();
  39. wrapper.update();
  40. const updatedLabel = wrapper.find('Label');
  41. expect(updatedLabel.length).toEqual(1);
  42. expect(updatedLabel.text()).toEqual(newValue);
  43. });
  44. it('edit value and press enter', function () {
  45. const fireEvent = createListeners('window');
  46. const handleChange = jest.fn();
  47. const wrapper = renderedComponent(handleChange);
  48. act(() => {
  49. // Press enter
  50. fireEvent.keyDown('Enter');
  51. });
  52. expect(handleChange).toHaveBeenCalled();
  53. wrapper.update();
  54. const updatedLabel = wrapper.find('Label');
  55. expect(updatedLabel.length).toEqual(1);
  56. expect(updatedLabel.text()).toEqual(newValue);
  57. });
  58. it('clear value and show error message', function () {
  59. const fireEvent = createListeners('window');
  60. const handleChange = jest.fn();
  61. const wrapper = renderedComponent(handleChange, '');
  62. act(() => {
  63. // Press enter
  64. fireEvent.keyDown('Enter');
  65. });
  66. expect(handleChange).toHaveBeenCalledTimes(0);
  67. wrapper.update();
  68. });
  69. it('displays a disabled value', function () {
  70. const handleChange = jest.fn();
  71. const wrapper = mountWithTheme(
  72. <EditableText value={currentValue} onChange={handleChange} isDisabled />
  73. );
  74. let label = wrapper.find('Label');
  75. expect(label.text()).toEqual(currentValue);
  76. label.simulate('click');
  77. const inputWrapper = wrapper.find('InputWrapper');
  78. expect(inputWrapper.length).toEqual(0);
  79. label = wrapper.find('Label');
  80. expect(label.length).toEqual(1);
  81. });
  82. describe('revert value and close editor', function () {
  83. it('prop value changes', function () {
  84. const handleChange = jest.fn();
  85. const newPropValue = 'new-prop-value';
  86. const wrapper = renderedComponent(handleChange, '');
  87. wrapper.setProps({value: newPropValue});
  88. wrapper.update();
  89. const updatedLabel = wrapper.find('Label');
  90. expect(updatedLabel.length).toEqual(1);
  91. expect(updatedLabel.text()).toEqual(newPropValue);
  92. });
  93. it('prop isDisabled changes', function () {
  94. const handleChange = jest.fn();
  95. const wrapper = renderedComponent(handleChange, '');
  96. wrapper.setProps({isDisabled: true});
  97. wrapper.update();
  98. const updatedLabel = wrapper.find('Label');
  99. expect(updatedLabel.length).toEqual(1);
  100. expect(updatedLabel.text()).toEqual(currentValue);
  101. });
  102. it('edit value and press escape', function () {
  103. const fireEvent = createListeners('window');
  104. const handleChange = jest.fn();
  105. const wrapper = renderedComponent(handleChange);
  106. act(() => {
  107. // Press escape
  108. fireEvent.keyDown('Escape');
  109. });
  110. expect(handleChange).toHaveBeenCalledTimes(0);
  111. wrapper.update();
  112. const updatedLabel = wrapper.find('Label');
  113. expect(updatedLabel.length).toEqual(1);
  114. expect(updatedLabel.text()).toEqual(currentValue);
  115. });
  116. it('enforces a max length if provided', function () {
  117. const wrapper = renderedComponent(jest.fn(), '', 4);
  118. const input = wrapper.find('input');
  119. expect(input.prop('maxLength')).toBe(4);
  120. });
  121. });
  122. });