input.spec.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. modelId: 'item-id',
  46. text: 'an existing item',
  47. memberList: [],
  48. teams: [],
  49. };
  50. const createWrapper = props => {
  51. return mount(<NoteInput {...defaultProps} {...props} />, routerContext);
  52. };
  53. it('edits existing message', async function() {
  54. const onUpdate = jest.fn();
  55. const wrapper = createWrapper({onUpdate});
  56. expect(
  57. wrapper
  58. .find('NoteInputNavTabLink')
  59. .first()
  60. .text()
  61. ).toBe('Edit');
  62. // Switch to preview
  63. wrapper
  64. .find('NoteInputNavTabLink')
  65. .last()
  66. .simulate('click');
  67. expect(wrapper.find('NotePreview').text()).toBe('an existing item\n');
  68. // Switch to edit
  69. wrapper
  70. .find('NoteInputNavTabLink')
  71. .first()
  72. .simulate('click');
  73. expect(wrapper.find('textarea').prop('value')).toBe('an existing item');
  74. // Can edit text
  75. changeReactMentionsInput(wrapper, 'new item');
  76. wrapper.find('textarea').simulate('keyDown', {key: 'Enter', ctrlKey: true});
  77. expect(onUpdate).toHaveBeenCalledWith({text: 'new item', mentions: []});
  78. });
  79. it('canels editing and moves to preview mode', async function() {
  80. const onEditFinish = jest.fn();
  81. const wrapper = createWrapper({onEditFinish});
  82. changeReactMentionsInput(wrapper, 'new value');
  83. expect(
  84. wrapper
  85. .find('FooterButton')
  86. .first()
  87. .text()
  88. ).toBe('Cancel');
  89. wrapper
  90. .find('FooterButton')
  91. .first()
  92. .simulate('click');
  93. expect(onEditFinish).toHaveBeenCalled();
  94. });
  95. });
  96. });