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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/* eslint no-unused-expressions: 0 */
import React from 'react/addons';
import App from '../../src/main/js/apps/background-tasks/app';
import Header from '../../src/main/js/apps/background-tasks/header';
import Stats from '../../src/main/js/apps/background-tasks/stats';
import Search from '../../src/main/js/apps/background-tasks/search';
import Tasks from '../../src/main/js/apps/background-tasks/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 TestUtils = React.addons.TestUtils;
let chai = require('chai');
let expect = chai.expect;
let sinon = require('sinon');
chai.use(require('sinon-chai'));
describe('Background Tasks', function () {
describe('App', () => {
it('should have #start()', () => {
expect(App.start).to.be.a('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('Header', () => {
it('should render', () => {
let component = TestUtils.renderIntoDocument(<Header/>),
header = TestUtils.scryRenderedDOMComponentsWithTag(component, 'header');
expect(header.length).to.equal(1);
});
});
describe('Search', () => {
it('should render search form', () => {
let spy = sinon.spy();
let component = TestUtils.renderIntoDocument(<Search options={{}}
onStatusChange={spy}
onCurrentsChange={spy}
onDateChange={spy}/>),
searchBox = TestUtils.scryRenderedDOMComponentsWithClass(component, 'search-box');
expect(searchBox).to.have.length(1);
});
it('should not render search form', () => {
let spy = sinon.spy();
let component = TestUtils.renderIntoDocument(<Search options={{ componentId: 'ABCD' }}
onStatusChange={spy}
onCurrentsChange={spy}
onDateChange={spy}/>),
searchBox = TestUtils.scryRenderedDOMComponentsWithClass(component, 'search-box');
expect(searchBox).to.be.empty;
});
it('should search', (done) => {
let spy = sinon.spy(),
searchSpy = sinon.spy();
let component = TestUtils.renderIntoDocument(<Search options={{}}
onStatusChange={spy}
onCurrentsChange={spy}
onDateChange={spy}
onSearch={searchSpy}/>);
let searchInput = React.findDOMNode(TestUtils.findRenderedDOMComponentWithClass(component, 'search-box-input'));
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 spy = sinon.spy(),
reloadSpy = sinon.spy();
let component = TestUtils.renderIntoDocument(<Search options={{}}
onStatusChange={spy}
onCurrentsChange={spy}
onDateChange={spy}
refresh={reloadSpy}/>);
let reloadButton = React.findDOMNode(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 = React.findDOMNode(result.refs.pendingCount);
expect(pendingCounter.textContent).to.contain('0');
});
it('should show 5 pending', () => {
let result = TestUtils.renderIntoDocument(<Stats pendingCount="5"/>),
pendingCounter = React.findDOMNode(result.refs.pendingCount);
expect(pendingCounter.textContent).to.contain('5');
});
it('should not show cancel pending button', () => {
let result = TestUtils.renderIntoDocument(<Stats pendingCount="0"/>),
cancelPending = React.findDOMNode(result.refs.cancelPending);
expect(cancelPending).to.not.be.ok;
});
it('should show cancel pending button', () => {
let result = TestUtils.renderIntoDocument(<Stats pendingCount="5"/>),
cancelPending = React.findDOMNode(result.refs.cancelPending);
expect(cancelPending).to.be.ok;
});
it('should trigger cancelling pending', () => {
let spy = sinon.spy();
let result = TestUtils.renderIntoDocument(<Stats pendingCount="5" cancelPending={spy}/>),
cancelPending = React.findDOMNode(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 failuresCount="0"/>),
failureCounter = React.findDOMNode(result.refs.failureCount);
expect(failureCounter.textContent).to.contain('0');
});
it('should show 5 failures', () => {
let result = TestUtils.renderIntoDocument(<Stats failuresCount="5"/>),
failureCounter = React.findDOMNode(result.refs.failureCount);
expect(failureCounter.textContent).to.contain('5');
});
it('should not show link to failures', () => {
let result = TestUtils.renderIntoDocument(<Stats failuresCount="0"/>),
failureCounter = React.findDOMNode(result.refs.failureCount);
expect(failureCounter.tagName.toLowerCase()).to.not.equal('a');
});
it('should show link to failures', () => {
let result = TestUtils.renderIntoDocument(<Stats failuresCount="5"/>),
failureCounter = React.findDOMNode(result.refs.failureCount);
expect(failureCounter.tagName.toLowerCase()).to.equal('a');
});
it('should trigger filtering failures', () => {
let spy = sinon.spy();
let result = TestUtils.renderIntoDocument(<Stats failuresCount="5" showFailures={spy}/>),
failureCounter = React.findDOMNode(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 = React.findDOMNode(result.refs.inProgressDuration);
expect(inProgressDuration.textContent).to.include('173ms');
});
it('should format duration', () => {
let result = TestUtils.renderIntoDocument(<Stats inProgressDuration="1073"/>),
inProgressDuration = React.findDOMNode(result.refs.inProgressDuration);
expect(inProgressDuration.textContent).to.include('1s');
});
it('should not show duration', () => {
let result = TestUtils.renderIntoDocument(<Stats/>),
inProgressDuration = React.findDOMNode(result.refs.inProgressDuration);
expect(inProgressDuration).to.not.be.ok;
});
});
});
describe('Tasks', () => {
it('should show list', () => {
let tasks = [
{ id: 'a' },
{ id: 'b' },
{ id: 'c' }
];
let result = TestUtils.renderIntoDocument(<Tasks tasks={tasks}/>);
expect(TestUtils.scryRenderedDOMComponentsWithTag(result, 'tr')).to.have.length(3 + /* table header */ 1);
});
});
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');
});
});
});
});
|