Skip to content

Commit 449f595

Browse files
committed
Fix: Markdown and CSV output do not show projects
1 parent ea24598 commit 449f595

File tree

4 files changed

+169
-5
lines changed

4 files changed

+169
-5
lines changed

spec/output/csv.spec.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const moment = require('moment');
2+
const config = require('../../src/include/config');
3+
const Report = require('../../src/models/report');
4+
const Sinon = require('sinon');
5+
const { describe } = require('mocha');
6+
const Csv = require('csv-string');
7+
const csv = require('../../src/output/csv');
8+
const { assert } = require('chai');
9+
10+
describe('csv output', () => {
11+
/**
12+
* @type {Sinon.SinonSpyStatic}
13+
*/
14+
let csvSpy;
15+
16+
beforeEach(() => {
17+
csvSpy = Sinon.spy(Csv, 'stringify')
18+
})
19+
20+
afterEach(() => {
21+
csvSpy.restore();
22+
})
23+
24+
it('renders projects if more than one', () => {
25+
const issues = [
26+
{
27+
times: [
28+
{
29+
user: "user1",
30+
project_namespace: "project1",
31+
seconds: 360,
32+
date: moment('2024-06-06 00:00:00')
33+
},
34+
{
35+
user: "user2",
36+
project_namespace: "project2",
37+
seconds: 720,
38+
date: moment('2024-06-06 00:00:00')
39+
}
40+
],
41+
stats: {time_estimate: 0, total_time_spent: 0}
42+
}
43+
];
44+
45+
const projectsMatcher = Sinon.match((value) => {
46+
return Array.isArray(value) && Array.isArray(value[0]) && value[0].includes('project1') && value[0].includes('project2')
47+
}, 'Both projects exist')
48+
49+
let reportConfig = new config();
50+
reportConfig.set('report', ['stats'])
51+
let report = new Report(reportConfig);
52+
report.issues = issues;
53+
report.mergeRequests = [];
54+
55+
let out = new csv(reportConfig, report);
56+
out.make();
57+
assert(csvSpy.calledWith(projectsMatcher), 'Not called properly')
58+
})
59+
60+
it('does not render single project', () => {
61+
const issues = [
62+
{
63+
times: [
64+
{
65+
user: "user2",
66+
project_namespace: "project2",
67+
seconds: 720,
68+
date: moment('2024-06-06 00:00:00')
69+
}
70+
],
71+
stats: {time_estimate: 0, total_time_spent: 0}
72+
}
73+
];
74+
75+
const projectsMatcher = Sinon.match((value) => {
76+
return Array.isArray(value) && Array.isArray(value[0]) && !value[0].includes('project2')
77+
}, 'Project does not exist')
78+
79+
let reportConfig = new config();
80+
reportConfig.set('report', ['stats'])
81+
let report = new Report(reportConfig);
82+
report.issues = issues;
83+
report.mergeRequests = [];
84+
85+
let out = new csv(reportConfig, report);
86+
out.make();
87+
assert(csvSpy.calledWith(projectsMatcher), 'Not called properly')
88+
})
89+
})

spec/output/markdown.spec.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const moment = require('moment');
2+
const config = require('../../src/include/config');
3+
const Report = require('../../src/models/report');
4+
const markdown = require('../../src/output/markdown');
5+
const Sinon = require('sinon');
6+
7+
describe('markdown class', () => {
8+
it('Renders projects if more than one', () => {
9+
const issues = [
10+
{
11+
times: [
12+
{
13+
user: "user1",
14+
project_namespace: "project1",
15+
seconds: 360,
16+
date: moment('2024-06-06 00:00:00')
17+
},
18+
{
19+
user: "user2",
20+
project_namespace: "project2",
21+
seconds: 720,
22+
date: moment('2024-06-06 00:00:00')
23+
}
24+
],
25+
stats: {time_estimate: 0, total_time_spent: 0}
26+
}
27+
];
28+
29+
const projectsMatcher = Sinon.match('project1').and(Sinon.match('project2'))
30+
let reportConfig = new config();
31+
reportConfig.set('report', ['stats'])
32+
let report = new Report(reportConfig);
33+
report.issues = issues;
34+
report.mergeRequests = [];
35+
36+
let out = new markdown(reportConfig, report);
37+
let mdMock = Sinon.mock(out);
38+
mdMock.expects('headline').once();
39+
mdMock.expects('write').once().withArgs(projectsMatcher);
40+
out.make();
41+
});
42+
43+
it('Does not render projects if one project only', () => {
44+
const issues = [
45+
{
46+
times: [
47+
{
48+
user: "user2",
49+
project_namespace: "project2",
50+
seconds: 720,
51+
date: moment('2024-06-06 00:00:00')
52+
}
53+
],
54+
stats: {time_estimate: 0, total_time_spent: 0}
55+
}
56+
];
57+
58+
const projectsMatcher = Sinon.match(function(value) {
59+
return ((typeof(value) === 'string') && (/project2/.test(value) === false));
60+
})
61+
62+
let reportConfig = new config();
63+
reportConfig.set('report', ['stats'])
64+
let report = new Report(reportConfig);
65+
report.issues = issues;
66+
report.mergeRequests = [];
67+
68+
let out = new markdown(reportConfig, report);
69+
let mdMock = Sinon.mock(out);
70+
mdMock.expects('headline').once();
71+
mdMock.expects('write').once().withArgs(projectsMatcher);
72+
out.make();
73+
});
74+
});
75+

src/output/csv.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class csv extends Base {
1717
stats[1].push(time);
1818
});
1919

20-
if (this.projects.length > 1) {
20+
if (Object.keys(this.projects).length > 1) {
2121
_.each(this.projects, (time, name) => {
2222
stats[0].push(name);
2323
stats[1].push(time);
@@ -101,4 +101,4 @@ class csv extends Base {
101101
}
102102
}
103103

104-
module.exports = csv;
104+
module.exports = csv;

src/output/markdown.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class markdown extends Base {
2525
_.each(this.stats, (time, name) => stats += `\n* **${name}**: ${time}`);
2626
stats += `\n`;
2727

28-
if (this.projects.length > 1) {
29-
_.each(this.projects, (time, name) => stats += `\n* **${name.red}**: ${time}`);
28+
if (Object.keys(this.projects).length > 1) {
29+
_.each(this.projects, (time, name) => stats += `\n* **${name}**: ${time}`);
3030
stats += `\n`;
3131
}
3232

@@ -69,4 +69,4 @@ class markdown extends Base {
6969
}
7070
}
7171

72-
module.exports = markdown;
72+
module.exports = markdown;

0 commit comments

Comments
 (0)