BigW Consortium Gitlab

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

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

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

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

winniehell committed
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
  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 committed
61
  describe('Issue', function() {
62 63
    describe('task lists', function() {
      fixture.load('issues/issue-with-task-list.html');
Fatih Acet committed
64
      beforeEach(function() {
65
        this.issue = new Issue();
Fatih Acet committed
66
      });
67

Fatih Acet committed
68 69 70
      it('modifies the Markdown field', function() {
        spyOn(jQuery, 'ajax').and.stub();
        $('input[type=checkbox]').attr('checked', true).trigger('change');
71
        expect($('.js-task-list-field').val()).toBe('- [x] Task List Item');
Fatih Acet committed
72
      });
73 74
      
      it('submits an ajax request on tasklist:changed', function() {
Fatih Acet committed
75 76
        spyOn(jQuery, 'ajax').and.callFake(function(req) {
          expect(req.type).toBe('PATCH');
77 78
          expect(req.url).toBe('https://fixture.invalid/namespace3/project3/issues/1.json');
          expect(req.data.issue.description).not.toBe(null);
Fatih Acet committed
79
        });
80 81

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}).call(this);