input.spec.jsx 3.4 KB

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