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

Commit

Permalink
fix(core): fromState returns incorrect relationship types for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
michiel committed Aug 7, 2019
1 parent 905b98e commit 4193e49
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 44 deletions.
97 changes: 70 additions & 27 deletions packages/core/__tests__/Resource/Resource.fromState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,91 @@ describe('Resource.fromState functionality', () => {
expect(() => resource.relationships).not.toThrow(TypeError);
});

test('that a new Resource can be remade based on an existing state', () => {
const resource = Resource.create(1, 'book',
{
title: 'A great book',
images: [
Resource.create(2, 'images', { file: 'image-1.png' }),
Resource.create(3, 'images-large', { file: 'image-2-large.png' }),
]
},
{
images: {
cardinality: 'one-to-many',
resource: 'images',
many: true,
},
},
);

const state = resource.state;
const recreatedResource = Resource.fromState(resource.id, resource.type, state);

expect(recreatedResource.id).toEqual(1);
expect(recreatedResource.type).toEqual('book');
expect(recreatedResource.data.title).toEqual('A great book');
expect(recreatedResource.data.images).toHaveLength(2);
expect(recreatedResource.data.images[0].id).toEqual(2);
expect(recreatedResource.data.images[0].type).toEqual('images');
expect(recreatedResource.data.images[1].id).toEqual(3);
expect(recreatedResource.data.images[1].type).toEqual('images-large');
});

