forked from SBoudrias/Inquirer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawlist.js
172 lines (143 loc) · 4.64 KB
/
rawlist.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
const { expect } = require('chai');
const _ = require('lodash');
const ReadlineStub = require('../../helpers/readline');
const fixtures = require('../../helpers/fixtures');
const Rawlist = require('../../../lib/prompts/rawlist');
describe('`rawlist` prompt', () => {
beforeEach(function () {
this.rl = new ReadlineStub();
this.fixture = _.clone(fixtures.rawlist);
this.rawlist = new Rawlist(this.fixture, this.rl);
});
it('should default to first choice', function (done) {
this.rawlist.run().then((answer) => {
expect(answer).to.equal('foo');
done();
});
this.rl.emit('line');
});
it('should select given index', function (done) {
this.rawlist.run().then((answer) => {
expect(answer).to.equal('bar');
done();
});
this.rl.emit('line', '2');
});
it('should not allow invalid index', function () {
const self = this;
const promise = this.rawlist.run();
this.rl.emit('line', 'blah');
setTimeout(() => {
self.rl.emit('line', '1');
}, 10);
return promise;
});
it('should require a choices array', () => {
const mkPrompt = function () {
return new Rawlist({ name: 'foo', message: 'bar' });
};
expect(mkPrompt).to.throw(/choices/);
});
it('should allow a default index', function (done) {
this.fixture.default = 1;
const list = new Rawlist(this.fixture, this.rl);
list.run().then((answer) => {
expect(answer).to.equal('bar');
done();
});
this.rl.emit('line');
});
it("shouldn't allow an invalid index as default", function (done) {
this.fixture.default = 4;
const list = new Rawlist(this.fixture, this.rl);
list.run().then((answer) => {
expect(answer).to.equal('foo');
done();
});
this.rl.emit('line');
});
it('should allow string default being the value', function (done) {
this.fixture.default = 'bum';
const list = new Rawlist(this.fixture, this.rl);
list.run().then((answer) => {
expect(answer).to.equal('bum');
done();
});
this.rl.emit('line');
});
it("shouldn't allow an invalid string default to change position", function (done) {
this.fixture.default = 'bumby';
const list = new Rawlist(this.fixture, this.rl);
list.run().then((answer) => {
expect(answer).to.equal('foo');
done();
});
this.rl.emit('line');
});
it('should allow for arrow navigation', function (done) {
this.rawlist.run().then((answer) => {
expect(answer).to.equal('bar');
done();
});
this.rl.input.emit('keypress', '', { name: 'down' });
this.rl.input.emit('keypress', '', { name: 'down' });
this.rl.input.emit('keypress', '', { name: 'up' });
this.rl.emit('line', this.rl.line);
});
it('should allow for arrow navigation after invalid input', function (done) {
this.rawlist.run().then((answer) => {
try {
expect(answer).to.equal('bar');
} catch (err) {
return done(err);
}
done();
});
this.rl.emit('line', 'blah');
this.rl.input.emit('keypress', '', { name: 'down' });
this.rl.input.emit('keypress', '', { name: 'down' });
this.rl.input.emit('keypress', '', { name: 'up' });
this.rl.emit('line', this.rl.line);
});
describe('going out of boundaries', () => {
beforeEach(function () {
this.pressKey = function (dir, times) {
for (let i = 0; i < times; i++) {
this.rl.input.emit('keypress', '', { name: dir });
}
this.rl.emit('line', this.rl.line);
};
});
describe('when loop undefined / true', () => {
it('loops to bottom when too far up', async function () {
const promise = this.rawlist.run();
this.pressKey('up', 2);
const answer = await promise;
expect(answer).to.equal('bar');
});
it('loops to top when too far down', async function () {
const promise = this.rawlist.run();
this.pressKey('down', 3);
const answer = await promise;
expect(answer).to.equal('foo');
});
});
describe('when loop: false', () => {
beforeEach(function () {
this.rawlist = new Rawlist(_.assign(this.fixture, { loop: false }), this.rl);
});
it('stays at top when too far up', async function () {
const promise = this.rawlist.run();
this.pressKey('up', 2);
const answer = await promise;
expect(answer).to.equal('foo');
});
it('stays at bottom when too far down', async function () {
const promise = this.rawlist.run();
this.pressKey('down', 3);
const answer = await promise;
expect(answer).to.equal('bum');
});
});
});
});