| import {mergeObjects} from '../../src/utils/operator'; |
| |
| |
| |
| |
| |
| |
| QUnit.module('utils'); |
| |
| |
| |
| |
| |
| |
| QUnit.test('Merge objects', function (assert) { |
| const obj001 = {id: {value: [0]}, a: {value: [1]}, b: {value: [1]}}; |
| const obj002 = {id: {value: [1]}, a: {value: [1]}, b: {value: [2]}}; |
| |
| |
| const fbad01 = function () { |
| mergeObjects(obj001, obj002, 'x', 'value'); |
| }; |
| assert.throws(fbad01, 'merge bad id key'); |
| |
| |
| const fbad02 = function () { |
| mergeObjects(obj001, obj002, 'id', 'x'); |
| }; |
| assert.throws(fbad02, 'merge bad value key'); |
| |
| |
| const obj003 = {id: {value: [0]}, a: {value: [1]}, b: {value: [2]}}; |
| const fbad03 = function () { |
| mergeObjects(obj001, obj003, 'id', 'value'); |
| }; |
| assert.throws(fbad03, 'merge with same id value'); |
| |
| |
| const ref00 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: [1]}, |
| b: {value: { |
| 0: [1], |
| 1: [2] |
| }} |
| }; |
| const res00 = mergeObjects(obj001, obj002, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res00), |
| JSON.stringify(ref00), |
| 'merge objects 00'); |
| |
| |
| const obj011 = {id: {value: [0]}, a: {value: [1]}, b: {value: [1]}}; |
| const obj012 = {id: {value: [1]}, a: {value: [1]}, b: {value: [2]}}; |
| const ref01 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: [1]}, |
| b: {value: { |
| 0: [1], |
| 1: [2] |
| }} |
| }; |
| const res01 = mergeObjects(obj011, obj012, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res01), |
| JSON.stringify(ref01), |
| 'merge objects 01'); |
| |
| |
| const obj021 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: [1]}, |
| b: {value: { |
| 0: [1], |
| 1: [2] |
| }} |
| }; |
| const obj022 = {id: {value: [2]}, a: {value: [1]}, b: {value: [2]}}; |
| const ref02 = { |
| id: {value: [0, 1, 2], merged: true}, |
| a: {value: [1]}, |
| b: {value: { |
| 0: [1], |
| 1: [2], |
| 2: [2] |
| }} |
| }; |
| const res02 = mergeObjects(obj021, obj022, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res02), |
| JSON.stringify(ref02), |
| 'merge objects 02'); |
| |
| |
| const obj031 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: [1]}, |
| b: {value: { |
| 0: [1], |
| 1: [2] |
| }} |
| }; |
| const obj032 = {id: {value: [2]}, a: {value: [2]}, b: {value: [3]}}; |
| const ref03 = { |
| id: {value: [0, 1, 2], merged: true}, |
| a: {value: { |
| 0: [1], |
| 1: [1], |
| 2: [2] |
| }}, |
| b: {value: { |
| 0: [1], |
| 1: [2], |
| 2: [3] |
| }} |
| }; |
| const res03 = mergeObjects(obj031, obj032, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res03), |
| JSON.stringify(ref03), |
| 'merge objects 03'); |
| |
| |
| const obj101 = {id: {value: [0]}, a: {value: [1]}}; |
| const obj102 = {id: {value: [1]}, a: {value: [2]}, b: {value: [1]}}; |
| const ref10 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: { |
| 0: [1], |
| 1: [2] |
| }}, |
| b: {value: { |
| 0: undefined, |
| 1: [1] |
| }} |
| }; |
| const res10 = mergeObjects(obj101, obj102, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res10), |
| JSON.stringify(ref10), |
| 'merge objects 10'); |
| |
| |
| const obj111 = {id: {value: [0]}, a: {value: [1]}, b: {value: [1]}}; |
| const obj112 = {id: {value: [1]}, a: {value: [2]}}; |
| const ref11 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: { |
| 0: [1], |
| 1: [2] |
| }}, |
| b: {value: { |
| 0: [1], |
| 1: undefined |
| }} |
| }; |
| const res11 = mergeObjects(obj111, obj112, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res11), |
| JSON.stringify(ref11), |
| 'merge objects 11'); |
| |
| |
| const obj121 = {id: {value: [0]}, a: {value: [1]}, b: {}}; |
| const obj122 = {id: {value: [1]}, a: {value: [2]}, b: {value: [1]}}; |
| const ref12 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: { |
| 0: [1], |
| 1: [2] |
| }}, |
| b: {value: { |
| 0: undefined, |
| 1: [1] |
| }} |
| }; |
| const res12 = mergeObjects(obj121, obj122, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res12), |
| JSON.stringify(ref12), |
| 'merge objects 12'); |
| |
| |
| const obj131 = {id: {value: [0]}, a: {value: [1]}, b: {value: [1]}}; |
| const obj132 = {id: {value: [1]}, a: {value: [2]}, b: {}}; |
| const ref13 = { |
| id: {value: [0, 1], merged: true}, |
| a: {value: { |
| 0: [1], |
| 1: [2] |
| }}, |
| b: {value: { |
| 0: [1], |
| 1: undefined |
| }} |
| }; |
| const res13 = mergeObjects(obj131, obj132, 'id', 'value'); |
| assert.equal( |
| JSON.stringify(res13), |
| JSON.stringify(ref13), |
| 'merge objects 13'); |
| |
| }); |