|
| 1 | +import {expect} from 'chai'; |
| 2 | +import {deepAssign} from '../../../lib/utils/object'; |
| 3 | + |
| 4 | +/* tslint:disable:max-classes-per-file */ |
| 5 | + |
| 6 | +describe('utils', () => { |
| 7 | + |
| 8 | + describe('object', () => { |
| 9 | + |
| 10 | + describe('deepAssign', () => { |
| 11 | + |
| 12 | + const childSourceF = {}; |
| 13 | + const childSourceA = {f: childSourceF}; |
| 14 | + const childSourceB = {}; |
| 15 | + const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim}; |
| 16 | + const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden'}; |
| 17 | + const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2)); |
| 18 | + |
| 19 | + it('should not be undefined', () => { |
| 20 | + |
| 21 | + const copy = deepAssign({}, source1, source2); |
| 22 | + |
| 23 | + expect(copy).not.to.be.undefined; |
| 24 | + }); |
| 25 | + |
| 26 | + it('should have all keys of sources', () => { |
| 27 | + |
| 28 | + const copy = deepAssign({}, source1, source2); |
| 29 | + |
| 30 | + sourceKeys |
| 31 | + .forEach(key => expect(copy).to.have.property(key)) |
| 32 | + ; |
| 33 | + }); |
| 34 | + |
| 35 | + it('should override previous properties', () => { |
| 36 | + |
| 37 | + const copy = deepAssign({}, source1, source2); |
| 38 | + |
| 39 | + expect(copy).to.have.property('over', 'ridden'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should have all primitive & function values of sources', () => { |
| 43 | + |
| 44 | + const copy = deepAssign({}, source1, source2); |
| 45 | + |
| 46 | + sourceKeys |
| 47 | + .forEach(key => { |
| 48 | + |
| 49 | + if (typeof copy[key] !== 'object') { |
| 50 | + |
| 51 | + expect(copy[key]).to.equal(source2[key] || source1[key]); |
| 52 | + } |
| 53 | + }) |
| 54 | + ; |
| 55 | + }); |
| 56 | + |
| 57 | + it('should have copies of all non-primitive values of sources', () => { |
| 58 | + |
| 59 | + const copy = deepAssign({}, source1, source2); |
| 60 | + |
| 61 | + sourceKeys |
| 62 | + .forEach(key => { |
| 63 | + |
| 64 | + if (typeof copy[key] === 'object') { |
| 65 | + |
| 66 | + expect(copy[key]).not.to.equal(source1[key] || source2[key]); |
| 67 | + expect(copy[key]).to.eql(source1[key] || source2[key]); |
| 68 | + } |
| 69 | + }) |
| 70 | + ; |
| 71 | + }); |
| 72 | + |
| 73 | + it('should have copies of child source', () => { |
| 74 | + |
| 75 | + const copy = deepAssign({}, source1, source2); |
| 76 | + |
| 77 | + expect(copy.a).to.have.property('f'); |
| 78 | + expect(copy.a.f).to.not.equal(source1.a.f); |
| 79 | + expect(copy.a.f).to.eql(source1.a.f); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should have copy of array items', () => { |
| 83 | + |
| 84 | + const copy = deepAssign({}, source1, source2); |
| 85 | + |
| 86 | + expect(copy.arr).to.be.an('array'); |
| 87 | + |
| 88 | + copy.arr.forEach((value, index) => { |
| 89 | + |
| 90 | + const isObject = typeof value === 'object'; |
| 91 | + |
| 92 | + if (isObject) { |
| 93 | + expect(value).not.to.equal(source2.arr[index]); |
| 94 | + expect(value).to.eql(source2.arr[index]); |
| 95 | + |
| 96 | + } else { |
| 97 | + |
| 98 | + expect(value).to.equal(source2.arr[index]); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + }); |
| 104 | + |
| 105 | + }); |
| 106 | +}); |
0 commit comments