command.copySettings.test.js
This file
Path
tests/command.copySettings.test.js
133 lines · 4.7 KiB · JavaScript
Last changed by
Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
History of this fileTake it
Add copyInheritedSettings (#1557)John Geebefore adoption5517d25
lines 1–133why, 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();34 expect(cmd._hasHelpOption).toBeTruthy();3536 source.helpOption(false);37 cmd.copyInheritedSettings(source);38 expect(cmd._hasHelpOption).toBeFalsy();39 });4041 test('when copyInheritedSettings then copies helpOption(flags, description)', () => {42 const source = new commander.Command();43 const cmd = new commander.Command();4445 source.helpOption('-Z, --zz', 'ddd');46 cmd.copyInheritedSettings(source);47 expect(cmd._helpFlags).toBe('-Z, --zz');48 expect(cmd._helpDescription).toBe('ddd');49 expect(cmd._helpShortFlag).toBe('-Z');50 expect(cmd._helpLongFlag).toBe('--zz');51 });5253 test('when copyInheritedSettings then copies addHelpCommand(name, description)', () => {54 const source = new commander.Command();55 const cmd = new commander.Command();5657 source.addHelpCommand('HELP [cmd]', 'ddd');58 cmd.copyInheritedSettings(source);59 expect(cmd._helpCommandName).toBe('HELP');60 expect(cmd._helpCommandnameAndArgs).toBe('HELP [cmd]');61 expect(cmd._helpCommandDescription).toBe('ddd');62 });6364 test('when copyInheritedSettings then copies configureHelp(config)', () => {65 const source = new commander.Command();66 const cmd = new commander.Command();6768 const configuration = { foo: 'bar', helpWidth: 123, sortSubcommands: true };69 source.configureHelp(configuration);70 cmd.copyInheritedSettings(source);71 expect(cmd.configureHelp()).toEqual(configuration);72 });7374 test('when copyInheritedSettings then copies exitOverride()', () => {75 const source = new commander.Command();76 const cmd = new commander.Command();7778 expect(cmd._exitCallback).toBeFalsy();79 source.exitOverride();80 cmd.copyInheritedSettings(source);81 expect(cmd._exitCallback).toBeTruthy(); // actually a function82 });8384 test('when copyInheritedSettings then copies storeOptionsAsProperties()', () => {85 const source = new commander.Command();86 const cmd = new commander.Command();8788 expect(cmd._storeOptionsAsProperties).toBeFalsy();89 source.storeOptionsAsProperties();90 cmd.copyInheritedSettings(source);91 expect(cmd._storeOptionsAsProperties).toBeTruthy();92 });9394 test('when copyInheritedSettings then copies combineFlagAndOptionalValue()', () => {95 const source = new commander.Command();96 const cmd = new commander.Command();9798 expect(cmd._combineFlagAndOptionalValue).toBeTruthy();99 source.combineFlagAndOptionalValue(false);100 cmd.copyInheritedSettings(source);101 expect(cmd._combineFlagAndOptionalValue).toBeFalsy();102 });103104 test('when copyInheritedSettings then copies allowExcessArguments()', () => {105 const source = new commander.Command();106 const cmd = new commander.Command();107108 expect(cmd._allowExcessArguments).toBeTruthy();109 source.allowExcessArguments(false);110 cmd.copyInheritedSettings(source);111 expect(cmd._allowExcessArguments).toBeFalsy();112 });113114 test('when copyInheritedSettings then copies enablePositionalOptions()', () => {115 const source = new commander.Command();116 const cmd = new commander.Command();117118 expect(cmd._enablePositionalOptions).toBeFalsy();119 source.enablePositionalOptions();120 cmd.copyInheritedSettings(source);121 expect(cmd._enablePositionalOptions).toBeTruthy();122 });123124 test('when copyInheritedSettings then copies showHelpAfterError()', () => {125 const source = new commander.Command();126 const cmd = new commander.Command();127128 expect(cmd._showHelpAfterError).toBeFalsy();129 source.showHelpAfterError();130 cmd.copyInheritedSettings(source);131 expect(cmd._showHelpAfterError).toBeTruthy();132 });133});