forked from SBoudrias/Inquirer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
97 lines (76 loc) · 2.47 KB
/
input.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
const { expect } = require('chai');
const ReadlineStub = require('../../helpers/readline');
const fixtures = require('../../helpers/fixtures');
const Input = require('../../../lib/prompts/input');
describe('`input` prompt', () => {
beforeEach(function () {
this.fixture = { ...fixtures.input };
this.rl = new ReadlineStub();
});
it('should use raw value from the user', function (done) {
const input = new Input(this.fixture, this.rl);
input.run().then((answer) => {
expect(answer).to.equal('Inquirer');
done();
});
this.rl.emit('line', 'Inquirer');
});
it('should output filtered value', function () {
this.fixture.filter = function () {
return 'pass';
};
const prompt = new Input(this.fixture, this.rl);
const promise = prompt.run();
this.rl.emit('line', '');
return promise.then(() => {
expect(this.rl.output.__raw__).to.contain('pass');
});
});
it('should apply the provided transform to the value', function (done) {
this.fixture.transformer = function (value) {
return value.split('').reverse().join('');
};
const prompt = new Input(this.fixture, this.rl);
prompt.run();
this.rl.line = 'Inquirer';
this.rl.input.emit('keypress');
setTimeout(() => {
expect(this.rl.output.__raw__).to.contain('reriuqnI');
done();
}, 10);
});
it('should use the answers object in the provided transformer', function (done) {
this.fixture.transformer = function (value, answers) {
return answers.capitalize ? value.toUpperCase() : value;
};
const answers = {
capitalize: true,
};
const prompt = new Input(this.fixture, this.rl, answers);
prompt.run();
this.rl.line = 'inquirer';
this.rl.input.emit('keypress');
setTimeout(() => {
expect(this.rl.output.__raw__).to.contain('INQUIRER');
done();
}, 200);
});
it('should use the flags object in the provided transformer', function (done) {
this.fixture.transformer = function (value, answers, flags) {
const text = answers.capitalize ? value.toUpperCase() : value;
if (flags.isFinal) return text + '!';
return text;
};
const answers = {
capitalize: true,
};
const prompt = new Input(this.fixture, this.rl, answers);
prompt.run();
this.rl.line = 'inquirer';
this.rl.input.emit('keypress');
setTimeout(() => {
expect(this.rl.output.__raw__).to.contain('INQUIRER');
done();
}, 200);
});
});