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
126
127
128
129
130
131
132
|
- import readline from 'readline';
- import { createReadlineInterface, promptUser, promptYesNo } from '../../src/util/readline';
- jest.mock('readline');
- jest.mock('../../src/util/readline', () => ({
- createReadlineInterface: jest.fn(),
- promptUser: jest.fn(),
- promptYesNo: jest.fn(),
- }));
- describe('readline utils', () => {
- let mockInterface: any;
- beforeEach(() => {
- mockInterface = {
- question: jest.fn(),
- close: jest.fn(),
- on: jest.fn(),
- };
- jest.mocked(readline.createInterface).mockReturnValue(mockInterface);
- jest.mocked(createReadlineInterface).mockReturnValue(mockInterface);
- jest.mocked(promptUser).mockReset();
- jest.mocked(promptYesNo).mockReset();
- });
- afterEach(() => {
- jest.resetAllMocks();
- });
- afterAll(() => {
- jest.restoreAllMocks();
- });
- describe('createReadlineInterface', () => {
- it('should create readline interface with stdin/stdout', () => {
- const result = createReadlineInterface();
- expect(createReadlineInterface).toHaveBeenCalledWith();
- expect(result).toBe(mockInterface);
- });
- });
- describe('promptUser', () => {
- it('should resolve with user answer', async () => {
- const question = 'Test question?';
- const answer = 'Test answer';
- jest.mocked(promptUser).mockResolvedValue(answer);
- const result = await promptUser(question);
- expect(result).toBe(answer);
- expect(promptUser).toHaveBeenCalledWith(question);
- });
- it('should reject on error', async () => {
- const error = new Error('Test error');
- jest.mocked(promptUser).mockRejectedValue(error);
- await expect(promptUser('Test question?')).rejects.toThrow(error);
- });
- it('should reject if readline creation fails', async () => {
- const error = new Error('Creation failed');
- jest.mocked(promptUser).mockRejectedValue(error);
- await expect(promptUser('Test question?')).rejects.toThrow(error);
- });
- });
- describe('promptYesNo', () => {
- it('should return true for "y" with default no', async () => {
- jest.mocked(promptYesNo).mockResolvedValue(true);
- const result = await promptYesNo('Test question?', false);
- expect(result).toBe(true);
- expect(promptYesNo).toHaveBeenCalledWith('Test question?', false);
- });
- it('should return false for "n" with default yes', async () => {
- jest.mocked(promptYesNo).mockResolvedValue(false);
- const result = await promptYesNo('Test question?', true);
- expect(result).toBe(false);
- expect(promptYesNo).toHaveBeenCalledWith('Test question?', true);
- });
- it('should return default value for empty response', async () => {
- jest.mocked(promptYesNo).mockResolvedValueOnce(true).mockResolvedValueOnce(false);
- await expect(promptYesNo('Test question?', true)).resolves.toBe(true);
- await expect(promptYesNo('Test question?', false)).resolves.toBe(false);
- });
- it('should handle different case inputs', async () => {
- jest.mocked(promptYesNo).mockResolvedValueOnce(true).mockResolvedValueOnce(false);
- await expect(promptYesNo('Test question?')).resolves.toBe(true);
- await expect(promptYesNo('Test question?', true)).resolves.toBe(false);
- });
- it('should append correct suffix based on default value', async () => {
- jest.mocked(promptYesNo).mockResolvedValue(true);
- await promptYesNo('Test question?', true);
- expect(promptYesNo).toHaveBeenCalledWith('Test question?', true);
- await promptYesNo('Test question?', false);
- expect(promptYesNo).toHaveBeenCalledWith('Test question?', false);
- });
- it('should return true for non-n input with defaultYes true', async () => {
- jest.mocked(promptYesNo).mockResolvedValue(true);
- const result = await promptYesNo('Test question?', true);
- expect(result).toBe(true);
- expect(promptYesNo).toHaveBeenCalledWith('Test question?', true);
- });
- it('should return false for input not starting with y with defaultYes false', async () => {
- jest.mocked(promptYesNo).mockResolvedValue(false);
- const result = await promptYesNo('Test question?', false);
- expect(result).toBe(false);
- expect(promptYesNo).toHaveBeenCalledWith('Test question?', false);
- });
- });
- });
|