Skip to content

Commit 6e00315

Browse files
committed
finish testing main
1 parent f46cf10 commit 6e00315

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { printFestiveTitle } from './festive.js';
1818
*/
1919
export const intParser = (choices) => (value) => {
2020
const parsed = Number.parseInt(value, 10);
21+
if (Number.isNaN(parsed)) {
22+
throw new InvalidArgumentError('Value could not be parsed as an integer.');
23+
}
2124
if (!choices.includes(parsed)) {
2225
const min = Math.min(...choices);
2326
const max = Math.max(...choices);

tests/main.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const mockCommander = (() => {
4949
})();
5050

5151
// import after mocks set up
52+
const { InvalidArgumentError } = await import('commander');
5253
const {
5354
authAction,
5455
initAction,
@@ -57,6 +58,7 @@ const {
5758
submitAction,
5859
handleError,
5960
} = await easyResolve(easyMocks);
61+
const { intParser } = await import('../src/main.js');
6062

6163
describe('main', () => {
6264
beforeEach(() => {
@@ -98,3 +100,25 @@ describe('main', () => {
98100
expect(handleError).toHaveBeenCalledWith(error);
99101
}));
100102
});
103+
104+
describe('argParser()', () => {
105+
test.each([null, undefined, '', false, true, {}, Promise.resolve(true)])(
106+
'throws if not parsable as int: %s',
107+
(value) => {
108+
expect(() => {
109+
intParser([1, 2, 3])(value);
110+
}).toThrow(InvalidArgumentError);
111+
}
112+
);
113+
114+
test('throws if value is not a valid choice', () => {
115+
expect(() => {
116+
intParser([1, 2, 3])('4');
117+
}).toThrow(InvalidArgumentError);
118+
});
119+
120+
test('returns integer if value is a valid choice', () => {
121+
const result = intParser([1, 2, 3])('1');
122+
expect(result).toBe(1);
123+
});
124+
});

0 commit comments

Comments
 (0)