aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/tests/apps/background-tasks-test.js
blob: 63b61f4dd20a35ac57563d7440d97cf60f67b42f (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* eslint no-unused-expressions: 0 */
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';

import Header from '../../src/main/js/apps/background-tasks/components/Header';
import Stats from '../../src/main/js/apps/background-tasks/components/Stats';
import Search from '../../src/main/js/apps/background-tasks/components/Search';
import Tasks from '../../src/main/js/apps/background-tasks/components/Tasks';
import { STATUSES, CURRENTS, DEBOUNCE_DELAY } from '../../src/main/js/apps/background-tasks/constants';
import { formatDuration } from '../../src/main/js/apps/background-tasks/helpers';

let chai = require('chai');
let expect = chai.expect;
let sinon = require('sinon');
chai.use(require('sinon-chai'));

describe('Background Tasks', function () {
  describe('Constants', () => {
    it('should have STATUSES', () => {
      expect(STATUSES).to.be.a('object');
      expect(Object.keys(STATUSES).length).to.equal(6);
    });

    it('should have CURRENTS', () => {
      expect(CURRENTS).to.be.a('object');
      expect(Object.keys(CURRENTS).length).to.equal(2);
    });
  });

  describe('Search', () => {
    it('should render search form', () => {
      let component = TestUtils.renderIntoDocument(
          <Search
              types={[]}
              date={{}}/>
          ),
          searchBox = TestUtils.scryRenderedDOMComponentsWithClass(component, 'js-search');
      expect(searchBox).to.have.length(1);
    });

    it('should not render search form', () => {
      let component = TestUtils.renderIntoDocument(
          <Search
              component={{ id: 'ABCD' }}
              types={[]}
              date={{}}/>
          ),
          searchBox = TestUtils.scryRenderedDOMComponentsWithClass(component, 'js-search');
      expect(searchBox).to.be.empty;
    });

    it('should search', (done) => {
      let searchSpy = sinon.spy();
      let component = TestUtils.renderIntoDocument(
          <Search
              types={[]}
              date={{}}
              onSearch={searchSpy}/>);
      let searchInput = ReactDOM.findDOMNode(
          TestUtils.findRenderedDOMComponentWithClass(component, 'js-search'));
      searchInput.value = 'some search query';
      TestUtils.Simulate.change(searchInput);
      setTimeout(() => {
        expect(searchSpy).to.have.been.calledWith('some search query');
        done();
      }, DEBOUNCE_DELAY);
    });

    it('should reload', () => {
      let reloadSpy = sinon.spy();
      let component = TestUtils.renderIntoDocument(
          <Search
              types={[]}
              date={{}}
              onRefresh={reloadSpy}/>
      );
      let reloadButton = component.refs.reloadButton;
      expect(reloadSpy).to.not.have.been.called;
      TestUtils.Simulate.click(reloadButton);
      expect(reloadSpy).to.have.been.called;
    });
  });

  describe('Stats', () => {
    describe('Pending', () => {
      it('should show zero pending', () => {
        let result = TestUtils.renderIntoDocument(<Stats pendingCount="0"/>),
            pendingCounter = result.refs.pendingCount;
        expect(pendingCounter.textContent).to.contain('0');
      });

      it('should show 5 pending', () => {
        let result = TestUtils.renderIntoDocument(<Stats pendingCount="5"/>),
            pendingCounter = result.refs.pendingCount;
        expect(pendingCounter.textContent).to.contain('5');
      });

      it('should not show cancel pending button', () => {
        let result = TestUtils.renderIntoDocument(<Stats pendingCount="0"/>),
            cancelPending = result.refs.cancelPending;
        expect(cancelPending).to.not.be.ok;
      });

      it('should show cancel pending button', () => {
        let result = TestUtils.renderIntoDocument(<Stats pendingCount="5"/>),
            cancelPending = result.refs.cancelPending;
        expect(cancelPending).to.be.ok;
      });

      it('should trigger cancelling pending', () => {
        let spy = sinon.spy();
        let result = TestUtils.renderIntoDocument(<Stats pendingCount="5" onCancelAllPending={spy}/>),
            cancelPending = result.refs.cancelPending;
        expect(spy).to.not.have.been.called;
        TestUtils.Simulate.click(cancelPending);
        expect(spy).to.have.been.called;
      });
    });

    describe('Failures', () => {
      it('should show zero failures', () => {
        let result = TestUtils.renderIntoDocument(<Stats failingCount="0"/>),
            failureCounter = result.refs.failureCount;
        expect(failureCounter.textContent).to.contain('0');
      });

      it('should show 5 failures', () => {
        let result = TestUtils.renderIntoDocument(<Stats failingCount="5"/>),
            failureCounter = result.refs.failureCount;
        expect(failureCounter.textContent).to.contain('5');
      });

      it('should not show link to failures', () => {
        let result = TestUtils.renderIntoDocument(<Stats failingCount="0"/>),
            failureCounter = result.refs.failureCount;
        expect(failureCounter.tagName.toLowerCase()).to.not.equal('a');
      });

      it('should show link to failures', () => {
        let result = TestUtils.renderIntoDocument(<Stats failingCount="5"/>),
            failureCounter = result.refs.failureCount;
        expect(failureCounter.tagName.toLowerCase()).to.equal('a');
      });

      it('should trigger filtering failures', () => {
        let spy = sinon.spy();
        let result = TestUtils.renderIntoDocument(<Stats failingCount="5" onShowFailing={spy}/>),
            failureCounter = result.refs.failureCount;
        expect(spy).to.not.have.been.called;
        TestUtils.Simulate.click(failureCounter);
        expect(spy).to.have.been.called;
      });
    });

    describe('In Progress Duration', () => {
      it('should show duration', () => {
        let result = TestUtils.renderIntoDocument(<Stats inProgressDuration="173"/>),
            inProgressDuration = result.refs.inProgressDuration;
        expect(inProgressDuration.textContent).to.include('173ms');
      });

      it('should format duration', () => {
        let result = TestUtils.renderIntoDocument(<Stats inProgressDuration="1073"/>),
            inProgressDuration = result.refs.inProgressDuration;
        expect(inProgressDuration.textContent).to.include('1s');
      });

      it('should not show duration', () => {
        let result = TestUtils.renderIntoDocument(<Stats/>),
            inProgressDuration = result.refs.inProgressDuration;
        expect(inProgressDuration).to.not.be.ok;
      });
    });
  });

  describe('Helpers', () => {
    describe('#formatDuration()', () => {
      it('should format 173ms', () => {
        expect(formatDuration(173)).to.equal('173ms');
      });

      it('should format 999ms', () => {
        expect(formatDuration(999)).to.equal('999ms');
      });

      it('should format 1s', () => {
        expect(formatDuration(1000)).to.equal('1s');
      });

      it('should format 1s', () => {
        expect(formatDuration(1001)).to.equal('1s');
      });

      it('should format 2s', () => {
        expect(formatDuration(1501)).to.equal('2s');
      });

      it('should format 59s', () => {
        expect(formatDuration(59000)).to.equal('59s');
      });

      it('should format 1min', () => {
        expect(formatDuration(60000)).to.equal('1min');
      });

      it('should format 1min', () => {
        expect(formatDuration(62757)).to.equal('1min');
      });

      it('should format 4min', () => {
        expect(formatDuration(224567)).to.equal('4min');
      });

      it('should format 80min', () => {
        expect(formatDuration(80 * 60 * 1000)).to.equal('80min');
      });
    });
  });
});