inputWithStorage.spec.jsx 1.8 KB

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