-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
input.js
116 lines (93 loc) · 2.9 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { describe, it, beforeEach, expect } from 'vitest';
import ReadlineStub from '../../helpers/readline.js';
import fixtures from '../../helpers/fixtures.js';
import Input from '../../../lib/prompts/input.js';
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).toEqual('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__).toContain('pass');
});
});
it('should apply the provided transform to the value', function (done) {
this.fixture.transformer = function (value) {
return [...value].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__).toContain('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__).toContain('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__).toContain('INQUIRER');
done();
}, 200);
});
it('should clear default on input', function (done) {
const defaultValue = 'default-string';
const input = new Input(
{
...this.fixture,
default: defaultValue,
},
this.rl,
);
input.run();
this.rl.line = 'inquirer';
this.rl.input.emit('keypress');
setTimeout(() => {
expect(this.rl.output.__raw__).toContain(defaultValue);
done();
}, 200);
});
});