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

Commit

Permalink
changed signature of fallbackStrategy to be (id, schema), no point in…
Browse files Browse the repository at this point in the history
… passing entity when the fallback is only executed when entity is undefined,   updated tests
  • Loading branch information
bjartebore committed Oct 14, 2019
1 parent c35b5bb commit 4050536
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 58,7 @@ const unvisitEntity = (id, schema, unvisit, getEntity, cache) => {
let entity = getEntity(id, schema);

if (entity === undefined && schema instanceof EntitySchema) {
entity = schema.fallback(entity, id);
entity = schema.fallback(id, schema);
}

if (typeof entity !== 'object' || entity === null) {
Expand Down
6 changes: 3 additions & 3 deletions src/schemas/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 15,7 @@ export default class EntitySchema {
return { ...entityA, ...entityB };
},
processStrategy = (input) => ({ ...input }),
fallbackStrategy = (input, key) => input
fallbackStrategy = (key, schema) => undefined
} = options;

this._key = key;
Expand Down Expand Up @@ -50,8 50,8 @@ export default class EntitySchema {
return this._mergeStrategy(entityA, entityB);
}

fallback(input, id) {
return this._fallbackStrategy(input, id);
fallback(id, schema) {
return this._fallbackStrategy(id, schema);
}

normalize(input, parent, key, visit, addEntity, visitedEntities) {
Expand Down
6 changes: 4 additions & 2 deletions src/schemas/__tests__/Entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 298,9 @@ describe(`${schema.Entity.name} denormalization`, () => {
'users',
{},
{
fallbackStrategy: (entity, id) => ({
id: id,
idAttribute: 'userId',
fallbackStrategy: (id, schema) => ({
[schema.idAttribute]: id,
name: 'John Doe'
})
}
Expand All @@ -325,6 326,7 @@ describe(`${schema.Entity.name} denormalization`, () => {

expect(denormalizedReport.publishedBy).toBe(denormalizedReport.draftedBy);
expect(denormalizedReport.publishedBy.name).toBe('John Doe');
expect(denormalizedReport.publishedBy.userId).toBe('456');
//
});
});

0 comments on commit 4050536

Please sign in to comment.