diff --git a/src/ConfigMergeCommand.js b/src/ConfigMergeCommand.js index e662cbf..407fee8 100644 --- a/src/ConfigMergeCommand.js +++ b/src/ConfigMergeCommand.js @@ -1,5 +1,6 @@ import { - mergeWith + mergeWith, + isObject } from 'lodash'; import ConfigCommand from './ConfigCommand'; @@ -15,8 +16,12 @@ class ConfigMergeCommand extends ConfigCommand { const value = this.optionsResolver.resolve(config, options); mergeWith(config, value, (x, y) => { // eslint-disable-line consistent-return - if (Array.isArray(x)) { - return x.concat(y); + if (Array.isArray(x) && Array.isArray(y)) { + return [...x, ...y]; + } else if (Array.isArray(x) && isObject(y)) { + return [...x, y]; + } else if (isObject(x) && Array.isArray(y)) { + return [x, ...y]; } }); } diff --git a/test/ConfigMergeCommand.spec.js b/test/ConfigMergeCommand.spec.js index 52ffff4..d846c3d 100644 --- a/test/ConfigMergeCommand.spec.js +++ b/test/ConfigMergeCommand.spec.js @@ -18,35 +18,103 @@ describe('ConfigMergeCommand', () => { }); describe('#execute()', () => { - it('should execute successfully', () => { - command.execute(config, { - foo: { - bar: 'bar1' - }, - bar: ['bar1'] - }); - command.execute(config, { - foo: { - bar: 'bar2' - }, - bar: ['bar2'] - }); + it('should execute successfully for `Function~>(Config|Undefined)`', () => { command.execute(config, x => { expect(x).toBe(config); return { - foo: { - bar: 'bar3' + obj: { + obj1: 'fn1' } }; }); command.execute(config, () => {}); + command.execute(config, () => { + return { + obj: { + obj1: 'fn2' + } + }; + }); + + expect(config.toObject()).toEqual({ + obj: { + obj1: 'fn2' + } + }); + }); + + it('should execute successfully for `Object~>Object`', () => { + command.execute(config, { + obj: { + obj1: 'obj1' + } + }); + command.execute(config, { + obj: { + obj1: 'obj2' + } + }); + + expect(config.toObject()).toEqual({ + obj: { + obj1: 'obj2' + } + }); + }); + + it('should execute successfully for `Object[]~>Object[]`', () => { + command.execute(config, { + arr: ['arr1'] + }); + command.execute(config, { + arr: ['arr2'] + }); + + expect(config.toObject()).toEqual({ + arr: ['arr1', 'arr2'] + }); + }); + + it('should execute successfully for `Object[]~>Object`', () => { + command.execute(config, { + arr: [{ + arr1: 'arr1' + }] + }); + command.execute(config, { + arr: { + arr2: 'arr2' + } + }); + + expect(config.toObject()).toEqual({ + arr: [{ + arr1: 'arr1' + }, { + arr2: 'arr2' + }] + }); + }); + + it('should execute successfully for `Object~>Object[]`', () => { + command.execute(config, { + obj: { + obj1: 'obj1' + } + }); + command.execute(config, { + obj: [{ + obj2: 'obj2' + }] + }); expect(config.toObject()).toEqual({ - foo: { - bar: 'bar3' - }, - bar: ['bar1', 'bar2'] + obj: [{ + obj1: 'obj1' + }, { + obj2: 'obj2' + }] }); }); });