-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathprompt.test.js
86 lines (76 loc) · 2.83 KB
/
prompt.test.js
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
const inquirer = require('inquirer');
const {
validateAccountSid,
promptForAccountDetails,
promptForProjectName,
} = require('../src/create-twilio-function/prompt');
console.log = jest.fn();
describe('accountSid validation', () => {
test('an accountSid should start with "AC"', () => {
expect(validateAccountSid('AC123')).toBe(true);
});
test('an accountSid can be left blank', () => {
expect(validateAccountSid('')).toBe(true);
});
test('an accountSid should not begin with anything but "AC"', () => {
expect(validateAccountSid('blah')).toEqual('An Account SID starts with "AC".');
});
});
describe('promptForAccountDetails', () => {
test('should ask for an accountSid if not specified', async () => {
inquirer.prompt = jest.fn(() =>
Promise.resolve({
accountSid: 'AC1234',
authToken: 'test-auth-token',
}),
);
await promptForAccountDetails({ name: 'function-test' });
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(expect.any(String));
});
test('should ask for an auth if not specified', async () => {
inquirer.prompt = jest.fn(() => Promise.resolve({ authToken: 'test-auth-token' }));
await promptForAccountDetails({
name: 'function-test',
accountSid: 'AC1234',
});
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(expect.any(String));
});
test('should not prompt if account sid and auth token specified', async () => {
inquirer.prompt = jest.fn(() =>
Promise.resolve({
accountSid: 'AC1234',
authToken: 'test-auth-token',
}),
);
await promptForAccountDetails({
name: 'function-test',
accountSid: 'AC5678',
authToken: 'other-test-token',
});
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
expect(inquirer.prompt).toHaveBeenCalledWith([]);
expect(console.log).not.toHaveBeenCalled();
});
test('should not ask for credentials if skip-credentials flag is true', async () => {
inquirer.prompt = jest.fn(() => {
return 0;
});
await promptForAccountDetails({ skipCredentials: true });
expect(inquirer.prompt).not.toHaveBeenCalled();
expect(console.log).not.toHaveBeenCalled();
});
});
describe('promptForProjectName', () => {
test('should ask for a project name', async () => {
inquirer.prompt = jest.fn(() => Promise.resolve({ name: 'test-name' }));
await promptForProjectName(['must be valid']);
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
});
});