Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
fix(ConfigMergeCommand): adds more merge scenarios (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdreizin authored Aug 8, 2016
1 parent d2cb38a commit db5642d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 22 deletions.
11 changes: 8 additions & 3 deletions src/ConfigMergeCommand.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
mergeWith
mergeWith,
isObject
} from 'lodash';
import ConfigCommand from './ConfigCommand';

Expand All @@ -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];
}
});
}
Expand Down
106 changes: 87 additions & 19 deletions test/ConfigMergeCommand.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}]
});
});
});
Expand Down

0 comments on commit db5642d

Please sign in to comment.