All files / src merge.test.ts

98.77% Statements 160/162
100% Branches 0/0
100% Functions 39/39
98.69% Lines 151/153
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 3571x 1x 1x 1x   1x   1x 1x 1x     1x 1x     1x 1x             1x             1x   1x 1x 1x 1x     1x 1x 1x   1x   1x 1x     1x 1x 1x   1x       1x 1x     1x 1x 1x   1x   1x 1x     1x   1x 1x   1x         1x 1x     1x 1x   1x             1x   1x     1x 1x   1x   1x     1x             1x   1x 1x     1x 1x   1x     1x       1x       1x 1x     1x 1x 1x   1x 1x     1x   1x 1x     1x 1x 1x   1x 2x     1x         1x 1x     1x 1x 1x   1x   1x 1x     1x 1x 1x   1x       1x 1x 1x 1x     1x 1x       1x       1x   1x 1x 1x     1x 1x       1x       1x         1x 1x     1x 1x 1x       1x 1x       1x       1x         1x 1x     1x 1x   1x       1x   1x     1x 1x   1x       1x   1x     1x 1x       1x       1x   1x     1x 1x       1x       1x   1x     1x 1x           1x       1x   1x     1x 1x           1x       1x   1x     1x 1x           1x       1x   1x   1x     1x    
import * as chai     from 'chai';
import ArrayStrategy from './Constants/ArrayStrategy';
import merge         from './merge';
import * as Messages from './Messages';
 
const assert = chai.assert;
 
