summaryrefslogtreecommitdiff
path: root/spec/javascripts/merge_request_spec.js
blob: 998637ef595687f7f4b92b5dacdb87b3771fed28 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable no-return-assign */

import $ from 'jquery';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import MergeRequest from '~/merge_request';
import CloseReopenReportToggle from '~/close_reopen_report_toggle';
import IssuablesHelper from '~/helpers/issuables_helper';

describe('MergeRequest', function() {
  describe('task lists', function() {
    let mock;

    preloadFixtures('merge_requests/merge_request_with_task_list.html');
    beforeEach(function() {
      loadFixtures('merge_requests/merge_request_with_task_list.html');

      spyOn(axios, 'patch').and.callThrough();
      mock = new MockAdapter(axios);

      mock
        .onPatch(`${gl.TEST_HOST}/frontend-fixtures/merge-requests-project/merge_requests/1.json`)
        .reply(200, {});

      return (this.merge = new MergeRequest());
    });

    afterEach(() => {
      mock.restore();
    });

    it('modifies the Markdown field', done => {
      spyOn($, 'ajax').and.stub();
      const changeEvent = document.createEvent('HTMLEvents');
      changeEvent.initEvent('change', true, true);
      $('input[type=checkbox]')
        .attr('checked', true)[0]
        .dispatchEvent(changeEvent);
      setTimeout(() => {
        expect($('.js-task-list-field').val()).toBe('- [x] Task List Item');
        done();
      });
    });

    describe('tasklist', () => {
      const lineNumber = 8;
      const lineSource = '- [ ] item 8';
      const index = 3;
      const checked = true;

      it('submits an ajax request on tasklist:changed', done => {
        $('.js-task-list-field').trigger({
          type: 'tasklist:changed',
          detail: { lineNumber, lineSource, index, checked },
        });

        setTimeout(() => {
          expect(axios.patch).toHaveBeenCalledWith(
            `${gl.TEST_HOST}/frontend-fixtures/merge-requests-project/merge_requests/1.json`,
            {
              merge_request: {
                description: '- [ ] Task List Item',
                lock_version: 0,
                update_task: { line_number: lineNumber, line_source: lineSource, index, checked },
              },
            },
          );

          done();
        });
      });

      // eslint-disable-next-line jasmine/no-disabled-tests
      xit('shows an error notification when tasklist update failed', done => {
        mock
          .onPatch(`${gl.TEST_HOST}/frontend-fixtures/merge-requests-project/merge_requests/1.json`)
          .reply(409, {});

        $('.js-task-list-field').trigger({
          type: 'tasklist:changed',
          detail: { lineNumber, lineSource, index, checked },
        });

        setTimeout(() => {
          expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
            'Someone edited this merge request at the same time you did. Please refresh the page to see changes.',
          );

          done();
        });
      });
    });
  });

  describe('class constructor', () => {
    beforeEach(() => {
      spyOn($, 'ajax').and.stub();
    });

    it('calls .initCloseReopenReport', () => {
      spyOn(IssuablesHelper, 'initCloseReopenReport');

      new MergeRequest(); // eslint-disable-line no-new

      expect(IssuablesHelper.initCloseReopenReport).toHaveBeenCalled();
    });

    it('calls .initDroplab', () => {
      const container = jasmine.createSpyObj('container', ['querySelector']);
      const dropdownTrigger = {};
      const dropdownList = {};
      const button = {};

      spyOn(CloseReopenReportToggle.prototype, 'initDroplab');
      spyOn(document, 'querySelector').and.returnValue(container);
      container.querySelector.and.returnValues(dropdownTrigger, dropdownList, button);

      new MergeRequest(); // eslint-disable-line no-new

      expect(document.querySelector).toHaveBeenCalledWith('.js-issuable-close-dropdown');
      expect(container.querySelector).toHaveBeenCalledWith('.js-issuable-close-toggle');
      expect(container.querySelector).toHaveBeenCalledWith('.js-issuable-close-menu');
      expect(container.querySelector).toHaveBeenCalledWith('.js-issuable-close-button');
      expect(CloseReopenReportToggle.prototype.initDroplab).toHaveBeenCalled();
    });
  });

  describe('hideCloseButton', () => {
    describe('merge request of another user', () => {
      beforeEach(() => {
        loadFixtures('merge_requests/merge_request_with_task_list.html');
        this.el = document.querySelector('.js-issuable-actions');
        new MergeRequest(); // eslint-disable-line no-new
        MergeRequest.hideCloseButton();
      });

      it('hides the dropdown close item and selects the next item', () => {
        const closeItem = this.el.querySelector('li.close-item');
        const smallCloseItem = this.el.querySelector('.js-close-item');
        const reportItem = this.el.querySelector('li.report-item');

        expect(closeItem).toHaveClass('hidden');
        expect(smallCloseItem).toHaveClass('hidden');
        expect(reportItem).toHaveClass('droplab-item-selected');
        expect(reportItem).not.toHaveClass('hidden');
      });
    });

    describe('merge request of current_user', () => {
      beforeEach(() => {
        loadFixtures('merge_requests/merge_request_of_current_user.html');
        this.el = document.querySelector('.js-issuable-actions');
        MergeRequest.hideCloseButton();
      });

      it('hides the close button', () => {
        const closeButton = this.el.querySelector('.btn-close');
        const smallCloseItem = this.el.querySelector('.js-close-item');

        expect(closeButton).toHaveClass('hidden');
        expect(smallCloseItem).toHaveClass('hidden');
      });
    });
  });
});