|
| 1 | +const commander = require('../'); |
| 2 | + |
| 3 | +// 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). |
| 5 | + |
| 6 | +test('when add subcommand with .command() then calls copyInheritedSettings from parent', () => { |
| 7 | + const program = new commander.Command(); |
| 8 | + |
| 9 | + // 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'); |
| 17 | + |
| 18 | + expect(copySettingMock).toHaveBeenCalledWith(program); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('copyInheritedSettings property tests', () => { |
| 22 | + test('when copyInheritedSettings then copies outputConfiguration(config)', () => { |
| 23 | + const source = new commander.Command(); |
| 24 | + const cmd = new commander.Command(); |
| 25 | + |
| 26 | + source.configureOutput({ foo: 'bar' }); |
| 27 | + cmd.copyInheritedSettings(source); |
| 28 | + expect(cmd.configureOutput().foo).toEqual('bar'); |
| 29 | + }); |
| 30 | + |
| 31 | + test('when copyInheritedSettings then copies helpOption(false)', () => { |
| 32 | + const source = new commander.Command(); |
| 33 | + const cmd = new commander.Command(); |
| 34 | + expect(cmd._hasHelpOption).toBeTruthy(); |
| 35 | + |
| 36 | + source.helpOption(false); |
| 37 | + cmd.copyInheritedSettings(source); |
| 38 | + expect(cmd._hasHelpOption).toBeFalsy(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('when copyInheritedSettings then copies helpOption(flags, description)', () => { |
| 42 | + const source = new commander.Command(); |
| 43 | + const cmd = new commander.Command(); |
| 44 | + |
| 45 | + 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 | + }); |
| 52 | + |
| 53 | + test('when copyInheritedSettings then copies addHelpCommand(name, description)', () => { |
| 54 | + const source = new commander.Command(); |
| 55 | + const cmd = new commander.Command(); |
| 56 | + |
| 57 | + 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 | + }); |
| 63 | + |
| 64 | + test('when copyInheritedSettings then copies configureHelp(config)', () => { |
| 65 | + const source = new commander.Command(); |
| 66 | + const cmd = new commander.Command(); |
| 67 | + |
| 68 | + const configuration = { foo: 'bar', helpWidth: 123, sortSubcommands: true }; |
| 69 | + source.configureHelp(configuration); |
| 70 | + cmd.copyInheritedSettings(source); |
| 71 | + expect(cmd.configureHelp()).toEqual(configuration); |
| 72 | + }); |
| 73 | + |
| 74 | + test('when copyInheritedSettings then copies exitOverride()', () => { |
| 75 | + const source = new commander.Command(); |
| 76 | + const cmd = new commander.Command(); |
| 77 | + |
| 78 | + expect(cmd._exitCallback).toBeFalsy(); |
| 79 | + source.exitOverride(); |
| 80 | + cmd.copyInheritedSettings(source); |
| 81 | + expect(cmd._exitCallback).toBeTruthy(); // actually a function |
| 82 | + }); |
| 83 | + |
| 84 | + test('when copyInheritedSettings then copies storeOptionsAsProperties()', () => { |
| 85 | + const source = new commander.Command(); |
| 86 | + const cmd = new commander.Command(); |
| 87 | + |
| 88 | + expect(cmd._storeOptionsAsProperties).toBeFalsy(); |
| 89 | + source.storeOptionsAsProperties(); |
| 90 | + cmd.copyInheritedSettings(source); |
| 91 | + expect(cmd._storeOptionsAsProperties).toBeTruthy(); |
| 92 | + }); |
| 93 | + |
| 94 | + test('when copyInheritedSettings then copies combineFlagAndOptionalValue()', () => { |
| 95 | + const source = new commander.Command(); |
| 96 | + const cmd = new commander.Command(); |
| 97 | + |
| 98 | + expect(cmd._combineFlagAndOptionalValue).toBeTruthy(); |
| 99 | + source.combineFlagAndOptionalValue(false); |
| 100 | + cmd.copyInheritedSettings(source); |
| 101 | + expect(cmd._combineFlagAndOptionalValue).toBeFalsy(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('when copyInheritedSettings then copies allowExcessArguments()', () => { |
| 105 | + const source = new commander.Command(); |
| 106 | + const cmd = new commander.Command(); |
| 107 | + |
| 108 | + expect(cmd._allowExcessArguments).toBeTruthy(); |
| 109 | + source.allowExcessArguments(false); |
| 110 | + cmd.copyInheritedSettings(source); |
| 111 | + expect(cmd._allowExcessArguments).toBeFalsy(); |
| 112 | + }); |
| 113 | + |
| 114 | + test('when copyInheritedSettings then copies enablePositionalOptions()', () => { |
| 115 | + const source = new commander.Command(); |
| 116 | + const cmd = new commander.Command(); |
| 117 | + |
| 118 | + expect(cmd._enablePositionalOptions).toBeFalsy(); |
| 119 | + source.enablePositionalOptions(); |
| 120 | + cmd.copyInheritedSettings(source); |
| 121 | + expect(cmd._enablePositionalOptions).toBeTruthy(); |
| 122 | + }); |
| 123 | + |
| 124 | + test('when copyInheritedSettings then copies showHelpAfterError()', () => { |
| 125 | + const source = new commander.Command(); |
| 126 | + const cmd = new commander.Command(); |
| 127 | + |
| 128 | + expect(cmd._showHelpAfterError).toBeFalsy(); |
| 129 | + source.showHelpAfterError(); |
| 130 | + cmd.copyInheritedSettings(source); |
| 131 | + expect(cmd._showHelpAfterError).toBeTruthy(); |
| 132 | + }); |
| 133 | +}); |
0 commit comments