describe('merge()', () => {
    it('should error if an invalid source object is provided', () => {
        assert.throws(() => merge({}, null), Messages.TYPE_ERROR_SOURCE(null));
    });
 
    it('should error if an invalid target object is provided', () => {
        assert.throws(() => merge(null, {}), Messages.TYPE_ERROR_TARGET(null));
    });
 
    it('should shallow merge the properties of one object into another', () => {
        const obj1 = {
            foo: null,
            bar: null,
            baz: null,
            car: null
        };
 
        const obj2 = {
            foo: 1,
            bar: true,
            baz: {},
            car: 'hello'
        };
 
        merge(obj1, obj2);
 
        assert.equal(obj1.foo, 1);
        assert.equal(obj1.bar, true);
        assert.equal(obj1.car, 'hello');
        assert.equal(obj1.baz, obj2.baz);
    });
 
    it('should add nested objects as references if `deep` option not set', () => {
        const obj1 = {foo: {bar: null}};
        const obj2 = {foo: {bar: 1}};
 
        merge(obj1, obj2);
 
        assert.equal(obj1.foo, obj2.foo);
        assert.equal(obj1.foo.bar, 1);
    });
 
    it('should recursively merge nested objects if `deep` option set', () => {
        const obj1 = {foo: {bar: null}};
        const obj2 = {foo: {bar: 1}};
 
        merge(obj1, obj2, {
            deep: true
        });
 
        assert.notEqual(obj1.foo, obj2.foo);
        assert.equal(obj1.foo.bar, 1);
    });
 
    it('should accept a third parameter `true` as a shorthand for `deep: true`', () => {
        const obj1 = {foo: {bar: null}};
        const obj2 = {foo: {bar: 1}};
 
        merge(obj1, obj2, true);
 
        assert.notEqual(obj1.foo, obj2.foo);
        assert.equal(obj1.foo.bar, 1);
    });
 
    it(`should add nested objects by references when deep merging,
        \`useReferenceIfTargetUnset\` option set, and target is unset`, () => {
        const obj1 = {foo: null};
        const obj2 = {foo: {bar: 1}};
 
        merge(obj1, obj2, {
            deep: true,
            useReferenceIfTargetUnset: true
        });
 
        assert.equal(obj1.foo, obj2.foo);
        assert.equal(obj1.foo.bar, 1);
    });
 
    it('should skip read-only properties', () => {
        const obj1: any = {};
 
        const obj2: any = {
            foo: 'bar',
            get baz() {
                return this.foo;
            }
        };
 
        merge(obj1, obj2);
 
        assert.isUndefined(obj1.baz);
    });
 
    it('should include accessor properties', () => {
        const obj1: any = {};
 
        let baz = 'car';
 
        const obj2: any = {
            foo: 'bar',
            get baz() {
                return baz;
            },
            set baz(value) {
                baz = value;
            }
        };
 
        merge(obj1, obj2);
 
        assert.isDefined(obj1.baz);
        assert.equal(obj1.baz, 'car');
    });
 
    it('should include read-only properties if `includeReadOnly` option set', () => {
        const obj1: any = {};
 
        const obj2: any = {
            foo: 'bar',
            get baz() {
                return this.foo;
            }
        };
 
        merge(obj1, obj2, {
            includeReadOnly: true
        });
 
        assert.isDefined(obj1.baz);
        assert.equal(obj1.baz, 'bar');
    });
 
    it('should skip non-enumerable properties', () => {
        const obj1: any = {};
        const obj2: any = {};
 
        Object.defineProperty(obj2, 'bar', {
            get: () => 'baz'
        });
 
        merge(obj1, obj2);
 
        assert.isDefined(obj2.bar);
        assert.isUndefined(obj1.bar);
    });
 
    it('should include non-enumerable properties if `includeNonEnumerable` option set', () => {
        const obj1: any = {};
        const obj2: any = {};
 
        Object.defineProperty(obj2, 'bar', {
            get: () => 'baz'
        });
 
        merge(obj1, obj2, {
            includeNonEmurable: true,
            includeReadOnly: true
        });
 
        assert.isDefined(obj2.bar);
        assert.isDefined(obj1.bar);
    });
 
    it('should merge arrays with a "REPLACE" strategy', () => {
        const arr1 = ['foo', 'bar'];
        const arr2 = ['foo', 'baz'];
 
        merge(arr1, arr2);
 
        assert.equal(arr1.length, 2);
        assert.equal(arr1[1], 'baz');
    });
 
    it('should merge arrays with a "PUSH" strategy if `arrayStrategy` set to "PUSH"', () => {
        const arr1 = ['foo', 'bar'];
        const arr2 = ['foo', 'baz'];
 
        merge(arr1, arr2, {
            arrayStrategy: ArrayStrategy.PUSH
        });
 
        assert.equal(arr1.length, 4);
        assert.equal(arr1[1], 'bar');
        assert.equal(arr1[2], 'foo');
        assert.equal(arr1[3], 'baz');
    });
 
    it('should deep merge arrays if `deep` option set', () => {
        const obj1: any = {
            foo: [{}]
        };
 
        const obj2: any = {
            foo: [{bar: 1}]
        };
 
        merge(obj1, obj2, true);
 
        assert.equal(obj1.foo.length, 1);
        assert.notEqual(obj1.foo[0], obj2.foo[0]);
        assert.equal(obj1.foo[0].bar, 1);
    });
 
    it('should deep merge arrays with a "PUSH" strategy if `deep` option set and `arrayStrategy` set to "PUSH"', () => {
        const obj1 = {
            foo: [{}]
        };
 
        const obj2 = {
            foo: [{bar: 1}]
        };
 
        merge(obj1, obj2, {
            deep: true,
            arrayStrategy: ArrayStrategy.PUSH
        });
 
        assert.equal(obj1.foo.length, 2);
        assert.equal(obj1.foo[1], obj2.foo[0]);
    });
 
    it('should throw an error if an invalid array strategy is provided', () => {
        assert.throws(() => {
            merge({}, {}, {arrayStrategy: ('foo' as ArrayStrategy)});
        }, Messages.INVALID_ARRAY_STRATEGY('foo'));
    });
 
    it('should add arrays by reference when deep merging if `useReferenceIfArray` option set', () => {
        const obj1 = {
            foo: [{}]
        };
 
        const obj2 = {
            foo: [{bar: 1}]
        };
 
        merge(obj1, obj2, {
            deep: true,
            useReferenceIfArray: true
        });
 
        assert.equal(obj1.foo, obj2.foo);
        assert.equal(obj1.foo.length, 1);
    });
 
    it('should error when adding an undefined property on a sealed object', () => {
        const obj1 = {};
 
        const obj2 = {
            foo: 'bar'
        };
 
        Object.seal(obj1);
 
        assert.throws(() => merge(obj1, obj2), Messages.MERGE_ERROR('foo'));
    });
 
    it('should error when adding an undefined property on a non-extensible object', () => {
        const obj1 = {};
 
        const obj2 = {
            foo: 'bar'
        };
 
        Object.preventExtensions(obj1);
 
        assert.throws(() => merge(obj1, obj2), Messages.MERGE_ERROR('foo'));
    });
 
    it('should provide a reasonable suggestion if a similar property name exists', () => {
        const obj1 = {
            foo: null
        };
 
        const obj2 = {
            Foo: 'bar'
        };
 
        Object.seal(obj1);
 
        assert.throws(() => merge(obj1, obj2), Messages.MERGE_ERROR('Foo', 'foo'));
    });
 
    it('should provide no suggestion if no similar property name exists', () => {
        const obj1 = {
            bot: null
        };
 
        const obj2 = {
            foo: 'bar'
        };
 
        Object.seal(obj1);
 
        assert.throws(() => merge(obj1, obj2), TypeError, new RegExp('^' + Messages.MERGE_ERROR('foo') + '$'));
    });
 
    it('should provide a reasonable suggestion if a multiple similar property names exists', () => {
        const obj1 = {
            fooBar: null,
            FooBar: null,
            foo: null
        };
 
        const obj2 = {
            Foo: 'bar'
        };
 
        Object.seal(obj1);
 
        assert.throws(() => merge(obj1, obj2), Messages.MERGE_ERROR('Foo', 'foo'));
    });
 
    it('should provide a reasonable suggestion if the most similar property names contains padding characters', () => {
        const obj1 = {
            baz: null,
            foo: null,
            FooBarCar: null
        };
 
        const obj2 = {
            bar: 'bar'
        };
 
        Object.seal(obj1);
 
        assert.throws(() => merge(obj1, obj2), Messages.MERGE_ERROR('bar', 'FooBarCar'));
    });
 
    it('should allow a custom error message to be provided', () => {
        const obj1 = {
            baz: null,
            foo: null,
            FooBarCar: null
        };
 
        const obj2 = {
            bar: 'bar'
        };
 
        Object.seal(obj1);
 
        const config = {
            errorMessage: (offending, suggestion) =>
                `Invalid configuration option "${offending}". Did you mean "${suggestion}"?`
        };
 
        assert.throws(() => merge(obj1, obj2, config), 'Invalid configuration option "bar". Did you mean "FooBarCar"?');
    });
});