diff --git a/src/__tests__/__snapshots__/index.test.js.snap b/src/__tests__/__snapshots__/index.test.js.snap index bdf16085..a8ceac78 100644 --- a/src/__tests__/__snapshots__/index.test.js.snap +++ b/src/__tests__/__snapshots__/index.test.js.snap @@ -52,6 +52,18 @@ Array [ ] `; +exports[`denormalize set to undefined if schema key is not in entities 1`] = ` +Object { + "author": undefined, + "comments": Array [ + Object { + "user": undefined, + }, + ], + "id": "123", +} +`; + exports[`normalize can use fully custom entity classes 1`] = ` Object { "entities": Object { diff --git a/src/__tests__/index.test.js b/src/__tests__/index.test.js index 5b3f8250..c7c2de31 100644 --- a/src/__tests__/index.test.js +++ b/src/__tests__/index.test.js @@ -220,6 +220,33 @@ describe('denormalize', () => { expect(denormalize('123', article, entities)).toMatchSnapshot(); }); + test('set to undefined if schema key is not in entities', () => { + const user = new schema.Entity('users'); + const comment = new schema.Entity('comments', { + user: user + }); + const article = new schema.Entity('articles', { + author: user, + comments: [comment] + }); + + const entities = { + articles: { + '123': { + id: '123', + author: '8472', + comments: ['1'] + } + }, + comments: { + '1': { + user: '123' + } + } + }; + expect(denormalize('123', article, entities)).toMatchSnapshot(); + }); + test('does not modify the original entities', () => { const user = new schema.Entity('users'); const article = new schema.Entity('articles', { author: user }); diff --git a/src/index.js b/src/index.js index c2e9032d..559a5b7b 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,11 @@ const getEntities = (entities) => { return entityOrId; } - return isImmutable ? entities.getIn([schemaKey, entityOrId.toString()]) : entities[schemaKey][entityOrId]; + if (isImmutable) { + return entities.getIn([schemaKey, entityOrId.toString()]); + } + + return entities[schemaKey] && entities[schemaKey][entityOrId]; }; };