input.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import changeReactMentionsInput from 'sentry-test/changeReactMentionsInput';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import NoteInput from 'sentry/components/activity/note/input';
  4. describe('NoteInput', function () {
  5. describe('New item', function () {
  6. const props = {
  7. group: {project: {}, id: 'groupId'},
  8. memberList: [],
  9. teams: [],
  10. };
  11. it('renders', function () {
  12. mountWithTheme(<NoteInput {...props} />);
  13. });
  14. it('submits when meta + enter is pressed', function () {
  15. const onCreate = jest.fn();
  16. const wrapper = mountWithTheme(<NoteInput {...props} onCreate={onCreate} />);
  17. const input = wrapper.find('textarea');
  18. changeReactMentionsInput(wrapper, 'something');
  19. input.simulate('keyDown', {key: 'Enter', metaKey: true});
  20. expect(onCreate).toHaveBeenCalled();
  21. });
  22. it('submits when ctrl + enter is pressed', function () {
  23. const onCreate = jest.fn();
  24. const wrapper = mountWithTheme(<NoteInput {...props} onCreate={onCreate} />);
  25. const input = wrapper.find('textarea');
  26. changeReactMentionsInput(wrapper, 'something');
  27. input.simulate('keyDown', {key: 'Enter', ctrlKey: true});
  28. expect(onCreate).toHaveBeenCalled();
  29. });
  30. it('does not submit when nothing is entered', function () {
  31. const onCreate = jest.fn();
  32. const wrapper = mountWithTheme(<NoteInput {...props} onCreate={onCreate} />);
  33. const input = wrapper.find('textarea');
  34. input.simulate('keyDown', {key: 'Enter', metaKey: true});
  35. expect(onCreate).not.toHaveBeenCalled();
  36. });
  37. it('handles errors', function () {
  38. const errorJSON = {detail: {message: '', code: 401, extra: ''}};
  39. const wrapper = mountWithTheme(
  40. <NoteInput {...props} error={!!errorJSON} errorJSON={errorJSON} />
  41. );
  42. const input = wrapper.find('textarea');
  43. changeReactMentionsInput(wrapper, 'something');
  44. input.simulate('keyDown', {key: 'Enter', ctrlKey: true});
  45. wrapper.update();
  46. expect(wrapper.find('ErrorMessage')).toHaveLength(1);
  47. });
  48. it('has a disabled submit button when no text is entered', function () {
  49. const errorJSON = {detail: {message: '', code: 401, extra: ''}};
  50. const wrapper = mountWithTheme(
  51. <NoteInput {...props} error={!!errorJSON} errorJSON={errorJSON} />
  52. );
  53. expect(wrapper.find('button[type="submit"]').prop('disabled')).toBe(true);
  54. });
  55. it('enables the submit button when text is entered', function () {
  56. const errorJSON = {detail: {message: '', code: 401, extra: ''}};
  57. const wrapper = mountWithTheme(
  58. <NoteInput {...props} error={!!errorJSON} errorJSON={errorJSON} />
  59. );
  60. changeReactMentionsInput(wrapper, 'something');
  61. expect(wrapper.find('button[type="submit"]').prop('disabled')).toBe(false);
  62. });
  63. });
  64. describe('Existing Item', function () {
  65. const defaultProps = {
  66. group: {project: {}, id: 'groupId'},
  67. modelId: 'item-id',
  68. text: 'an existing item',
  69. memberList: [],
  70. teams: [],
  71. };
  72. const createWrapper = props =>
  73. mountWithTheme(<NoteInput {...defaultProps} {...props} />);
  74. it('edits existing message', function () {
  75. const onUpdate = jest.fn();
  76. const wrapper = createWrapper({onUpdate});
  77. expect(wrapper.find('NoteInputNavTabLink').first().text()).toBe('Edit');
  78. // Switch to preview
  79. wrapper.find('NoteInputNavTabLink').last().simulate('click');
  80. expect(wrapper.find('NotePreview').text()).toBe('an existing item\n');
  81. // Switch to edit
  82. wrapper.find('NoteInputNavTabLink').first().simulate('click');
  83. expect(wrapper.find('textarea').prop('value')).toBe('an existing item');
  84. // Can edit text
  85. changeReactMentionsInput(wrapper, 'new item');
  86. wrapper.find('textarea').simulate('keyDown', {key: 'Enter', ctrlKey: true});
  87. expect(onUpdate).toHaveBeenCalledWith({text: 'new item', mentions: []});
  88. });
  89. it('canels editing and moves to preview mode', function () {
  90. const onEditFinish = jest.fn();
  91. const wrapper = createWrapper({onEditFinish});
  92. changeReactMentionsInput(wrapper, 'new value');
  93. expect(wrapper.find('FooterButton').first().text()).toBe('Cancel');
  94. wrapper.find('FooterButton').first().simulate('click');
  95. expect(onEditFinish).toHaveBeenCalled();
  96. });
  97. });
  98. });