-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement lazily created sub commands.
- Loading branch information
Showing
2 changed files
with
131 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const commander = require('../'); | ||
|
||
// Test some behaviours of .action not covered in more specific tests. | ||
|
||
describe('with subCommandBuilder', () => { | ||
test('when .action called then builder action is executed', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
program.command('run', 'run description', { | ||
subCommandBuilder: parent => { | ||
return parent | ||
.command('run') | ||
.action(actionMock); | ||
} | ||
}); | ||
program.parse(['node', 'test', 'run']); | ||
expect(actionMock).toHaveBeenCalled(); | ||
}); | ||
|
||
test('when .action called then builder promised action is executed', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
program.command('run', 'run description', { | ||
subCommandBuilder: parent => { | ||
return Promise.resolve(parent | ||
.command('run') | ||
.action(actionMock)); | ||
} | ||
}); | ||
return program.parseAsync(['node', 'test', 'run']).then(() => { | ||
expect(actionMock).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('arguments is resolved and passed to action handler', () => { | ||
const actionSpy = jest.fn(); | ||
return new commander.Command() | ||
.command('run <handler>', 'run description', { | ||
subCommandBuilder: parent => { | ||
return Promise.resolve(parent | ||
.command('run <arg>') | ||
.action((arg) => { | ||
expect(arg).toEqual('foo'); | ||
actionSpy(); | ||
})); | ||
} | ||
}) | ||
.parseAsync(['node', 'test', 'run', 'foo']).then(() => { | ||
expect(actionSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('options is resolved and passed to action handler', () => { | ||
const actionSpy = jest.fn(); | ||
return new commander.Command() | ||
.command('run <handler>', 'run description', { | ||
subCommandBuilder: parent => { | ||
return Promise.resolve(parent | ||
.command('run') | ||
.requiredOption('--test-option <val>', 'test value') | ||
.action(command => { | ||
expect(command.testOption).toEqual('bar'); | ||
actionSpy(); | ||
}) | ||
); | ||
} | ||
}) | ||
.parseAsync(['node', 'test', 'run', '--test-option', 'bar']).then(() => { | ||
expect(actionSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
test('recursive sub command action handler', () => { | ||
const infoSpy = jest.fn(); | ||
const runSpy = jest.fn(); | ||
const runCommandBuilder = parent => { | ||
return parent.command('run <handler>', 'run description', { | ||
subCommandBuilder: (parent, _operands, _unknown) => { | ||
if (_operands && _operands[0] === 'run') { | ||
runSpy(); | ||
return runCommandBuilder(parent); | ||
} | ||
return Promise.resolve(parent | ||
.command('info') | ||
.action(infoSpy) | ||
); | ||
} | ||
}); | ||
}; | ||
return runCommandBuilder(new commander.Command()) | ||
.parseAsync(['node', 'test', 'run', 'run', 'run', 'run', 'info']).then(() => { | ||
expect(infoSpy).toHaveBeenCalled(); | ||
expect(runSpy).toHaveBeenCalledTimes(3); | ||
}) | ||
; | ||
}); | ||
}); |