Skip to content

Commit

Permalink
fix: output total number of commits in human readable form
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Dec 13, 2024
1 parent 78c2a91 commit 66b1a6f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
57 changes: 56 additions & 1 deletion libs/report/committers/src/lib/committers-monthly.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parseMonthly } from './parse-monthly';
import { format } from 'date-fns';
import { outputMonthlyCommitters, parseMonthly } from './parse-monthly';
import { MonthlyData } from './types';

describe('parseMonthly', () => {
const sampleCommits = [
Expand Down Expand Up @@ -92,3 +94,56 @@ describe('parseMonthly', () => {
expect(result).toEqual([]);
});
});

describe('outputMonthlyCommitters', () => {
const consoleTableSpy = jest.spyOn(console, 'table').mockImplementation();

afterEach(() => {
consoleTableSpy.mockClear();
});

afterAll(() => {
consoleTableSpy.mockRestore();
});

it('should format and display the monthly commit data correctly', () => {
const exampleCommits = [
{
month: 'January 2024',
start: new Date('2024-01-01'),
end: new Date('2024-01-31'),
committers: {
Marco: 5,
George: 3,
},
},
{
month: 'February 2024',
start: new Date('2024-02-01'),
end: new Date('2024-02-28'),
committers: {
Greg: 7,
},
},
] as MonthlyData[];

const expectedOutput = [
{
month: 'January 2024',
start: format(new Date('2024-01-01'), 'yyyy-MM-dd'),
end: format(new Date('2024-01-31'), 'yyyy-MM-dd'),
totalCommits: 8,
},
{
month: 'February 2024',
start: format(new Date('2024-02-01'), 'yyyy-MM-dd'),
end: format(new Date('2024-02-28'), 'yyyy-MM-dd'),
totalCommits: 7,
},
];

outputMonthlyCommitters(exampleCommits);
expect(consoleTableSpy).toHaveBeenCalledTimes(1);
expect(consoleTableSpy).toHaveBeenCalledWith(expectedOutput);
});
});
4 changes: 3 additions & 1 deletion libs/report/committers/src/lib/parse-monthly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export function parseMonthly(startDate: Date, endDate: Date, entries: Commit[])
}

function monthlyDataHumanReadable(data: MonthlyData) {
const totalCommits = Object.values(data.committers).reduce((sum, count) => sum + count, 0);
return {
...data,
month: data.month,
start: format(data.start, 'yyyy-MM-dd'),
end: format(data.end, 'yyyy-MM-dd'),
totalCommits,
};
}

Expand Down

0 comments on commit 66b1a6f

Please sign in to comment.