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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { shallow } from 'enzyme';
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import Breadcrumb from '../../../src/main/js/apps/code/components/Breadcrumb';
import Breadcrumbs from '../../../src/main/js/apps/code/components/Breadcrumbs';
import Component from '../../../src/main/js/apps/code/components/Component';
import ComponentDetach from '../../../src/main/js/apps/code/components/ComponentDetach';
import ComponentMeasure from '../../../src/main/js/apps/code/components/ComponentMeasure';
import ComponentName from '../../../src/main/js/apps/code/components/ComponentName';
import Components from '../../../src/main/js/apps/code/components/Components';
import ComponentsEmpty from '../../../src/main/js/apps/code/components/ComponentsEmpty';
import Truncated from '../../../src/main/js/apps/code/components/Truncated';
import { getComponentUrl } from '../../../src/main/js/helpers/urls';
import QualifierIcon from '../../../src/main/js/components/shared/qualifier-icon';
chai.use(sinonChai);
const measures = [
{ key: 'ncloc', val: 9757 }
];
const exampleComponent = {
uuid: 'A1',
key: 'A',
name: 'AA',
qualifier: 'TRK',
msr: measures
};
const exampleComponent2 = { uuid: 'B2', key: 'B' };
const exampleComponent3 = { uuid: 'C3', key: 'C' };
const exampleOnBrowse = sinon.spy();
describe('Code :: Components', () => {
describe('<Breadcrumb/>', () => {
it('should render <ComponentName/>', () => {
const output = shallow(
<Breadcrumb
component={exampleComponent}
onBrowse={exampleOnBrowse}/>
);
expect(output.type())
.to.equal(ComponentName);
expect(output.props())
.to.deep.equal({ component: exampleComponent, onBrowse: exampleOnBrowse })
});
});
describe('<Breadcrumbs/>', () => {
let output;
let list;
before(() => {
output = shallow(
<Breadcrumbs
breadcrumbs={[exampleComponent, exampleComponent2, exampleComponent3]}
onBrowse={exampleOnBrowse}/>);
list = output.find(Breadcrumb);
});
it('should render list of <Breadcrumb/>s', () => {
expect(list)
.to.have.length(3);
expect(list.at(0).prop('component'))
.to.equal(exampleComponent);
expect(list.at(1).prop('component'))
.to.equal(exampleComponent2);
expect(list.at(2).prop('component'))
.to.equal(exampleComponent3);
});
it('should pass onBrowse to all components except the last one', () => {
expect(list.at(0).prop('onBrowse'))
.to.equal(exampleOnBrowse);
expect(list.at(1).prop('onBrowse'))
.to.equal(exampleOnBrowse);
expect(list.at(2).prop('onBrowse'))
.to.equal(null);
});
});
describe('<Component/>', () => {
let output;
before(() => {
output = shallow(
<Component
component={exampleComponent}
coverageMetric="coverage"
onBrowse={exampleOnBrowse}/>);
});
it('should render <ComponentName/>', () => {
const findings = output.find(ComponentName);
expect(findings)
.to.have.length(1);
expect(findings.first().props())
.to.deep.equal({ component: exampleComponent, onBrowse: exampleOnBrowse });
});
it('should render <ComponentMeasure/>s', () => {
const findings = output.find(ComponentMeasure);
expect(findings)
.to.have.length(5);
expect(findings.at(0).props())
.to.deep.equal({ component: exampleComponent, metricKey: 'ncloc', metricType: 'SHORT_INT' });
expect(findings.at(1).props())
.to.deep.equal({ component: exampleComponent, metricKey: 'sqale_index', metricType: 'SHORT_WORK_DUR' });
expect(findings.at(2).props())
.to.deep.equal({ component: exampleComponent, metricKey: 'violations', metricType: 'SHORT_INT' });
expect(findings.at(3).props())
.to.deep.equal({ component: exampleComponent, metricKey: 'coverage', metricType: 'PERCENT' });
expect(findings.at(4).props())
.to.deep.equal({ component: exampleComponent, metricKey: 'duplicated_lines_density', metricType: 'PERCENT' });
});
it('should render <ComponentDetach/>', () => {
const findings = output.find(ComponentDetach);
expect(findings)
.to.have.length(1);
expect(findings.first().props())
.to.deep.equal({ component: exampleComponent });
});
});
describe('<ComponentDetach/>', () => {
it('should render link', () => {
const output = shallow(
<ComponentDetach component={exampleComponent}/>);
const expectedUrl = getComponentUrl(exampleComponent.key);
expect(output.type())
.to.equal('a');
expect(output.prop('target'))
.to.equal('_blank');
expect(output.prop('href'))
.to.equal(expectedUrl);
});
});
describe('<ComponentMeasure/>', () => {
it('should render formatted measure', () => {
const output = shallow(
<ComponentMeasure
component={exampleComponent}
metricKey="ncloc"
metricType="SHORT_INT"/>);
expect(output.text())
.to.equal('9.8k');
});
it('should not render measure', () => {
const output = shallow(
<ComponentMeasure
component={exampleComponent}
metricKey="random"
metricType="SHORT_INT"/>);
expect(output.text())
.to.equal('');
});
});
describe('<ComponentName/>', () => {
it('should render <QualifierIcon/>', () => {
const output = shallow(
<ComponentName
component={exampleComponent}
onBrowse={exampleOnBrowse}/>);
const findings = output.find(QualifierIcon);
expect(findings)
.to.have.length(1);
expect(findings.first().prop('qualifier'))
.to.equal('TRK');
});
it('should render link to component', () => {
const output = shallow(
<ComponentName
component={exampleComponent}
onBrowse={exampleOnBrowse}/>);
const findings = output.find('a');
expect(findings)
.to.have.length(1);
expect(findings.first().text())
.to.equal('AA');
});
it('should not render link to component', () => {
const output = shallow(
<ComponentName
component={exampleComponent}
onBrowse={null}/>);
const findings = output.find('span');
expect(output.find('a'))
.to.have.length(0);
expect(findings)
.to.have.length(1);
expect(findings.first().text())
.to.equal('AA');
});
it('should browse on click', () => {
const spy = sinon.spy();
const preventDefaultSpy = sinon.spy();
const output = shallow(
<ComponentName
component={exampleComponent}
onBrowse={spy}/>);
const findings = output.find('a');
findings.first().simulate('click', { preventDefault: preventDefaultSpy });
expect(preventDefaultSpy).to.have.been.called;
expect(spy).to.have.been.calledWith(exampleComponent);
});
});
describe('<Components/>', () => {
let output;
before(() => {
output = shallow(
<Components
baseComponent={exampleComponent}
components={[exampleComponent2, exampleComponent3]}
onBrowse={exampleOnBrowse}/>);
});
it('should render base component', () => {
const findings = output.findWhere(node => {
return node.type() === Component && node.prop('component') === exampleComponent;
});
expect(findings)
.to.have.length(1);
expect(findings.first().prop('onBrowse'))
.to.not.be.ok;
});
it('should render children component', () => {
const findings = output.findWhere(node => {
return node.type() === Component && node.prop('component') !== exampleComponent;
});
expect(findings)
.to.have.length(2);
expect(findings.at(0).prop('onBrowse'))
.to.equal(exampleOnBrowse)
});
});
describe('<ComponentsEmpty/>', () => {
it('should render', () => {
const output = shallow(<ComponentsEmpty/>);
expect(output.text())
.to.include('no_results');
});
});
describe('<Truncated/>', () => {
it('should render and set title', () => {
const output = shallow(<Truncated title="ABC">123</Truncated>);
expect(output.type())
.to.equal('span');
expect(output.text())
.to.equal('123');
expect(output.prop('data-title'))
.to.equal('ABC');
});
});
});
|