note_form_spec.js 4.1 KB
Newer Older
1
import Vue from 'vue';
2
import createStore from '~/notes/stores';
3
import issueNoteForm from '~/notes/components/note_form.vue';
Simon Knox's avatar
Simon Knox committed
4
import { noteableDataMock, notesDataMock } from '../mock_data';
5
import { keyboardDownEvent } from '../../issue_show/helpers';
6

7
describe('issue_note_form component', () => {
8
  let store;
9 10 11 12
  let vm;
  let props;

  beforeEach(() => {
13
    const Component = Vue.extend(issueNoteForm);
14

15
    store = createStore();
Simon Knox's avatar
Simon Knox committed
16
    store.dispatch('setNoteableData', noteableDataMock);
17 18 19
    store.dispatch('setNotesData', notesDataMock);

    props = {
20
      isEditing: false,
21
      noteBody: 'Magni suscipit eius consectetur enim et ex et commodi.',
22
      noteId: '545',
23 24 25 26 27 28 29 30 31 32 33 34
    };

    vm = new Component({
      store,
      propsData: props,
    }).$mount();
  });

  afterEach(() => {
    vm.$destroy();
  });

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  describe('noteHash', () => {
    it('returns note hash string based on `noteId`', () => {
      expect(vm.noteHash).toBe(`#note_${props.noteId}`);
    });

    it('return note hash as `#` when `noteId` is empty', done => {
      vm.noteId = '';
      Vue.nextTick()
        .then(() => {
          expect(vm.noteHash).toBe('#');
        })
        .then(done)
        .catch(done.fail);
    });
  });

51
  describe('conflicts editing', () => {
52
    it('should show conflict message if note changes outside the component', done => {
53
      vm.isEditing = true;
54
      vm.noteBody = 'Foo';
55 56
      const message =
        'This comment has changed since you started editing, please review the updated comment to ensure information is not lost.';
57 58

      Vue.nextTick(() => {
59
        expect(
60 61 62 63
          vm.$el
            .querySelector('.js-conflict-edit-warning')
            .textContent.replace(/\s+/g, ' ')
            .trim(),
64
        ).toEqual(message);
65 66
        done();
      });
67 68 69 70 71
    });
  });

  describe('form', () => {
    it('should render text area with placeholder', () => {
72 73 74
      expect(vm.$el.querySelector('textarea').getAttribute('placeholder')).toEqual(
        'Write a comment or drag your files here…',
      );
75 76 77
    });

    it('should link to markdown docs', () => {
78
      const { markdownDocsPath } = notesDataMock;
79

80 81 82
      expect(vm.$el.querySelector(`a[href="${markdownDocsPath}"]`).textContent.trim()).toEqual(
        'Markdown',
      );
83 84 85 86 87
    });

    describe('keyboard events', () => {
      describe('up', () => {
        it('should ender edit mode', () => {
88 89 90
          spyOn(vm, 'editMyLastNote').and.callThrough();
          vm.$el.querySelector('textarea').value = 'Foo';
          vm.$el.querySelector('textarea').dispatchEvent(keyboardDownEvent(38, true));
91

92
          expect(vm.editMyLastNote).toHaveBeenCalled();
93 94 95 96
        });
      });

      describe('enter', () => {
97
        it('should save note when cmd+enter is pressed', () => {
98 99 100
          spyOn(vm, 'handleUpdate').and.callThrough();
          vm.$el.querySelector('textarea').value = 'Foo';
          vm.$el.querySelector('textarea').dispatchEvent(keyboardDownEvent(13, true));
101

102 103
          expect(vm.handleUpdate).toHaveBeenCalled();
        });
104

105 106 107 108 109
        it('should save note when ctrl+enter is pressed', () => {
          spyOn(vm, 'handleUpdate').and.callThrough();
          vm.$el.querySelector('textarea').value = 'Foo';
          vm.$el.querySelector('textarea').dispatchEvent(keyboardDownEvent(13, false, true));

110
          expect(vm.handleUpdate).toHaveBeenCalled();
111 112 113 114 115
        });
      });
    });

    describe('actions', () => {
116
      it('should be possible to cancel', done => {
117 118
        spyOn(vm, 'cancelHandler').and.callThrough();
        vm.isEditing = true;
119

120 121
        Vue.nextTick(() => {
          vm.$el.querySelector('.note-edit-cancel').click();
122

123 124 125 126 127
          Vue.nextTick(() => {
            expect(vm.cancelHandler).toHaveBeenCalled();
            done();
          });
        });
128 129
      });

130
      it('should be possible to update the note', done => {
131
        vm.isEditing = true;
132

133 134 135 136 137 138 139 140 141
        Vue.nextTick(() => {
          vm.$el.querySelector('textarea').value = 'Foo';
          vm.$el.querySelector('.js-vue-issue-save').click();

          Vue.nextTick(() => {
            expect(vm.isSubmitting).toEqual(true);
            done();
          });
        });
142 143 144
      });
    });
  });
145
});