|
| 1 | +import { expect } from 'chai' |
| 2 | +import { merge } from './merge' |
| 3 | + |
| 4 | +describe('merge', () => { |
| 5 | + it('removes nulled arrays when nested items were found', () => { |
| 6 | + const object1 = { status: 'draft', postImage: null, blogImageSizes: null } |
| 7 | + const object2 = { 'blogImageSizes.0': 4130, 'blogImageMimeTypes.0': 'image/jpeg' } |
| 8 | + |
| 9 | + expect(merge(object1, object2)).to.deep.equal({ |
| 10 | + status: 'draft', |
| 11 | + postImage: null, |
| 12 | + 'blogImageSizes.0': 4130, |
| 13 | + 'blogImageMimeTypes.0': 'image/jpeg', |
| 14 | + }) |
| 15 | + }) |
| 16 | + |
| 17 | + context('object with nested fields are given in the first argument', () => { |
| 18 | + const object1 = { status: { |
| 19 | + type: 'draft', |
| 20 | + updated: 'yesterday', |
| 21 | + tags: ['super'], |
| 22 | + } } |
| 23 | + |
| 24 | + it('flattens everything and changes just nested property when it was given nested', () => { |
| 25 | + const object2 = { 'status.type': 'newDraft' } |
| 26 | + |
| 27 | + expect(merge(object1, object2)).to.deep.equal({ |
| 28 | + 'status.type': object2['status.type'], |
| 29 | + 'status.updated': 'yesterday', |
| 30 | + 'status.tags.0': 'super', |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('changes entire record when 2 objects are given', () => { |
| 35 | + const object2 = { status: { |
| 36 | + type: 'newType', |
| 37 | + updated: 'today', |
| 38 | + } } |
| 39 | + |
| 40 | + expect(merge(object1, object2)).to.deep.equal({ |
| 41 | + 'status.type': object2.status.type, |
| 42 | + 'status.updated': 'today', |
| 43 | + }) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + describe('multiple parameters', () => { |
| 48 | + const object1 = { status: { type: 'draft' } } |
| 49 | + |
| 50 | + it('returns flatten object when one other argument is given', () => { |
| 51 | + expect(merge(object1)).to.deep.equal({ |
| 52 | + 'status.type': 'draft', |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('merges more then 2 arguments', () => { |
| 57 | + const object2 = { |
| 58 | + 'status.type': 'status2', |
| 59 | + 'status.age': '1 day', |
| 60 | + } |
| 61 | + const object3 = { |
| 62 | + 'status.type': 'status3', |
| 63 | + names: [ |
| 64 | + 'Wojtek', |
| 65 | + ], |
| 66 | + } |
| 67 | + expect(merge(object1, object2, object3)).to.deep.equal({ |
| 68 | + 'status.type': 'status2', |
| 69 | + 'status.age': '1 day', |
| 70 | + 'names.0': 'Wojtek', |
| 71 | + }) |
| 72 | + }) |
| 73 | + }) |
| 74 | +}) |
0 commit comments