summaryrefslogtreecommitdiff
path: root/spec/javascripts/notes/components/issue_comment_form_spec.js
blob: cca5ec887a365e9410be54a14d948f7b8809303c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Vue from 'vue';
import store from '~/notes/stores';
import issueCommentForm from '~/notes/components/issue_comment_form.vue';
import { loggedOutIssueData, notesDataMock, userDataMock, issueDataMock } from '../mock_data';
import { keyboardDownEvent } from '../../issue_show/helpers';

describe('issue_comment_form component', () => {
  let vm;
  const Component = Vue.extend(issueCommentForm);
  let mountComponent;

  beforeEach(() => {
    mountComponent = () => new Component({
      store,
    }).$mount();
  });

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

  describe('user is logged in', () => {
    beforeEach(() => {
      store.dispatch('setUserData', userDataMock);
      store.dispatch('setIssueData', issueDataMock);
      store.dispatch('setNotesData', notesDataMock);

      vm = mountComponent();
    });

    it('should render user avatar with link', () => {
      expect(vm.$el.querySelector('.timeline-icon .user-avatar-link').getAttribute('href')).toEqual(userDataMock.path);
    });

    describe('textarea', () => {
      it('should render textarea with placeholder', () => {
        expect(
          vm.$el.querySelector('.js-main-target-form textarea').getAttribute('placeholder'),
        ).toEqual('Write a comment or drag your files here...');
      });

      it('should support quick actions', () => {
        expect(
          vm.$el.querySelector('.js-main-target-form textarea').getAttribute('data-supports-quick-actions'),
        ).toEqual('true');
      });

      it('should link to markdown docs', () => {
        const { markdownDocsPath } = notesDataMock;
        expect(vm.$el.querySelector(`a[href="${markdownDocsPath}"]`).textContent.trim()).toEqual('Markdown');
      });

      it('should link to quick actions docs', () => {
        const { quickActionsDocsPath } = notesDataMock;
        expect(vm.$el.querySelector(`a[href="${quickActionsDocsPath}"]`).textContent.trim()).toEqual('quick actions');
      });

      describe('edit mode', () => {
        it('should enter edit mode when arrow up is pressed', () => {
          spyOn(vm, 'editCurrentUserLastNote').and.callThrough();
          vm.$el.querySelector('.js-main-target-form textarea').value = 'Foo';
          vm.$el.querySelector('.js-main-target-form textarea').dispatchEvent(keyboardDownEvent(38, true));

          expect(vm.editCurrentUserLastNote).toHaveBeenCalled();
        });
      });

      describe('event enter', () => {
        it('should save note when cmd/ctrl+enter is pressed', () => {
          spyOn(vm, 'handleSave').and.callThrough();
          vm.$el.querySelector('.js-main-target-form textarea').value = 'Foo';
          vm.$el.querySelector('.js-main-target-form textarea').dispatchEvent(keyboardDownEvent(13, true));

          expect(vm.handleSave).toHaveBeenCalled();
        });
      });
    });

    describe('actions', () => {
      it('should be possible to close the issue', () => {
        expect(vm.$el.querySelector('.btn-comment-and-close').textContent.trim()).toEqual('Close issue');
      });

      it('should render comment button as disabled', () => {
        expect(vm.$el.querySelector('.js-comment-submit-button').getAttribute('disabled')).toEqual('disabled');
      });

      it('should enable comment button if it has note', (done) => {
        vm.note = 'Foo';
        Vue.nextTick(() => {
          expect(vm.$el.querySelector('.js-comment-submit-button').getAttribute('disabled')).toEqual(null);
          done();
        });
      });

      it('should update buttons texts when it has note', (done) => {
        vm.note = 'Foo';
        Vue.nextTick(() => {
          expect(vm.$el.querySelector('.btn-comment-and-close').textContent.trim()).toEqual('Comment & close issue');
          expect(vm.$el.querySelector('.js-note-discard')).toBeDefined();
          done();
        });
      });
    });

    describe('issue is confidential', () => {
      it('shows information warning', (done) => {
        store.dispatch('setIssueData', Object.assign(issueDataMock, { confidential: true }));
        Vue.nextTick(() => {
          expect(vm.$el.querySelector('.confidential-issue-warning')).toBeDefined();
          done();
        });
      });
    });
  });

  describe('user is not logged in', () => {
    beforeEach(() => {
      store.dispatch('setUserData', null);
      store.dispatch('setIssueData', loggedOutIssueData);
      store.dispatch('setNotesData', notesDataMock);

      vm = mountComponent();
    });

    it('should render signed out widget', () => {
      expect(vm.$el.textContent.replace(/\s+/g, ' ').trim()).toEqual('Please register or sign in to reply');
    });

    it('should not render submission form', () => {
      expect(vm.$el.querySelector('textarea')).toEqual(null);
    });
  });
});