issue_spec.js 4.76 KB
Newer Older
1 2
/* eslint-disable space-before-function-paren, no-var, one-var, one-var-declaration-per-line, no-use-before-define, indent, no-trailing-spaces, comma-dangle, padded-blocks, max-len */
/* global Issue */
Fatih Acet's avatar
Fatih Acet committed
3 4 5 6 7

/*= require lib/utils/text_utility */
/*= require issue */

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

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

winniehell's avatar
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  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() {
      $boxClosed = $('div.status-box-closed');
      expect($boxClosed).toExist();
      expect($boxClosed).toHaveText('Closed');

      $boxOpen = $('div.status-box-open');
      expect($boxOpen).toExist();
      expect($boxOpen).toHaveText('Open');

      $btnClose = $('.btn-close.btn-grouped');
      expect($btnClose).toExist();
      expect($btnClose).toHaveText('Close issue');

      $btnReopen = $('.btn-reopen.btn-grouped');
      expect($btnReopen).toExist();
      expect($btnReopen).toHaveText('Reopen issue');
  }

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

Fatih Acet's avatar
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's avatar
Fatih Acet committed
73
      });
74

75
      it('submits an ajax request on tasklist:changed', function() {
Fatih Acet's avatar
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's avatar
Fatih Acet committed
80
        });
81 82

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

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

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

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

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

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

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

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

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

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

      $btnClose.attr('href', INVALID_URL);
Fatih Acet's avatar
Fatih Acet committed
133
      $btnClose.trigger('click');
winniehell's avatar
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
      fixture.load('issues/closed-issue.html');
winniehell's avatar
winniehell committed
144 145 146
      findElements();
      this.issue = new Issue();

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

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

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

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

}).call(this);