Skip to content

Commit 19aed8f

Browse files
author
Romain Gagnaire
committed
test(interactive-mode): Covered new allowInquirerPlugins option
1 parent 8ee87a5 commit 19aed8f

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/interactive-mode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const inquirer = require('inquirer');
33
/**
44
* Initiate an interactive prompt to get values from the user.
55
* @param {object} values The values to configure the prompt
6+
* @param {object} inquirerOptions Payload encapsulating runtime configuration options for inquirer, mainly "allowInquirerPlugins", which enables support of inquirer plugins
67
* @return {object} A promise that, when fullfilled, will contain answer of the questions prompted to the user
78
*/
89
module.exports = (values = {}, inquirerOptions = {}) => {

test/interactive-mode.test.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ const inquirer = require('inquirer');
22
const interactiveMode = require('../src/interactive-mode');
33

44
describe('interactive-mode', () => {
5+
let createdInquirerPromptStub;
56
let inquirerCreatePromptModuleStub;
67
let inquirerPromptStub;
78
let values;
89

910
beforeAll(() => {
10-
inquirerPromptStub = jest.fn();
11-
inquirerCreatePromptModuleStub = jest.spyOn(inquirer, 'createPromptModule').mockReturnValue(inquirerPromptStub);
11+
createdInquirerPromptStub = jest.fn();
12+
inquirerCreatePromptModuleStub = jest.spyOn(inquirer, 'createPromptModule').mockReturnValue(createdInquirerPromptStub);
13+
inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockResolvedValue({});
1214
});
1315

1416
describe('with no values', () => {
@@ -22,7 +24,7 @@ describe('interactive-mode', () => {
2224
});
2325

2426
test('should call prompt', () => {
25-
expect(inquirerPromptStub).toHaveBeenCalled();
27+
expect(createdInquirerPromptStub).toHaveBeenCalled();
2628
});
2729
});
2830

@@ -48,11 +50,59 @@ describe('interactive-mode', () => {
4850
});
4951

5052
test('should call prompt', () => {
53+
expect(createdInquirerPromptStub).toHaveBeenCalled();
54+
});
55+
56+
test('should properly transform the values to inquirer values', () => {
57+
const args = createdInquirerPromptStub.mock.calls[1][0];
58+
expect(args.length).toEqual(Object.keys(values).length);
59+
60+
args.forEach((question) => {
61+
const inputValues = values[question.name];
62+
expect(inputValues).toBeTruthy();
63+
expect(question.type).toBe(inputValues.type);
64+
expect(question.message).toBe(inputValues.describe);
65+
expect(question.default).toBe(inputValues.default);
66+
expect(question.choices).toBe(inputValues.choices);
67+
});
68+
});
69+
});
70+
71+
describe('with support of inquirer plugins', () => {
72+
beforeAll(() => {
73+
const inquirerOptions = {
74+
allowInquirerPlugins: true,
75+
};
76+
77+
values = {
78+
title: {
79+
type: 'input',
80+
describe: 'Message to display',
81+
default: 'default value',
82+
},
83+
message: {
84+
type: 'list',
85+
describe: 'Welcome message',
86+
choices: ['hi', 'hello', 'hola!'],
87+
}
88+
};
89+
interactiveMode(values, inquirerOptions);
90+
});
91+
92+
test('should not have called createPromptModule', () => {
93+
expect(inquirerCreatePromptModuleStub).not.toHaveBeenCalled();
94+
});
95+
96+
test('should not have called prompt generated by createPromptModule', () => {
97+
expect(createdInquirerPromptStub).not.toHaveBeenCalled();
98+
});
99+
100+
test('should call the standard inquirer.prompt() method', () => {
51101
expect(inquirerPromptStub).toHaveBeenCalled();
52102
});
53103

54104
test('should properly transform the values to inquirer values', () => {
55-
const args = inquirerPromptStub.mock.calls[1][0];
105+
const args = inquirerPromptStub.mock.calls[0][0];
56106
expect(args.length).toEqual(Object.keys(values).length);
57107

58108
args.forEach((question) => {

0 commit comments

Comments
 (0)