Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Added tests for deep merging. .deepMerge fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mschipperheyn committed Jun 14, 2016
1 parent 07281aa commit c831888
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import UnionSchema from './UnionSchema';
import lodashIsEqual from 'lodash/isEqual';
import lodashIsObject from 'lodash/isObject';

const NormalizedRecord = new Record({entities:null, result: null}, 'NormalizedRecord');
const PolymorphicMapper = new Record({id:null, schema: null});
const NormalizedRecord = Record({entities:null, result: null}, 'NormalizedRecord');
const PolymorphicMapper = Record({id:null, schema: null});

function defaultAssignEntity(normalized, key, entity) {
normalized[key] = entity;
Expand Down Expand Up @@ -120,8 +120,8 @@ function visitRecord(obj, schema, bag, options){
}
}

const Record = schema.getRecord();
return new Record(normalized);
const RRecord = schema.getRecord();
return new RRecord(normalized);
}

function defaultMapper(iterableSchema, itemSchema, bag, options) {
Expand Down Expand Up @@ -322,13 +322,13 @@ function normalize(obj, schema, options = {
if(options.useMapsForEntityObjects){
entityStructure[schemaKey] = new Map(bag[schemaKey]);
}else{
const ValueStructure = new Record(bag[schemaKey]);
const ValueStructure = Record(bag[schemaKey]);
entityStructure[schemaKey] = new ValueStructure({});
}

}

const EntityStructure = new Record(keyStructure);
const EntityStructure = Record(keyStructure);

entities = new EntityStructure(entityStructure);

Expand Down
42 changes: 39 additions & 3 deletions test/normalizerTest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ import { Record, List, Map } from 'immutable';

const reducerKey = 'myReducer';

const Tag = new Record({
const Tag = Record({
id:null,
label: null
});

const User = new Record({
const User = Record({
id:null,
nickName: null,
});

const Article = new Record({
const Article = Record({
//base comment
id:null,
txt:null,
Expand Down Expand Up @@ -274,6 +274,42 @@ describe("test normalizr", () => {

});

it("allows deep merging of new data", () => {
const normalized = normalize(json.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:true
});

const normalizedUpdate = normalize(jsonUpdate.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:true
});

let normalizedMerged = normalized.entities.merge(normalizedUpdate.entities);

expect(normalizedMerged.articles).to.contain.key('49444');
expect(normalizedMerged.articles).to.contain.key('49441');

});

it("allows deep merging of new data using deepMerge", () => {
const normalized = normalize(json.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:true
});

const normalizedUpdate = normalize(jsonUpdate.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:true
});

normalizedMerged = normalized.entities.mergeDeep(normalizedUpdate.entities);

expect(normalizedMerged.articles).to.contain.key('49441');
expect(normalizedMerged.articles).to.contain.key('49444');

});

it("allows accessing results through a proxy", () => {
const normalized = normalize(json.articles.items, arrayOf(schemas.article),{
getState:store.getState,
Expand Down

0 comments on commit c831888

Please sign in to comment.