-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
expand.js
174 lines (146 loc) · 4.97 KB
/
expand.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
const { expect } = require('chai');
const ReadlineStub = require('../../helpers/readline');
const fixtures = require('../../helpers/fixtures');
const Expand = require('../../../lib/prompts/expand');
describe('`expand` prompt', () => {
beforeEach(function () {
this.fixture = { ...fixtures.expand };
this.rl = new ReadlineStub();
this.expand = new Expand(this.fixture, this.rl);
});
it('should throw if `key` is missing', function () {
expect(() => {
this.fixture.choices = ['a', 'a'];
return new Expand(this.fixture, this.rl);
}).to.throw(/Format error/);
});
it('should throw if `key` is duplicate', function () {
expect(() => {
this.fixture.choices = [
{ key: 'a', name: 'foo' },
{ key: 'a', name: 'foo' },
];
return new Expand(this.fixture, this.rl);
}).to.throw(/Duplicate key error/);
});
it('should throw if `key` is duplicate case insensitive', function () {
expect(() => {
this.fixture.choices = [
{ key: 'a', name: 'foo' },
{ key: 'A', name: 'foo' },
];
return new Expand(this.fixture, this.rl);
}).to.throw(/Duplicate key error/);
});
it('should throw if `key` is `h`', function () {
expect(() => {
this.fixture.choices = [{ key: 'h', name: 'foo' }];
return new Expand(this.fixture, this.rl);
}).to.throw(/Reserved key error/);
});
it('should allow false as a value', function () {
const promise = this.expand.run();
this.rl.emit('line', 'd');
return promise.then((answer) => {
expect(answer).to.equal(false);
});
});
it('pass the value as answer, and display short on the prompt', function () {
this.fixture.choices = [
{ key: 'a', name: 'A Name', value: 'a value', short: 'ShortA' },
{ key: 'b', name: 'B Name', value: 'b value', short: 'ShortB' },
];
const prompt = new Expand(this.fixture, this.rl);
const promise = prompt.run();
this.rl.emit('line', 'b');
return promise.then((answer) => {
expect(answer).to.equal('b value');
expect(this.rl.output.__raw__).to.match(/ShortB/);
});
});
it('should use a string the `default` value', function (done) {
this.fixture.default = 'chile';
this.expand = new Expand(this.fixture, this.rl);
this.expand.run().then((answer) => {
expect(answer).to.equal('chile');
done();
});
this.rl.emit('line');
});
it('should use the `default` argument value', function (done) {
this.fixture.default = 1;
this.expand = new Expand(this.fixture, this.rl);
this.expand.run().then((answer) => {
expect(answer).to.equal('bar');
done();
});
this.rl.emit('line');
});
it('should return the user input', function (done) {
this.expand.run().then((answer) => {
expect(answer).to.equal('bar');
done();
});
this.rl.emit('line', 'b');
});
it('should strip the user input', function (done) {
this.expand.run().then((answer) => {
expect(answer).to.equal('bar');
done();
});
this.rl.emit('line', ' b ');
});
it('should have help option', function (done) {
this.expand.run().then((answer) => {
expect(this.rl.output.__raw__).to.match(/a\) acab/);
expect(this.rl.output.__raw__).to.match(/b\) bar/);
expect(answer).to.equal('chile');
done();
});
this.rl.emit('line', 'h');
this.rl.emit('line', 'c');
});
it('should not allow invalid command', function () {
const self = this;
const promise = this.expand.run();
this.rl.emit('line', 'blah');
setTimeout(() => {
self.rl.emit('line', 'a');
}, 10);
return promise;
});
it('should display and capitalize the default choice `key`', function () {
this.fixture.default = 1;
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();
expect(this.rl.output.__raw__).to.contain('(aBcdh)');
});
it('should display and capitalize the default choice by name value', function () {
this.fixture.default = 'chile';
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();
expect(this.rl.output.__raw__).to.contain('(abCdh)');
});
it('should display and capitalize the default choice H (Help) `key` if no string default matched', function () {
this.fixture.default = 'chile!';
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();
expect(this.rl.output.__raw__).to.contain('(abcdH)');
});
it('should display and capitalize the default choice H (Help) `key` if none provided', function () {
delete this.fixture.default;
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();
expect(this.rl.output.__raw__).to.contain('(abcdH)');
});
it("should 'autocomplete' the user input", function (done) {
this.expand = new Expand(this.fixture, this.rl);
this.expand.run();
this.rl.line = 'a';
this.rl.emit('keypress');
setTimeout(() => {
expect(this.rl.output.__raw__).to.contain('acab');
done();
}, 10);
});
});