-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathoptions.choices.test.js
57 lines (52 loc) · 2.02 KB
/
options.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
const commander = require('../');
test('when option argument in choices then option set', () => {
const program = new commander.Command();
program
.exitOverride()
.addOption(new commander.Option('--colour <shade>').choices(['red', 'blue']));
program.parse(['--colour', 'red'], { from: 'user' });
expect(program.opts().colour).toBe('red');
});
test('when option 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: () => {}
})
.addOption(new commander.Option('--colour <shade>').choices(['red', 'blue']));
expect(() => {
program.parse(['--colour', '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.Option('--colour <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 option = new commander.Option('--colour <shade>').choices(param);
option.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: () => {}
})
.addOption(new commander.Option('--colour <shade>').choices(param));
param.push('orange');
expect(() => {
program.parse(['--colour', 'orange'], { from: 'user' });
}).toThrow();
});
});