-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
api.js
381 lines (317 loc) · 9.62 KB
/
api.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* Test Prompt public APIs
*/
const { expect } = require('chai');
const fixtures = require('../helpers/fixtures');
const ReadlineStub = require('../helpers/readline');
const inquirer = require('../../lib/inquirer');
const { autosubmit } = require('../helpers/events');
// Define prompts and their public API
const prompts = [
{
name: 'input',
apis: ['filter', 'validate', 'default', 'message', 'requiredValues'],
},
{
name: 'confirm',
apis: ['message', 'requiredValues'],
},
{
name: 'rawlist',
apis: ['filter', 'message', 'choices', 'requiredValues'],
},
{
name: 'list',
apis: ['filter', 'message', 'choices', 'requiredValues'],
},
{
name: 'expand',
apis: ['requiredValues', 'message'],
},
{
name: 'checkbox',
apis: ['requiredValues', 'message', 'choices', 'filter', 'validate'],
},
{
name: 'password',
apis: ['requiredValues', 'message', 'filter', 'validate', 'default'],
},
];
// Define tests
const tests = {
filter() {
describe('filter API', () => {
it('should filter the user input', function (done) {
this.fixture.filter = function () {
return 'pass';
};
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run().then((answer) => {
expect(answer).to.equal('pass');
done();
});
this.rl.emit('line', '');
});
it('should allow filter function to be asynchronous', function (done) {
this.fixture.filter = function () {
const done = this.async();
setTimeout(() => {
done(null, 'pass');
}, 0);
};
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run().then((answer) => {
expect(answer).to.equal('pass');
done();
});
this.rl.emit('line', '');
});
it('should handle errors produced in async filters', function () {
let called = 0;
const { rl } = this;
this.fixture.filter = function () {
called++;
const cb = this.async();
if (called === 2) {
return cb(null, 'pass');
}
rl.emit('line');
return cb(new Error('fail'));
};
const prompt = new this.Prompt(this.fixture, this.rl);
const promise = prompt.run();
this.rl.emit('line');
return promise;
});
it('should pass previous answers to the prompt filter function', () => {
const prompt = inquirer.createPromptModule();
const questions = [
{
type: 'confirm',
name: 'q1',
message: 'message',
},
{
type: 'confirm',
name: 'q2',
message: 'message',
filter(input, answers) {
expect(answers.q1).to.equal(true);
return input;
},
default: false,
},
];
const promise = prompt(questions);
autosubmit(promise.ui);
return promise.then((answers) => {
expect(answers.q1).to.equal(true);
expect(answers.q2).to.equal(false);
});
});
});
},
validate() {
describe('validate API', () => {
it('should reject input if boolean false is returned', function () {
let called = 0;
this.fixture.validate = () => {
called++;
// Make sure returning false won't continue
if (called === 2) {
return true;
}
this.rl.emit('line');
return false;
};
const prompt = new this.Prompt(this.fixture, this.rl);
const promise = prompt.run();
this.rl.emit('line');
return promise;
});
it('should reject input if a string is returned', function (done) {
const self = this;
let called = 0;
const errorMessage = 'uh oh, error!';
this.fixture.validate = function () {
called++;
// Make sure returning false won't continue
if (called === 2) {
done();
return;
}
self.rl.emit('line');
return errorMessage;
};
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run();
this.rl.emit('line');
});
it('should reject input if a Promise is returned which rejects', function (done) {
const self = this;
let called = 0;
const errorMessage = 'uh oh, error!';
this.fixture.validate = function () {
called++;
// Make sure returning false won't continue
if (called === 2) {
done();
return;
}
self.rl.emit('line');
return Promise.reject(errorMessage);
};
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run();
this.rl.emit('line');
});
it('should accept input if boolean true is returned', function () {
let called = 0;
this.fixture.validate = function () {
called++;
return true;
};
const prompt = new this.Prompt(this.fixture, this.rl);
const promise = prompt.run().then(() => {
expect(called).to.equal(1);
});
this.rl.emit('line');
return promise;
});
it('should allow validate function to be asynchronous', function () {
const self = this;
let called = 0;
this.fixture.validate = function () {
const done = this.async();
setTimeout(() => {
called++;
// Make sure returning false won't continue
if (called === 2) {
done(null, true);
} else {
self.rl.emit('line');
}
done(false);
}, 0);
};
const prompt = new this.Prompt(this.fixture, this.rl);
const promise = prompt.run();
this.rl.emit('line');
return promise;
});
it('should allow validate function to return a Promise', function () {
this.fixture.validate = function () {
return Promise.resolve(true);
};
const prompt = new this.Prompt(this.fixture, this.rl);
const promise = prompt.run();
this.rl.emit('line');
return promise;
});
it('should pass previous answers to the prompt validation function', () => {
const prompt = inquirer.createPromptModule();
const questions = [
{
type: 'confirm',
name: 'q1',
message: 'message',
},
{
type: 'confirm',
name: 'q2',
message: 'message',
validate(input, answers) {
expect(answers.q1).to.equal(true);
return true;
},
default: false,
},
];
const promise = prompt(questions);
autosubmit(promise.ui);
return promise.then((answers) => {
expect(answers.q1).to.equal(true);
expect(answers.q2).to.equal(false);
});
});
});
},
default() {
describe('default API', () => {
it('should allow a default value', function (done) {
this.fixture.default = 'pass';
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run().then((answer) => {
expect(this.rl.output.__raw__).to.contain('(pass)');
expect(answer).to.equal('pass');
done();
});
this.rl.emit('line', '');
});
it('should allow a falsy default value', function (done) {
this.fixture.default = 0;
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run().then((answer) => {
expect(this.rl.output.__raw__).to.contain('(0)');
expect(answer).to.equal(0);
done();
});
this.rl.emit('line', '');
});
});
},
message() {
describe('message API', () => {
it('should print message on screen', function () {
this.fixture.message = 'Foo bar bar foo bar';
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run();
expect(this.rl.output.__raw__).to.contain(this.fixture.message);
});
it('should default to name for message', function () {
this.fixture.name = 'testfoobarbarfoobar';
delete this.fixture.message;
const prompt = new this.Prompt(this.fixture, this.rl);
prompt.run();
expect(this.rl.output.__raw__).to.contain(this.fixture.name + ':');
});
});
},
choices() {
describe('choices API', () => {
it('should print choices to screen', function () {
const prompt = new this.Prompt(this.fixture, this.rl);
const { choices } = prompt.opt;
prompt.run();
choices.filter(inquirer.Separator.exclude).forEach((choice) => {
expect(this.rl.output.__raw__).to.contain(choice.name);
});
});
});
},
requiredValues() {
describe('Missing value', () => {
it('`name` should throw', function () {
expect(() => {
delete this.fixture.name;
return new this.Prompt(this.fixture, this.rl);
}).to.throw(/name/);
});
});
},
};
// Run tests
describe('Prompt public APIs', () => {
prompts.forEach((detail) => {
describe('on ' + detail.name + ' prompt', () => {
beforeEach(function () {
this.fixture = { ...fixtures[detail.name] };
this.Prompt = inquirer.prompt.prompts[detail.name];
this.rl = new ReadlineStub();
});
detail.apis.forEach((apiName) => {
tests[apiName](detail.name);
});
});
});
});