Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve backwards compatibility for command events (#1403)
* Improve backwards compatibility for command events * Implement tests for legacy command event
- Loading branch information
1 parent
9b087c8
commit bd538aa
Showing
2 changed files
with
43 additions
and
2 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,38 @@ | ||
const commander = require('../'); | ||
|
||
// The action handler used to be implemented using command events and listeners. | ||
// Now, this is mostly just for backwards compatibility. | ||
|
||
describe(".command('*')", () => { | ||
test('when action handler for subcommand then emit command:subcommand', () => { | ||
const mockListener = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.command('sub') | ||
.action(() => {}); | ||
program.on('command:sub', mockListener); | ||
program.parse(['sub'], { from: 'user' }); | ||
expect(mockListener).toHaveBeenCalled(); | ||
}); | ||
|
||
test('when no action handler for subcommand then still emit command:subcommand', () => { | ||
const mockListener = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.command('sub'); | ||
program.on('command:sub', mockListener); | ||
program.parse(['sub'], { from: 'user' }); | ||
expect(mockListener).toHaveBeenCalled(); | ||
}); | ||
|
||
test('when subcommand has argument then emit command:subcommand with argument', () => { | ||
const mockListener = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.command('sub <file>') | ||
.action(() => {}); | ||
program.on('command:sub', mockListener); | ||
program.parse(['sub', 'file'], { from: 'user' }); | ||
expect(mockListener).toHaveBeenCalledWith(['file'], []); | ||
}); | ||
}); |