forked from tj/commander.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.collectAllOptions.test.js
59 lines (48 loc) · 1.7 KB
/
command.collectAllOptions.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
var { Command } = require('../');
function createCommanderInstance(mockFn) {
const subCmd = new Command()
.name('sub_cmd')
.option('-f, --force');
subCmd
.command('sub_sub_cmd')
.option('-d, --delete')
.action(mockFn);
const root = new Command();
root
.option('-q, --quiet');
root
.useSubcommand(subCmd);
return root;
}
// TESTS
test('should collect options from all 3 levels when all passed', () => {
const actionMock = jest.fn();
const program = createCommanderInstance(actionMock);
program.parse(['node', 'test', 'sub_cmd', 'sub_sub_cmd', '-f', '-q', '-d']);
expect(actionMock).toHaveBeenCalledTimes(1);
expect(actionMock.mock.calls[0].length).toBe(1);
expect(actionMock.mock.calls[0][0] instanceof Command).toBeTruthy();
expect(actionMock.mock.calls[0][0].name()).toBe('sub_sub_cmd');
expect(actionMock.mock.calls[0][0].collectAllOptions()).toEqual({
quiet: true,
force: true,
delete: true
});
});
test('should collect options from all 3 levels when just some passed', () => {
const actionMock = jest.fn();
const program = createCommanderInstance(actionMock);
program.parse(['node', 'test', 'sub_cmd', 'sub_sub_cmd', '-q']);
expect(actionMock).toHaveBeenCalledTimes(1);
expect(actionMock.mock.calls[0].length).toBe(1);
expect(actionMock.mock.calls[0][0] instanceof Command).toBeTruthy();
expect(actionMock.mock.calls[0][0].name()).toBe('sub_sub_cmd');
const allOpts = actionMock.mock.calls[0][0].collectAllOptions();
expect(allOpts).toEqual({
quiet: true,
force: undefined,
delete: undefined
});
// The attrs are enumerable, just undefined !
expect(Object.keys(allOpts).sort()).toEqual(['delete', 'force', 'quiet']);
});