Skip to content

Commit 5374fc7

Browse files
committed
Add the last integration test
1 parent c738cb8 commit 5374fc7

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { readPlansFromFile } from './readPlansFromFile';
2+
import { EnergyPlan } from './calculateAnnualCost';
3+
import { main } from './uswitch';
4+
import readline from 'readline/promises';
5+
6+
jest.mock('./readPlansFromFile');
7+
jest.mock('readline/promises', () => ({
8+
createInterface: jest.fn(),
9+
}));
10+
type ReadLineInterfaceMock = {
11+
prompt: jest.Mock;
12+
on: jest.Mock;
13+
close: jest.Mock;
14+
};
15+
describe('uswitch integration', () => {
16+
let originalArgv;
17+
let mockPlans: EnergyPlan[];
18+
let mockConsoleLog: jest.SpyInstance;
19+
let mockConsoleError: jest.SpyInstance;
20+
let mockConsoleDebug: jest.SpyInstance;
21+
let mockProcessExit: jest.SpyInstance;
22+
let mockReadlineInterface: ReadLineInterfaceMock;
23+
24+
beforeEach(() => {
25+
jest.clearAllMocks();
26+
27+
mockPlans = [
28+
{
29+
supplier: 'sse',
30+
plan: 'standard',
31+
rates: [{ price: 13.5, threshold: 150 }, { price: 11.1, threshold: 100 }, { price: 10 }],
32+
standing_charge: 9,
33+
} as EnergyPlan,
34+
];
35+
36+
mockReadlineInterface = {
37+
prompt: jest.fn(),
38+
on: jest.fn(),
39+
close: jest.fn(),
40+
};
41+
42+
(readline.createInterface as jest.Mock).mockReturnValue(mockReadlineInterface);
43+
44+
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();
45+
mockConsoleError = jest.spyOn(console, 'error').mockImplementation();
46+
mockConsoleDebug = jest.spyOn(console, 'debug').mockImplementation();
47+
mockProcessExit = jest.spyOn(process, 'exit').mockImplementation();
48+
49+
(readPlansFromFile as jest.Mock).mockResolvedValue(mockPlans);
50+
originalArgv = process.argv;
51+
});
52+
53+
afterEach(() => {
54+
process.argv = originalArgv;
55+
});
56+
57+
test('should exit if no file argument is provided', async () => {
58+
process.argv = ['node', 'uswitch.ts'];
59+
60+
await main();
61+
62+
expect(mockConsoleError).toHaveBeenCalledWith('Usage: uswitch plans.json');
63+
expect(mockProcessExit).toHaveBeenCalledWith(1);
64+
});
65+
66+
describe('and when tring to add a price or cancelling', () => {
67+
let lineHandler: ((line: string) => void) | undefined;
68+
let closeHandler: (() => void) | undefined;
69+
70+
beforeEach(() => {
71+
process.argv = ['node', 'uswitch.ts', 'plans.json'];
72+
mockReadlineInterface.on.mockImplementation((event: string, handler: unknown) => {
73+
if (event === 'line') lineHandler = handler as (line: string) => void;
74+
});
75+
mockReadlineInterface.on.mockImplementation((event: string, handler: unknown) => {
76+
if (event === 'line') lineHandler = handler as (line: string) => void;
77+
if (event === 'close') closeHandler = handler as () => void;
78+
});
79+
});
80+
81+
test('should read plans and handle user input', async () => {
82+
await main();
83+
84+
expect(readPlansFromFile).toHaveBeenCalledWith('plans.json');
85+
lineHandler('price 1000');
86+
expect(mockConsoleLog).toHaveBeenCalledWith('sse,standard,146.16');
87+
});
88+
89+
test('should read plans and close', async () => {
90+
await main();
91+
92+
expect(readPlansFromFile).toHaveBeenCalledWith('plans.json');
93+
lineHandler('exit');
94+
if (closeHandler) {
95+
closeHandler();
96+
expect(mockConsoleDebug).toHaveBeenCalledWith('Well done!');
97+
}
98+
});
99+
});
100+
});

test-pnpm/uswitch/uswitch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { commandHandler } from './commandHandler';
33
import { readPlansFromFile } from './readPlansFromFile';
44
import readline from 'readline/promises';
55

6-
async function main() {
6+
export async function main() {
77
const file = process.argv[2];
88
if (!file) {
99
console.error('Usage: uswitch plans.json');
@@ -26,7 +26,10 @@ async function main() {
2626
// }
2727
}
2828

29-
main();
29+
// Only run main if this file is being run directly
30+
if (require.main === module) {
31+
main();
32+
}
3033

3134
function promptUserForPrices(rl: readline.Interface, plans: EnergyPlan[]): void {
3235
rl.prompt();

0 commit comments

Comments
 (0)