command.onCommand.test.js
This file
Path
tests/command.onCommand.test.js
33 lines · 1.3 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334const 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'], []); });});