Skip to content

Commit 163791f

Browse files
committed
Some changes acc to inquire compatibility
1 parent 04ff80a commit 163791f

File tree

8 files changed

+98
-65
lines changed

8 files changed

+98
-65
lines changed

setup.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import { askAndUpdateTalawaApiUrl } from './src/setup/askForDocker/askForDocker'
1414
// Ask and set up reCAPTCHA
1515
const askAndSetRecaptcha = async (): Promise<void> => {
1616
try {
17-
const { shouldUseRecaptcha } = await inquirer.prompt({
18-
type: 'confirm',
19-
name: 'shouldUseRecaptcha',
20-
message: 'Would you like to set up reCAPTCHA?',
21-
default: true,
22-
});
17+
const { shouldUseRecaptcha } = await inquirer.prompt([
18+
{
19+
type: 'confirm',
20+
name: 'shouldUseRecaptcha',
21+
message: 'Would you like to set up reCAPTCHA?',
22+
default: true,
23+
},
24+
]);
2325

2426
if (shouldUseRecaptcha) {
2527
const { recaptchaSiteKeyInput } = await inquirer.prompt([

src/setup/askAndSetDockerOption/askAndSetDockerOption.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22

33
// Mock modules
4-
vi.mock('inquirer', () => ({
5-
default: {
6-
prompt: vi.fn(),
7-
},
8-
}));
4+
vi.mock('inquirer', async () => {
5+
const actual = await vi.importActual('inquirer');
6+
return {
7+
default: {
8+
...actual,
9+
prompt: vi.fn(),
10+
},
11+
};
12+
});
913

1014
vi.mock('setup/updateEnvFile/updateEnvFile', () => ({
1115
default: vi.fn(),

src/setup/askAndSetDockerOption/askAndSetDockerOption.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { askForDocker } from 'setup/askForDocker/askForDocker';
44

55
// Function to manage Docker setup
66
const askAndSetDockerOption = async (): Promise<void> => {
7-
const { useDocker } = await inquirer.prompt({
8-
type: 'confirm',
9-
name: 'useDocker',
10-
message: 'Would you like to set up with Docker?',
11-
default: false,
12-
});
7+
const { useDocker } = await inquirer.prompt([
8+
{
9+
type: 'confirm',
10+
name: 'useDocker',
11+
message: 'Would you like to set up with Docker?',
12+
default: false,
13+
},
14+
]);
1315

1416
if (useDocker) {
1517
console.log('Setting up with Docker...');

src/setup/askAndUpdatePort/askAndUpdatePort.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import inquirer from 'inquirer';
44

55
// Ask and update the custom port
66
const askAndUpdatePort = async (): Promise<void> => {
7-
const { shouldSetCustomPortResponse } = await inquirer.prompt({
8-
type: 'confirm',
9-
name: 'shouldSetCustomPortResponse',
10-
message:
11-
'Would you like to set up a custom port for running Talawa Admin without Docker?',
12-
default: true,
13-
});
7+
const { shouldSetCustomPortResponse } = await inquirer.prompt([
8+
{
9+
type: 'confirm',
10+
name: 'shouldSetCustomPortResponse',
11+
message:
12+
'Would you like to set up a custom port for running Talawa Admin without Docker?',
13+
default: true,
14+
},
15+
]);
1416

1517
if (shouldSetCustomPortResponse) {
1618
const customPort = await askForCustomPort();

src/setup/askAndUpdatePort/askForUpdatePort.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ import inquirer from 'inquirer';
66

77
vi.mock('setup/askForCustomPort/askForCustomPort');
88
vi.mock('setup/updateEnvFile/updateEnvFile');
9-
vi.mock('inquirer');
9+
// Fix Inquirer mock for v12+
10+
vi.mock('inquirer', async () => {
11+
const actual = await vi.importActual('inquirer');
12+
return {
13+
default: {
14+
...actual,
15+
prompt: vi.fn(),
16+
},
17+
};
18+
});
1019

1120
describe('askAndUpdatePort', () => {
1221
afterEach(() => {

src/setup/askForCustomPort/askForCustomPort.spec.ts

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import inquirer from 'inquirer';
33
import { askForCustomPort, validatePort } from './askForCustomPort';
44

5-
vi.mock('inquirer');
5+
vi.mock('inquirer', async () => {
6+
const actual = await vi.importActual('inquirer');
7+
return {
8+
default: {
9+
...actual,
10+
prompt: vi.fn(),
11+
},
12+
};
13+
});
614

715
describe('askForCustomPort', () => {
816
beforeEach(() => {
@@ -11,18 +19,20 @@ describe('askForCustomPort', () => {
1119

1220
describe('basic port validation', () => {
1321
it('should return default port if user provides no input', async () => {
14-
vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
15-
customPort: '4321',
16-
});
22+
vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce([
23+
{ customPort: '4321' },
24+
]);
1725

1826
const result = await askForCustomPort();
1927
expect(result).toBe(4321);
2028
});
2129

2230
it('should return user-provided port', async () => {
23-
vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
24-
customPort: '8080',
25-
});
31+
vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce([
32+
{
33+
customPort: '8080',
34+
},
35+
]);
2636

2737
const result = await askForCustomPort();
2838
expect(result).toBe(8080);
@@ -44,21 +54,21 @@ describe('askForCustomPort', () => {
4454
describe('retry mechanism', () => {
4555
it('should handle invalid port input and prompt again', async () => {
4656
vi.spyOn(inquirer, 'prompt')
47-
.mockResolvedValueOnce({ customPort: 'abcd' })
48-
.mockResolvedValueOnce({ customPort: '8080' });
57+
.mockResolvedValueOnce([{ customPort: 'abcd' }])
58+
.mockResolvedValueOnce([{ customPort: '8080' }]);
4959

5060
const result = await askForCustomPort();
5161
expect(result).toBe(8080);
5262
});
5363

5464
it('should return default port after maximum retry attempts', async () => {
5565
vi.spyOn(inquirer, 'prompt')
56-
.mockResolvedValueOnce({ customPort: 'invalid-port-attempt1' })
57-
.mockResolvedValueOnce({ customPort: 'invalid-port-attempt2' })
58-
.mockResolvedValueOnce({ customPort: 'invalid-port-attempt3' })
59-
.mockResolvedValueOnce({ customPort: 'invalid-port-attempt4' })
60-
.mockResolvedValueOnce({ customPort: 'invalid-port-attempt5' })
61-
.mockResolvedValueOnce({ customPort: 'invalid-port-attempt6' });
66+
.mockResolvedValueOnce([{ customPort: 'invalid-port-attempt1' }])
67+
.mockResolvedValueOnce([{ customPort: 'invalid-port-attempt2' }])
68+
.mockResolvedValueOnce([{ customPort: 'invalid-port-attempt3' }])
69+
.mockResolvedValueOnce([{ customPort: 'invalid-port-attempt4' }])
70+
.mockResolvedValueOnce([{ customPort: 'invalid-port-attempt5' }])
71+
.mockResolvedValueOnce([{ customPort: 'invalid-port-attempt6' }]);
6272

6373
const result = await askForCustomPort();
6474
expect(result).toBe(4321);
@@ -68,36 +78,36 @@ describe('askForCustomPort', () => {
6878
describe('reserved ports', () => {
6979
it('should return user-provided port after confirming reserved port', async () => {
7080
vi.spyOn(inquirer, 'prompt')
71-
.mockResolvedValueOnce({ customPort: '80' })
72-
.mockResolvedValueOnce({ confirmPort: true });
81+
.mockResolvedValueOnce([{ customPort: '80' }])
82+
.mockResolvedValueOnce([{ confirmPort: true }]);
7383

7484
const result = await askForCustomPort();
7585
expect(result).toBe(80);
7686
});
7787

7888
it('should re-prompt user for port if reserved port confirmation is denied', async () => {
7989
vi.spyOn(inquirer, 'prompt')
80-
.mockResolvedValueOnce({ customPort: '80' })
81-
.mockResolvedValueOnce({ confirmPort: false })
82-
.mockResolvedValueOnce({ customPort: '8080' });
90+
.mockResolvedValueOnce([{ customPort: '80' }])
91+
.mockResolvedValueOnce([{ confirmPort: false }])
92+
.mockResolvedValueOnce([{ customPort: '8080' }]);
8393

8494
const result = await askForCustomPort();
8595
expect(result).toBe(8080);
8696
});
8797

8898
it('should return default port if reserved port confirmation is denied after maximum retry attempts', async () => {
8999
vi.spyOn(inquirer, 'prompt')
90-
.mockResolvedValueOnce({ customPort: '80' })
91-
.mockResolvedValueOnce({ confirmPort: false })
92-
.mockResolvedValueOnce({ customPort: '80' })
93-
.mockResolvedValueOnce({ confirmPort: false })
94-
.mockResolvedValueOnce({ customPort: '80' })
95-
.mockResolvedValueOnce({ confirmPort: false })
96-
.mockResolvedValueOnce({ customPort: '80' })
97-
.mockResolvedValueOnce({ confirmPort: false })
98-
.mockResolvedValueOnce({ customPort: '80' })
99-
.mockResolvedValueOnce({ confirmPort: false })
100-
.mockResolvedValueOnce({ customPort: '80' });
100+
.mockResolvedValueOnce([{ customPort: '80' }])
101+
.mockResolvedValueOnce([{ confirmPort: false }])
102+
.mockResolvedValueOnce([{ customPort: '80' }])
103+
.mockResolvedValueOnce([{ confirmPort: false }])
104+
.mockResolvedValueOnce([{ customPort: '80' }])
105+
.mockResolvedValueOnce([{ confirmPort: false }])
106+
.mockResolvedValueOnce([{ customPort: '80' }])
107+
.mockResolvedValueOnce([{ confirmPort: false }])
108+
.mockResolvedValueOnce([{ customPort: '80' }])
109+
.mockResolvedValueOnce([{ confirmPort: false }])
110+
.mockResolvedValueOnce([{ customPort: '80' }]);
101111

102112
const result = await askForCustomPort();
103113
expect(result).toBe(4321);

src/setup/askForCustomPort/askForCustomPort.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function validatePort(input: string): string | boolean {
1717
}
1818

1919
export async function reservedPortWarning(port: number): Promise<boolean> {
20-
const { confirmPort } = await inquirer.prompt<{ confirmPort: boolean }>([
20+
const answer = await inquirer.prompt([
2121
{
2222
type: 'confirm',
2323
name: 'confirmPort',
@@ -26,14 +26,14 @@ export async function reservedPortWarning(port: number): Promise<boolean> {
2626
},
2727
]);
2828

29-
return confirmPort;
29+
return answer.confirmPort;
3030
}
3131

3232
export async function askForCustomPort(): Promise<number> {
3333
let remainingAttempts = MAX_RETRY_ATTEMPTS;
3434

3535
while (remainingAttempts--) {
36-
const { customPort } = await inquirer.prompt<{ customPort: string }>([
36+
const answer = await inquirer.prompt([
3737
{
3838
type: 'input',
3939
name: 'customPort',
@@ -43,6 +43,8 @@ export async function askForCustomPort(): Promise<number> {
4343
},
4444
]);
4545

46+
const customPort = answer.customPort;
47+
4648
if (customPort && validatePort(customPort) === true) {
4749
if (Number(customPort) >= 1024) {
4850
return Number(customPort);

src/setup/askForDocker/askForDocker.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ export const askForDocker = async (): Promise<string> => {
3232
// Function to ask and update Talawa API URL
3333
export const askAndUpdateTalawaApiUrl = async (): Promise<void> => {
3434
try {
35-
const { shouldSetTalawaApiUrlResponse } = await inquirer.prompt({
36-
type: 'confirm',
37-
name: 'shouldSetTalawaApiUrlResponse',
38-
message: 'Would you like to set up Talawa API endpoint?',
39-
default: true,
40-
});
35+
const { shouldSetTalawaApiUrlResponse } = await inquirer.prompt([
36+
{
37+
type: 'confirm',
38+
name: 'shouldSetTalawaApiUrlResponse',
39+
message: 'Would you like to set up Talawa API endpoint?',
40+
default: true,
41+
},
42+
]);
4143

4244
if (shouldSetTalawaApiUrlResponse) {
4345
let endpoint = '';

0 commit comments

Comments
 (0)