BigW Consortium Gitlab

issue_spec.js 6.38 KB
Newer Older
1
/* eslint-disable space-before-function-paren, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */
2
import Issue from '~/issue';
3
import '~/lib/utils/text_utility';
Fatih Acet committed
4

5
describe('Issue', function() {
6
  let $boxClosed, $boxOpen, $btn;
winniehell committed
7

8 9 10
  preloadFixtures('issues/closed-issue.html.raw');
  preloadFixtures('issues/issue-with-task-list.html.raw');
  preloadFixtures('issues/open-issue.html.raw');
11

winniehell committed
12
  function expectErrorMessage() {
13
    const $flashMessage = $('div.flash-alert');
winniehell committed
14 15 16 17 18 19 20 21
    expect($flashMessage).toExist();
    expect($flashMessage).toBeVisible();
    expect($flashMessage).toHaveText('Unable to update this issue at this time.');
  }

  function expectIssueState(isIssueOpen) {
    expectVisibility($boxClosed, !isIssueOpen);
    expectVisibility($boxOpen, isIssueOpen);
22
    expect($btn).toHaveText(isIssueOpen ? 'Close issue' : 'Reopen issue');
winniehell committed
23 24
  }

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  function expectNewBranchButtonState(isPending, canCreate) {
    if (Issue.$btnNewBranch.length === 0) {
      return;
    }

    const $available = Issue.$btnNewBranch.find('.available');
    expect($available).toHaveText('New branch');

    if (!isPending && canCreate) {
      expect($available).toBeVisible();
    } else {
      expect($available).toBeHidden();
    }

    const $unavailable = Issue.$btnNewBranch.find('.unavailable');
    expect($unavailable).toHaveText('New branch unavailable');

    if (!isPending && !canCreate) {
      expect($unavailable).toBeVisible();
    } else {
      expect($unavailable).toBeHidden();
    }
winniehell committed
47 48 49 50 51 52 53 54 55 56
  }

  function expectVisibility($element, shouldBeVisible) {
    if (shouldBeVisible) {
      expect($element).not.toHaveClass('hidden');
    } else {
      expect($element).toHaveClass('hidden');
    }
  }

57
  function findElements(isIssueInitiallyOpen) {
58 59 60
    $boxClosed = $('div.status-box-closed');
    expect($boxClosed).toExist();
    expect($boxClosed).toHaveText('Closed');
winniehell committed
61

62 63 64
    $boxOpen = $('div.status-box-open');
    expect($boxOpen).toExist();
    expect($boxOpen).toHaveText('Open');
winniehell committed
65

66 67 68
    $btn = $('.js-issuable-close-button');
    expect($btn).toExist();
    expect($btn).toHaveText(isIssueInitiallyOpen ? 'Close issue' : 'Reopen issue');
winniehell committed
69 70
  }

71 72 73 74 75
  describe('task lists', function() {
    beforeEach(function() {
      loadFixtures('issues/issue-with-task-list.html.raw');
      this.issue = new Issue();
    });
76

77 78 79 80 81
    it('submits an ajax request on tasklist:changed', function() {
      spyOn(jQuery, 'ajax').and.callFake(function(req) {
        expect(req.type).toBe('PATCH');
        expect(req.url).toBe(gl.TEST_HOST + '/frontend-fixtures/issues-project/issues/1.json'); // eslint-disable-line prefer-template
        expect(req.data.issue.description).not.toBe(null);
Fatih Acet committed
82
      });
83 84

      $('.js-task-list-field').trigger('tasklist:changed');
Fatih Acet committed
85 86 87
    });
  });

88 89 90 91 92 93 94 95 96
  [true, false].forEach((isIssueInitiallyOpen) => {
    describe(`with ${isIssueInitiallyOpen ? 'open' : 'closed'} issue`, function() {
      const action = isIssueInitiallyOpen ? 'close' : 'reopen';

      function ajaxSpy(req) {
        if (req.url === this.$triggeredButton.attr('href')) {
          expect(req.type).toBe('PUT');
          expectNewBranchButtonState(true, false);
          return this.issueStateDeferred;
97 98
        } else if (req.url === Issue.createMrDropdownWrap.dataset.canCreatePath) {
          expect(req.type).toBe('GET');
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
          expectNewBranchButtonState(true, false);
          return this.canCreateBranchDeferred;
        }

        expect(req.url).toBe('unexpected');
        return null;
      }

      beforeEach(function() {
        if (isIssueInitiallyOpen) {
          loadFixtures('issues/open-issue.html.raw');
        } else {
          loadFixtures('issues/closed-issue.html.raw');
        }

114
        findElements(isIssueInitiallyOpen);
115 116
        this.issue = new Issue();
        expectIssueState(isIssueInitiallyOpen);
117 118

        this.$triggeredButton = $btn;
119

120
        this.$projectIssuesCounter = $('.issue_counter').first();
121 122 123 124 125 126 127
        this.$projectIssuesCounter.text('1,001');

        this.issueStateDeferred = new jQuery.Deferred();
        this.canCreateBranchDeferred = new jQuery.Deferred();

        spyOn(jQuery, 'ajax').and.callFake(ajaxSpy.bind(this));
      });
winniehell committed
128

129 130 131
      it(`${action}s the issue`, function() {
        this.$triggeredButton.trigger('click');
        this.issueStateDeferred.resolve({
Fatih Acet committed
132 133
          id: 34
        });
134 135 136
        this.canCreateBranchDeferred.resolve({
          can_create_branch: !isIssueInitiallyOpen
        });
winniehell committed
137

138
        expectIssueState(!isIssueInitiallyOpen);
139
        expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
140 141 142
        expect(this.$projectIssuesCounter.text()).toBe(isIssueInitiallyOpen ? '1,000' : '1,002');
        expectNewBranchButtonState(false, !isIssueInitiallyOpen);
      });
winniehell committed
143

144 145 146
      it(`fails to ${action} the issue if saved:false`, function() {
        this.$triggeredButton.trigger('click');
        this.issueStateDeferred.resolve({
Fatih Acet committed
147 148
          saved: false
        });
149 150 151
        this.canCreateBranchDeferred.resolve({
          can_create_branch: isIssueInitiallyOpen
        });
winniehell committed
152

153
        expectIssueState(isIssueInitiallyOpen);
154
        expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
155 156 157
        expectErrorMessage();
        expect(this.$projectIssuesCounter.text()).toBe('1,001');
        expectNewBranchButtonState(false, isIssueInitiallyOpen);
Fatih Acet committed
158
      });
winniehell committed
159

160 161 162 163 164
      it(`fails to ${action} the issue if HTTP error occurs`, function() {
        this.$triggeredButton.trigger('click');
        this.issueStateDeferred.reject();
        this.canCreateBranchDeferred.resolve({
          can_create_branch: isIssueInitiallyOpen
165 166
        });

167
        expectIssueState(isIssueInitiallyOpen);
168
        expect(this.$triggeredButton.get(0).getAttribute('disabled')).toBeNull();
169 170 171 172
        expectErrorMessage();
        expect(this.$projectIssuesCounter.text()).toBe('1,001');
        expectNewBranchButtonState(false, isIssueInitiallyOpen);
      });
winniehell committed
173

174 175 176 177
      it('disables the new branch button if Ajax call fails', function() {
        this.$triggeredButton.trigger('click');
        this.issueStateDeferred.reject();
        this.canCreateBranchDeferred.reject();
winniehell committed
178

179
        expectNewBranchButtonState(false, false);
Fatih Acet committed
180
      });
winniehell committed
181

182 183 184
      it('does not trigger Ajax call if new branch button is missing', function() {
        Issue.$btnNewBranch = $();
        this.canCreateBranchDeferred = null;
winniehell committed
185

186 187 188
        this.$triggeredButton.trigger('click');
        this.issueStateDeferred.reject();
      });
Fatih Acet committed
189 190
    });
  });
191
});