command.copySettings.test.js
This file
Path
tests/command.copySettings.test.js
147 lines · 5.3 KiB · JavaScript
Last changed by
Refactor help option implementation to hold actual Option (#2006)John Geebefore adoption09244af
History of this fileTake it
Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
lines 1–36why, one step back1const commander = require('../');23// Tests some private properties as simpler than pure tests of observable behaviours.4// Testing before and after values in some cases, to ensure value actually changes (when copied).56test('when add subcommand with .command() then calls copyInheritedSettings from parent', () => {7 const program = new commander.Command();89 // This is a bit intrusive, but check expectation that copyInheritedSettings is called internally.10 const copySettingMock = jest.fn();11 program.createCommand = (name) => {12 const cmd = new commander.Command(name);13 cmd.copyInheritedSettings = copySettingMock;14 return cmd;15 };16 program.command('sub');1718 expect(copySettingMock).toHaveBeenCalledWith(program);19});2021describe('copyInheritedSettings property tests', () => {22 test('when copyInheritedSettings then copies outputConfiguration(config)', () => {23 const source = new commander.Command();24 const cmd = new commander.Command();2526 source.configureOutput({ foo: 'bar' });27 cmd.copyInheritedSettings(source);28 expect(cmd.configureOutput().foo).toEqual('bar');29 });3031 test('when copyInheritedSettings then copies helpOption(false)', () => {32 const source = new commander.Command();33 const cmd = new commander.Command();3435 source.helpOption(false);36 cmd.copyInheritedSettings(source);Refactor help option implementation to hold actual Option (#2006)John Geebefore adoption09244af
line 37why, one step back37 expect(cmd._getHelpOption()).toBe(null);Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
lines 38–45why, one step back38 });3940 test('when copyInheritedSettings then copies helpOption(flags, description)', () => {41 const source = new commander.Command();42 const cmd = new commander.Command();4344 source.helpOption('-Z, --zz', 'ddd');45 cmd.copyInheritedSettings(source);Refactor help option implementation to hold actual Option (#2006)John Geebefore adoption09244af
lines 46–51why, one step back46 expect(cmd._getHelpOption()).toBe(source._getHelpOption());47 // const helpOption = cmd._getHelpOption();48 // expect(helpOption.flags).toBe('-Z, --zz');49 // expect(helpOption.description).toBe('ddd');50 // expect(helpOption.short).toBe('-Z');51 // expect(helpOption.long).toBe('--zz');Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
lines 52–53why, one step back52 });53Refactor help command implementation to hold actual Command (#2087)John Geebefore adoptionff08a02
line 54why, one step back54 test('when copyInheritedSettings then copies custom help command', () => {Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
lines 55–57why, one step back55 const source = new commander.Command();56 const cmd = new commander.Command();57Refactor help command implementation to hold actual Command (#2087)John Geebefore adoptionff08a02
line 58why, one step back58 source.helpCommand('HELP [cmd]', 'ddd');Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
line 59why, one step back59 cmd.copyInheritedSettings(source);Refactor help command implementation to hold actual Command (#2087)John Geebefore adoptionff08a02
lines 60–75why, one step back60 cmd.helpCommand(true); // force enable61 const helpCommand = cmd._getHelpCommand();62 expect(helpCommand).toBeTruthy();63 expect(helpCommand.name()).toBe('HELP');64 expect(helpCommand.description()).toBe('ddd');65 });6667 test('when copyInheritedSettings then does not copy help enable override', () => {68 const source = new commander.Command();69 const cmd = new commander.Command();7071 // Existing behaviour, force enable/disable does not inherit,72 // largely so (probably redundant) program.helpCommand(true) does not inherit to leaf subcommands.73 source.helpCommand(true);74 cmd.copyInheritedSettings(source);75 expect(cmd._addHelpOption).toBeUndefined();Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
lines 76–147why, one step back76 });7778 test('when copyInheritedSettings then copies configureHelp(config)', () => {79 const source = new commander.Command();80 const cmd = new commander.Command();8182 const configuration = { foo: 'bar', helpWidth: 123, sortSubcommands: true };83 source.configureHelp(configuration);84 cmd.copyInheritedSettings(source);85 expect(cmd.configureHelp()).toEqual(configuration);86 });8788 test('when copyInheritedSettings then copies exitOverride()', () => {89 const source = new commander.Command();90 const cmd = new commander.Command();9192 expect(cmd._exitCallback).toBeFalsy();93 source.exitOverride();94 cmd.copyInheritedSettings(source);95 expect(cmd._exitCallback).toBeTruthy(); // actually a function96 });9798 test('when copyInheritedSettings then copies storeOptionsAsProperties()', () => {99 const source = new commander.Command();100 const cmd = new commander.Command();101102 expect(cmd._storeOptionsAsProperties).toBeFalsy();103 source.storeOptionsAsProperties();104 cmd.copyInheritedSettings(source);105 expect(cmd._storeOptionsAsProperties).toBeTruthy();106 });107108 test('when copyInheritedSettings then copies combineFlagAndOptionalValue()', () => {109 const source = new commander.Command();110 const cmd = new commander.Command();111112 expect(cmd._combineFlagAndOptionalValue).toBeTruthy();113 source.combineFlagAndOptionalValue(false);114 cmd.copyInheritedSettings(source);115 expect(cmd._combineFlagAndOptionalValue).toBeFalsy();116 });117118 test('when copyInheritedSettings then copies allowExcessArguments()', () => {119 const source = new commander.Command();120 const cmd = new commander.Command();121122 expect(cmd._allowExcessArguments).toBeTruthy();123 source.allowExcessArguments(false);124 cmd.copyInheritedSettings(source);125 expect(cmd._allowExcessArguments).toBeFalsy();126 });127128 test('when copyInheritedSettings then copies enablePositionalOptions()', () => {129 const source = new commander.Command();130 const cmd = new commander.Command();131132 expect(cmd._enablePositionalOptions).toBeFalsy();133 source.enablePositionalOptions();134 cmd.copyInheritedSettings(source);135 expect(cmd._enablePositionalOptions).toBeTruthy();136 });137138 test('when copyInheritedSettings then copies showHelpAfterError()', () => {139 const source = new commander.Command();140 const cmd = new commander.Command();141142 expect(cmd._showHelpAfterError).toBeFalsy();143 source.showHelpAfterError();144 cmd.copyInheritedSettings(source);145 expect(cmd._showHelpAfterError).toBeTruthy();146 });147});