CygnusKnight CygnusKnight - 2 months ago 37
Javascript Question

Losing properties when using mergeDeep() of Immutable.js

I'm trying to merge 2 immutable maps as below, using mergeDeep() of Immutable.js

import { expect } from 'chai';
import Immutable from 'immutable';
describe.only('Test', () => {
it('should return correct merged objects', () => {
const current = Immutable.Map({
a: [],
b: {},
c: {
'c01': {
key: 'c01'
}
}
});
const next = {
a: [],
b: {},
c: {
'c01': {
key: 'c01'
},
'c02': {
key: 'c02'
}
}
};
const newObj = Immutable.Map({
a: [],
b: {},
c: {
'c02': {
key: 'c02'
}
}
});
expect(current.mergeDeep(newObj).toJSON()).to.deep.equal(next);
});
});


However, the property 'c01' is missing after merging.

AssertionError: expected { Object (a, b, ...) } to deeply equal { Object (a, b, ...) }
+ expected - actual

{
"a": []
"b": {}
"c": {
+ "c01": {
+ "key": "c01"
+ }
"c02": {
"key": "c02"
}
}


Can mergeDeep() do the merging for different properties from 2 different Map objects or only merge properties which are mutual in both? If it can't, how can I get the expected merged object as above?

Answer

change

const current = Immutable.Map({ ... });

to

const current = Immutable.fromJS({ ... });