inputWithStorage.spec.jsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import changeReactMentionsInput from 'sentry-test/changeReactMentionsInput';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import NoteInputWithStorage from 'sentry/components/activity/note/inputWithStorage';
  4. import localStorage from 'sentry/utils/localStorage';
  5. jest.mock('sentry/utils/localStorage');
  6. describe('NoteInputWithStorage', function () {
  7. const defaultProps = {
  8. storageKey: 'storage',
  9. itemKey: 'item1',
  10. group: {project: {}, id: 'groupId'},
  11. memberList: [],
  12. teams: [],
  13. };
  14. const createWrapper = props =>
  15. mountWithTheme(<NoteInputWithStorage {...defaultProps} {...props} />);
  16. it('loads draft item from local storage when mounting', function () {
  17. localStorage.getItem.mockImplementation(() => JSON.stringify({item1: 'saved item'}));
  18. const wrapper = createWrapper();
  19. expect(localStorage.getItem).toHaveBeenCalledWith('storage');
  20. expect(wrapper.find('textarea').prop('value')).toBe('saved item');
  21. });
  22. it('saves draft when input changes', function () {
  23. const wrapper = createWrapper();
  24. changeReactMentionsInput(wrapper, 'WIP COMMENT');
  25. expect(localStorage.setItem).toHaveBeenCalledWith(
  26. 'storage',
  27. JSON.stringify({item1: 'WIP COMMENT'})
  28. );
  29. });
  30. it('removes draft item after submitting', function () {
  31. localStorage.getItem.mockImplementation(() =>
  32. JSON.stringify({item1: 'draft item', item2: 'item2', item3: 'item3'})
  33. );
  34. const wrapper = createWrapper();
  35. changeReactMentionsInput(wrapper, 'new comment');
  36. wrapper.find('textarea').simulate('keyDown', {key: 'Enter', ctrlKey: true});
  37. expect(localStorage.setItem).toHaveBeenLastCalledWith(
  38. 'storage',
  39. JSON.stringify({item2: 'item2', item3: 'item3'})
  40. );
  41. });
  42. });