1 import { merge } from './merge';
3 describe('merge', () => {
4 it('recursively merges an object deeply with another object', () => {
5 const original = { foo: 'bar', baz: { qux: 'quux' } };
6 const overwrite = { foo: 'baz', baz: { qux: 'quuz' } };
7 const result = merge(original, overwrite);
8 const expected = { foo: 'baz', baz: { qux: 'quuz' } };
10 expect(result).toEqual(expected);
13 it('keeps properties that are not defined on the overwriting object intact', () => {
14 const original = { foo: 'bar', baz: 'qux', quux: { quuz: 'corge', grault: 'garply' } };
15 const overwrite = { foo: 'baz', quux: { quuz: 'grault' } };
16 const result = merge(original, overwrite);
17 const expected = { foo: 'baz', baz: 'qux', quux: { quuz: 'grault', grault: 'garply' } };
19 expect(result).toEqual(expected);
22 it("keeps reference identify for properties which aren't overwritten by the overwriting object", () => {
23 const original = { foo: 'bar', baz: { qux: 'quux' } };
24 const overwrite = { foo: 'baz', qux: { qux: 'quux' } };
25 const result = merge(original, overwrite);
27 expect(result.baz).toBe(original.baz);
30 it('returns the original object unmodified should it be identical to the overwriting object', () => {
31 const original = { foo: 'bar', baz: { qux: 'quux' } };
32 const overwrite = original;
33 const result = merge(original, overwrite);
35 expect(result).toBe(original);
38 it('should excludeEmpty values properly', () => {
39 expect(merge({ foo: true }, { foo: false }, { excludeEmpty: true })).toEqual({ foo: false });
40 expect(merge({ foo: true }, { foo: '' }, { excludeEmpty: true })).toEqual({ foo: true });
41 expect(merge({ foo: true }, { foo: [] }, { excludeEmpty: true })).toEqual({ foo: [] });
42 expect(merge({ foo: true }, { foo: null }, { excludeEmpty: true })).toEqual({ foo: true });
43 expect(merge({ foo: true }, { foo: undefined }, { excludeEmpty: true })).toEqual({ foo: true });
44 expect(merge({ foo: true }, {}, { excludeEmpty: true })).toEqual({ foo: true });
45 expect(merge({ foo: true }, { foo: NaN }, { excludeEmpty: true })).toEqual({ foo: true });
46 expect(merge({ foo: true }, { foo: 0 }, { excludeEmpty: true })).toEqual({ foo: 0 });