/
argument.choices.test.js
59 lines (54 loc) · 2.02 KB
/
argument.choices.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
const commander = require('../');
test('when command argument in choices then argument set', () => {
const program = new commander.Command();
let shade;
program
.exitOverride()
.addArgument(new commander.Argument('<shade>').choices(['red', 'blue']))
.action((shadeParam) => { shade = shadeParam; });
program.parse(['red'], { from: 'user' });
expect(shade).toBe('red');
});
test('when command argument is not in choices then error', () => {
// Lightweight check, more detailed testing of behaviour in command.exitOverride.test.js
const program = new commander.Command();
program
.exitOverride()
.configureOutput({
writeErr: () => {}
})
.addArgument(new commander.Argument('<shade>').choices(['red', 'blue']));
expect(() => {
program.parse(['orange'], { from: 'user' });
}).toThrow();
});
describe('choices parameter is treated as readonly, per TypeScript declaration', () => {
test('when choices called then parameter does not change', () => {
// Unlikely this could break, but check the API we are declaring in TypeScript.
const original = ['red', 'blue', 'green'];
const param = original.slice();
new commander.Argument('<shade>').choices(param);
expect(param).toEqual(original);
});
test('when choices called and argChoices later changed then parameter does not change', () => {
const original = ['red', 'blue', 'green'];
const param = original.slice();
const argument = new commander.Argument('<shade>').choices(param);
argument.argChoices.push('purple');
expect(param).toEqual(original);
});
test('when choices called and parameter changed the choices does not change', () => {
const program = new commander.Command();
const param = ['red', 'blue'];
program
.exitOverride()
.configureOutput({
writeErr: () => {}
})
.addArgument(new commander.Argument('<shade>').choices(param));
param.push('orange');
expect(() => {
program.parse(['orange'], { from: 'user' });
}).toThrow();
});
});