BigW Consortium Gitlab

issue_spec.js 4.72 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-var, one-var, one-var-declaration-per-line, no-use-before-define, comma-dangle, max-len */
2
/* global Issue */
Fatih Acet committed
3

4 5
require('~/lib/utils/text_utility');
require('~/issue');
Fatih Acet committed
6 7

(function() {
winniehell committed
8 9 10
  var INVALID_URL = 'http://goesnowhere.nothing/whereami';
  var $boxClosed, $boxOpen, $btnClose, $btnReopen;

11 12 13
  preloadFixtures('issues/closed-issue.html.raw');
  preloadFixtures('issues/issue-with-task-list.html.raw');
  preloadFixtures('issues/open-issue.html.raw');
14

winniehell committed
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
  function expectErrorMessage() {
    var $flashMessage = $('div.flash-alert');
    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);

    expectVisibility($btnClose, isIssueOpen);
    expectVisibility($btnReopen, !isIssueOpen);
  }

  function expectPendingRequest(req, $triggeredButton) {
    expect(req.type).toBe('PUT');
    expect(req.url).toBe($triggeredButton.attr('href'));
    expect($triggeredButton).toHaveProp('disabled', true);
  }

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

  function findElements() {
45 46 47
    $boxClosed = $('div.status-box-closed');
    expect($boxClosed).toExist();
    expect($boxClosed).toHaveText('Closed');
winniehell committed
48

49 50 51
    $boxOpen = $('div.status-box-open');
    expect($boxOpen).toExist();
    expect($boxOpen).toHaveText('Open');
winniehell committed
52

53 54 55
    $btnClose = $('.btn-close.btn-grouped');
    expect($btnClose).toExist();
    expect($btnClose).toHaveText('Close issue');
winniehell committed
56

57 58 59
    $btnReopen = $('.btn-reopen.btn-grouped');
    expect($btnReopen).toExist();
    expect($btnReopen).toHaveText('Reopen issue');
winniehell committed
60 61
  }

Fatih Acet committed
62
  describe('Issue', function() {
63
    describe('task lists', function() {
Fatih Acet committed
64
      beforeEach(function() {
65
        loadFixtures('issues/issue-with-task-list.html.raw');
66
        this.issue = new Issue();
Fatih Acet committed
67
      });
68

Fatih Acet committed
69 70 71
      it('modifies the Markdown field', function() {
        spyOn(jQuery, 'ajax').and.stub();
        $('input[type=checkbox]').attr('checked', true).trigger('change');
72
        expect($('.js-task-list-field').val()).toBe('- [x] Task List Item');
Fatih Acet committed
73
      });
74

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

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

winniehell committed
87
  describe('close issue', function() {
Fatih Acet committed
88
    beforeEach(function() {
89
      loadFixtures('issues/open-issue.html.raw');
winniehell committed
90 91 92 93
      findElements();
      this.issue = new Issue();

      expectIssueState(true);
Fatih Acet committed
94
    });
winniehell committed
95

Fatih Acet committed
96 97
    it('closes an issue', function() {
      spyOn(jQuery, 'ajax').and.callFake(function(req) {
winniehell committed
98 99
        expectPendingRequest(req, $btnClose);
        req.success({
Fatih Acet committed
100 101 102
          id: 34
        });
      });
winniehell committed
103

Fatih Acet committed
104
      $btnClose.trigger('click');
winniehell committed
105 106 107

      expectIssueState(false);
      expect($btnClose).toHaveProp('disabled', false);
Fatih Acet committed
108
    });
winniehell committed
109

Fatih Acet committed
110 111
    it('fails to close an issue with success:false', function() {
      spyOn(jQuery, 'ajax').and.callFake(function(req) {
winniehell committed
112 113
        expectPendingRequest(req, $btnClose);
        req.success({
Fatih Acet committed
114 115 116
          saved: false
        });
      });
winniehell committed
117 118

      $btnClose.attr('href', INVALID_URL);
Fatih Acet committed
119
      $btnClose.trigger('click');
winniehell committed
120 121 122 123

      expectIssueState(true);
      expect($btnClose).toHaveProp('disabled', false);
      expectErrorMessage();
Fatih Acet committed
124
    });
winniehell committed
125

Fatih Acet committed
126 127
    it('fails to closes an issue with HTTP error', function() {
      spyOn(jQuery, 'ajax').and.callFake(function(req) {
winniehell committed
128 129
        expectPendingRequest(req, $btnClose);
        req.error();
Fatih Acet committed
130
      });
winniehell committed
131 132

      $btnClose.attr('href', INVALID_URL);
Fatih Acet committed
133
      $btnClose.trigger('click');
winniehell committed
134 135 136 137 138 139 140 141 142

      expectIssueState(true);
      expect($btnClose).toHaveProp('disabled', true);
      expectErrorMessage();
    });
  });

  describe('reopen issue', function() {
    beforeEach(function() {
143
      loadFixtures('issues/closed-issue.html.raw');
winniehell committed
144 145 146
      findElements();
      this.issue = new Issue();

147
      expectIssueState(false);
Fatih Acet committed
148
    });
winniehell committed
149 150

    it('reopens an issue', function() {
Fatih Acet committed
151
      spyOn(jQuery, 'ajax').and.callFake(function(req) {
winniehell committed
152 153
        expectPendingRequest(req, $btnReopen);
        req.success({
Fatih Acet committed
154 155 156
          id: 34
        });
      });
winniehell committed
157

Fatih Acet committed
158
      $btnReopen.trigger('click');
winniehell committed
159 160 161

      expectIssueState(true);
      expect($btnReopen).toHaveProp('disabled', false);
Fatih Acet committed
162 163 164
    });
  });
}).call(this);