Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #196 from teamleadercrm/PRO-3146-fix-error-when-me…
Browse files Browse the repository at this point in the history
…rging-entities

[PRO-3146] Fix error when trying to merge entities
  • Loading branch information
ArnaudWeyts authored Jan 23, 2020
2 parents 6eb04ef + c684a19 commit 711d44e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Unreleased

## ♻️ Changed
### 🐛 Fixed

* Fix error when trying to merge entity types that were not present in the store ([ArnaudWeyts](https://github.com/ArnaudWeyts) in [#196](https://github.com/teamleadercrm/react-hooks-api/pull/196))

### ♻️ Changed

* [INTERNAL] Replace deprecated tslint with eslint ([ArnaudWeyts](https://github.com/ArnaudWeyts) in [#194](https://github.com/teamleadercrm/react-hooks-api/pull/194))

Expand Down
24 changes: 24 additions & 0 deletions src/store/entities/__tests__/selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,29 @@ describe('Entities selectors', () => {

expect(mergeEntitiesIntoPaths(entitiesState, paths, mainEntity)).toEqual(result);
});

it('Does not crash when the referenced entity type is not in the entities state', () => {
const entitiesState = {};

const mainEntity = {
id: 'e6538393-aa7e-4ec2-870b-f75b3d85f706',
assignee: {
type: 'team',
id: '708c8008-3455-49a9-b66a-5222bcadb0cc'
},
};

const paths = ['assignee'];

const result = {
id: 'e6538393-aa7e-4ec2-870b-f75b3d85f706',
assignee: {
type: 'team',
id: '708c8008-3455-49a9-b66a-5222bcadb0cc'
},
};

expect(mergeEntitiesIntoPaths(entitiesState, paths, mainEntity)).toEqual(result);
})
});
});
4 changes: 2 additions & 2 deletions src/store/entities/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const mergeEntitiesIntoPaths = (entities: EntitiesState, paths: string[],

if (Array.isArray(sideloadReference)) {
sideloadReference.forEach((reference, index) => {
sideloadedEntity = entities[TYPE_DOMAIN_MAPPING[reference.type]][reference.id];
sideloadedEntity = entities[TYPE_DOMAIN_MAPPING[reference.type]]?.[reference.id];

// @TODO, hard coding the keys here means we only allow the first key to be an array of possible references
// find a way to support every nesting type
Expand All @@ -40,7 +40,7 @@ export const mergeEntitiesIntoPaths = (entities: EntitiesState, paths: string[],
return;
}

sideloadedEntity = entities[TYPE_DOMAIN_MAPPING[sideloadReference.type]][sideloadReference.id];
sideloadedEntity = entities[TYPE_DOMAIN_MAPPING[sideloadReference.type]]?.[sideloadReference.id];

set(draftEntity, path.split('.').map(camelCase).join('.'), { ...sideloadReference, ...sideloadedEntity });
});
Expand Down

0 comments on commit 711d44e

Please sign in to comment.