test('that a new resource can be made with relationships from state', () => {
const state = {
id: 2,
data: {
title: 'John\'s biography',
author: {
id: 1,
data: {
name: 'John',
authorImage: {
id: 5,
data: {
src: 'image.jpg',
type: 'people',
state: {
id: 1,
data: {
name: 'John',
authorImage: {
type: 'image',
state: {
id: 5,
data: {
src: 'image.jpg',
},
relationships: {},
meta: {},
},
},
relationships: {},
meta: {},
},
},
relationships: {
authorImage: {
cardinality: 'many-to-one',
resource: 'authorImage',
relationships: {
authorImage: {
cardinality: 'many-to-one',
resource: 'authorImage',
},
},
},
meta: {},
meta: {},
}
},
publication: [
{
id: 3,
data: {
name: 'Publication 1',
type: 'publication',
state: {
id: 3,
data: {
name: 'Publication 1',
},
relationships: {},
meta: {},
},
relationships: {},
meta: {},
},
{
id: 4,
data: {
name: 'Publication 2',
type: 'publication',
state: {
id: 4,
data: {
name: 'Publication 2',
},
relationships: {},
meta: {},
},
relationships: {},
meta: {},
},
],
coAuthor: null,
Expand Down Expand Up @@ -93,7 +136,7 @@ describe('Resource.fromState functionality', () => {
expect(resource.data.publication[0].data.name).toEqual('Publication 1');
expect(resource.data.publication[0]).toHaveProperty('setMetadata');
expect(resource.data.publication[1].data.name).toEqual('Publication 2');
expect(resource.data.publication[1]).toHaveProperty('setMetadata')
expect(resource.data.publication[1]).toHaveProperty('setMetadata');
expect(resource.data.coAuthor).toEqual(null);
});
});
10 changes: 5 additions & 5 deletions packages/core/__tests__/Resource/Resource.state.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ describe('The Resource state', () => {

expect(book.state.data.author).not.toBe(author);
expect(book.state.data.author).not.toHaveProperty('setMetadata');
expect(book.state.data.author).toStrictEqual(author.state);
expect(book.state.data.author).toEqual({ state: author.state, type: author.type });

expect(book.state.data.author.data.authorImage).not.toBe(authorImage);
expect(book.state.data.author.data.authorImage).not.toHaveProperty('setMetadata');
expect(book.state.data.author.data.authorImage).toStrictEqual(authorImage.state);
expect(book.state.data.author.state.data.authorImage).not.toBe(authorImage);
expect(book.state.data.author.state.data.authorImage).not.toHaveProperty('setMetadata');
expect(book.state.data.author.state.data.authorImage).toEqual({ state: authorImage.state, type: authorImage.type });

expect(book.state.data.publication[0]).not.toBe(publication1);
expect(book.state.data.publication[0]).not.toHaveProperty('setMetadata');
expect(book.state.data.publication[0]).toStrictEqual(publication1.state);
expect(book.state.data.publication[0]).toEqual({ state: publication1.state, type: publication1.type });
});

});
26 changes: 18 additions & 8 deletions packages/core/src/Resource/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ function Resource(id, type, data = null, relationships = null, meta = null) {
}

if (Array.isArray(normalState.data[relationKey])) {
return normalState.data[relationKey].map(value => value.state);
return normalState.data[relationKey].map(value => ({ state: value.state, type: value.type }));
}

return normalState.data[relationKey].state;
return { state: normalState.data[relationKey].state, type: normalState.data[relationKey].type };
});

return Object.assign({}, normalState, {
Expand Down Expand Up @@ -180,23 +180,33 @@ Resource.create = (id = null, type, data = null, relationships = null, meta = nu
* @returns {HyralResource}
*/
Resource.fromState = (id, type, state) => {
if (!state || !state.relationships) {
return Resource.create(
id,
type,
state ? state.data : null,
null,
state ? state.meta : null,
);
}

const relationData = mapValues(state.relationships, (relation, relationKey) => {
if (!state.data[relationKey]) {
return null;
}

if (Array.isArray(state.data[relationKey])) {
return state.data[relationKey].map(value => Resource.fromState(
state.data[relationKey].id,
relation.resource,
value || null,
value.state ? value.state.id : null,
value.type,
value.state || null,
));
}

return Resource.fromState(
state.data[relationKey].id,
relation.resource,
state.data[relationKey] || null,
state.data[relationKey].state ? state.data[relationKey].state.id : null,
state.data[relationKey].type,
state.data[relationKey].state || null,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ describe('Validations for the responseNormalizer', () => {
expect(result.data[1].relationships.images.resource).toBe('images');
expect(result.data[1].relationships.images.many).toBeTruthy();
expect(result.data[1].relationships.images.cardinality).toEqual('one-to-many');
expect(result.data[1].data.images).toHaveLength(2);
expect(result.data[1].data.images).toHaveLength(3);
expect(result.data[1].data.images[0]).toHaveProperty('id');
expect(result.data[1].data.images[0]).toHaveProperty('type');
expect(result.data[1].data.images[0].type).toEqual('images');
expect(result.data[1].data.images[1].type).toEqual('images');
expect(result.data[1].data.images[2].type).toEqual('images-large');
});

test('that the responseNormalizer does not overwrite the resource relationships decorator', () => {
Expand Down Expand Up @@ -150,7 +153,7 @@ describe('Validations for the responseNormalizer', () => {
const result = responseNormalizer(jsonResponseFixture);

expect(result.data[1].data).toHaveProperty('images');
expect(result.data[1].data.images).toHaveLength(2);
expect(result.data[1].data.images).toHaveLength(3);
expect(result.data[1].data.images[0]).toMatchSchema(resourceJsonSchema);

expect(result.data[1].data.images[0].metadata.loaded).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
{
"type": "images",
"id": "0CF27581-7348-4616-AD07-2450F9D04798"
},
{
"type": "images-large",
"id": "EF7584C8-1A89-4AB0-A87A-135BDAFE86FE"
}
]
}
Expand All @@ -80,15 +84,23 @@
"type": "images",
"id": "0eb618b7-4af1-507d-9769-57ff9e897b52",
"attributes": {
"image.name": "list-imago-tracker.jpg",
"image.name": "list.jpg",
"focusPoint": "bottom"
}
},
{
"type": "images-large",
"id": "EF7584C8-1A89-4AB0-A87A-135BDAFE86FE",
"attributes": {
"image.name": "list-large.jpg",
"focusPoint": "bottom"
}
},
{
"type": "images",
"id": "cbf08c79-62ae-5a7e-971a-bd693350285d",
"attributes": {
"image.name": "list-employee-experience-tracker.jpg",
"image.name": "list.jpg",
"focusPoint": "center-left"
}
}
Expand Down

0 comments on commit 4193e49

Please sign in to comment.