Skip to content

Commit

Permalink
Fix schema key is not in entities (paularmstrong#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmouk authored and paularmstrong committed Aug 5, 2019
1 parent 15f6dfe commit 7573fbf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
};

Expand Down

0 comments on commit 7573fbf

Please sign in to comment.