-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.utils.spec.ts
More file actions
125 lines (93 loc) · 2.94 KB
/
input.utils.spec.ts
File metadata and controls
125 lines (93 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import inquirer from 'inquirer';
import type { Mock } from 'vitest';
import { InputUtils } from './input.utils';
import {
InputPromptDto,
InputTypeEnum,
InputUtilsCustomChoiceEnum,
} from '@/interfaces';
vi.mock('inquirer');
const makeSut = () => {
const inputUtils = new InputUtils();
return { sut: inputUtils };
};
class SeparatorMock {
constructor() {
return '';
}
}
describe('InputUtils', () => {
beforeEach(() => {
vi.clearAllMocks();
(inquirer.Separator as unknown as Mock).mockReturnValueOnce(SeparatorMock);
});
describe('prompt', () => {
it('should prompt for user input', async () => {
const { sut } = makeSut();
const input: InputPromptDto = {
type: InputTypeEnum.Input,
message: 'Enter your name:',
};
const expectedData = 'John Doe';
(inquirer.prompt as unknown as Mock).mockResolvedValueOnce({
data: expectedData,
});
const result = await sut.prompt(input);
expect(result).toBe(expectedData);
expect(inquirer.prompt).toHaveBeenCalledWith([
{ name: 'data', ...input },
]);
});
it('should handle list input type with custom choices', async () => {
const { sut } = makeSut();
const input: InputPromptDto = {
type: InputTypeEnum.List,
message: 'Select your favorite color:',
choices: [
'Red',
'Green',
'Blue',
InputUtilsCustomChoiceEnum.Separator,
'Yellow',
],
};
const expectedData = 'Green';
(inquirer.prompt as unknown as Mock).mockResolvedValueOnce({
data: expectedData,
});
const result = await sut.prompt(input);
expect(result).toBe(expectedData);
expect(inquirer.prompt).toHaveBeenCalledWith([
{
name: 'data',
type: 'list',
message: 'Select your favorite color:',
choices: ['Red', 'Green', 'Blue', SeparatorMock, 'Yellow'],
},
]);
});
it('should exit the process if user force closes the prompt', async () => {
const { sut } = makeSut();
const input: InputPromptDto = {
type: InputTypeEnum.Input,
message: 'Enter your name:',
};
(inquirer.prompt as unknown as Mock).mockRejectedValueOnce(
new Error('User force closed the prompt with 0 null'),
);
const exitSpy = vi.spyOn(process, 'exit');
await expect(sut.prompt(input)).rejects.toThrowError();
expect(exitSpy).toHaveBeenCalledWith(0);
});
it('should rethrow any other errors', async () => {
const { sut } = makeSut();
const input: InputPromptDto = {
type: InputTypeEnum.Input,
message: 'Enter your name:',
};
const expectedError = new Error('Something went wrong');
(inquirer.prompt as unknown as Mock).mockRejectedValueOnce(expectedError);
await expect(sut.prompt(input)).rejects.toThrowError(expectedError);
});
});
});