Skip to content

Commit

Permalink
Test output of printPlainSummary() to confirm jest works
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindrics committed Jun 11, 2024
1 parent a9ed0b8 commit 92b3748
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/printers/text.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { TotalCosts } from '../cost';
import { hideSpinner } from '../logger';
import { printPlainSummary } from './text';

describe('printPlainSummary', () => {
const mockConsoleClear = jest.spyOn(console, 'clear').mockImplementation();
const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();

afterEach(() => {
jest.clearAllMocks();
});

it('should print the plain summary correctly', () => {
const accountAlias = 'testAccount';
const costs: TotalCosts = {
totals: {
lastMonth: 123.456,
thisMonth: 678.901,
last7Days: 45.678,
yesterday: 12.345,
},
totalsByService: {
lastMonth: { service1: 654.321 },
thisMonth: { service1: 109.876 },
last7Days: { service1: 87.654 },
yesterday: { service1: 54.321 },
},
};

printPlainSummary(accountAlias, costs);

expect(mockConsoleClear).toHaveBeenCalled();
expect(mockConsoleLog).toHaveBeenNthCalledWith(1, '');
expect(mockConsoleLog).toHaveBeenNthCalledWith(2, `Account: ${accountAlias}`);
expect(mockConsoleLog).toHaveBeenNthCalledWith(3, '');
expect(mockConsoleLog).toHaveBeenNthCalledWith(4, 'Totals:');
expect(mockConsoleLog).toHaveBeenNthCalledWith(5, ` Last Month: $${costs.totals.lastMonth.toFixed(2)}`);
expect(mockConsoleLog).toHaveBeenNthCalledWith(6, ` This Month: $${costs.totals.thisMonth.toFixed(2)}`);
expect(mockConsoleLog).toHaveBeenNthCalledWith(7, ` Last 7 Days: $${costs.totals.last7Days.toFixed(2)}`);
expect(mockConsoleLog).toHaveBeenNthCalledWith(8, ` Yesterday: $${costs.totals.yesterday.toFixed(2)}`);
});
});
4 changes: 4 additions & 0 deletions src/printers/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ export function printPlainText(accountAlias: string, totals: TotalCosts, isSumma
console.log(` ${service}: $${serviceTotals.yesterday[service].toFixed(2)}`);
});
}

if (process.env.NODE_ENV === 'test') {
module.exports = { printPlainSummary };
}

0 comments on commit 92b3748

Please sign in to comment.