-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
100 lines (88 loc) · 3.16 KB
/
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import test from 'ava';
import _ from 'lodash';
import ReadlineStub from './readline';
import fixtures from './fixtures';
import Input from '.';
test.beforeEach(t => {
t.context.fixture = _.clone(fixtures.input);
t.context.rl = new ReadlineStub();
});
test('should use raw value from the user', async t => {
const prompt = new Input(t.context.fixture, t.context.rl);
const input = prompt.run();
t.context.rl.emit('line', 'Inquirer');
const answer = await input;
t.is(answer, 'Inquirer');
});
test('should output filtered value', async t => {
t.context.fixture.filter = () => 'pass';
const prompt = new Input(t.context.fixture, t.context.rl);
const input = prompt.run();
t.context.rl.emit('line', '');
await input;
t.truthy(t.context.rl.output.__raw__.includes('pass'));
});
test('should apply the provided transform to the value', async t => {
t.context.fixture.transformer = value => value.split('').reverse().join('');
const prompt = new Input(t.context.fixture, t.context.rl);
const input = prompt.run();
t.context.rl.emit('line', 'Inquirer');
await input;
t.truthy(t.context.rl.output.__raw__.includes('reriuqnI'));
});
test('should use the answers object in the provided transformer', async t => {
t.context.fixture.transformer = (value, answers) =>
answers.capitalize ? value.toUpperCase() : value;
const answers = {
capitalize: true
};
const prompt = new Input(t.context.fixture, t.context.rl, answers);
const input = prompt.run();
t.context.rl.emit('line', 'inquirer');
await input;
t.truthy(t.context.rl.output.__raw__.includes('INQUIRER'));
});
test('should use the flags object in the provided transformer', async t => {
t.context.fixture.transformer = (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(t.context.fixture, t.context.rl, answers);
const input = prompt.run();
t.context.rl.emit('line', 'inquirerr');
await input;
t.truthy(t.context.rl.output.__raw__.includes('INQUIRERR'));
});
test('should autosubmit', async t => {
t.context.fixture.autoSubmit = value => value.length === 5;
const prompt = new Input(t.context.fixture, t.context.rl);
const input = prompt.run();
t.context.rl.line = '12345';
t.context.rl.input.emit('keypress');
await input;
t.truthy(t.context.rl.output.__raw__.includes('12345'));
});
test('for secret input, should mask the input with "*" if the `mask` option was provided by the user was `true`', async t => {
t.context.fixture.mask = true;
t.context.fixture.secret = true;
const prompt = new Input(t.context.fixture, t.context.rl);
const input = prompt.run();
t.context.rl.emit('line', '12345');
await input;
t.truthy(t.context.rl.output.__raw__.includes('*****'));
});
test('for secret input, should mask the input if a `mask` string was provided by the user', async t => {
t.context.fixture.mask = '#';
t.context.fixture.secret = true;
const prompt = new Input(t.context.fixture, t.context.rl);
const input = prompt.run();
t.context.rl.emit('line', '12345');
await input;
t.truthy(t.context.rl.output.__raw__.includes('#####'));
});