Skip to content

Commit

Permalink
fix(kitsu-core): fix deserialisation of relationships from primary data
Browse files Browse the repository at this point in the history
  • Loading branch information
pedep committed Jun 23, 2022
1 parent d5e20ac commit a6e0c49
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/kitsu-core/src/deserialise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { linkRelationships } from '../linkRelationships'
function deserialiseArray (array) {
for (let value of array.data) {
if (array.included) value = linkRelationships(value, array.included)
if (value.relationships) value = linkRelationships(value)
value = linkRelationships(value, array.data)
if (value.attributes) value = deattribute(value)
array.data[array.data.indexOf(value)] = value
}
Expand Down
102 changes: 102 additions & 0 deletions packages/kitsu-core/src/deserialise/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,5 +670,107 @@ describe('kitsu-core', () => {
]
})
})

it('deserialises a relationship from the data key', () => {
expect.assertions(1)

const input = deserialise({
data: [
{
id: '1',
type: 'anime',
attributes: { name: 'A' },
relationships: {
prequel: {
data: {
type: 'anime',
id: '42'
}
}
}
},
{
id: '2',
type: 'anime',
attributes: { name: 'B' },
relationships: {
prequel: {
data: {
type: 'anime',
id: '1'
}
}
}
},
{
id: '3',
type: 'anime',
attributes: { name: 'C' },
relationships: {
prequel: {
data: {
type: 'anime',
id: '1'
}
}
}
}
]
})

const output = {
data: [
{
id: '1',
type: 'anime',
name: 'A',
prequel: {
data: {
id: '42',
type: 'anime'
}
}
},
{
id: '2',
type: 'anime',
name: 'B',
prequel: {
data: {
id: '1',
type: 'anime',
name: 'A',
prequel: {
data: {
id: '42',
type: 'anime'
}
}
}
}
},
{
id: '3',
type: 'anime',
name: 'C',
prequel: {
data: {
id: '1',
type: 'anime',
name: 'A',
prequel: {
data: {
id: '42',
type: 'anime'
}
}
}
}
}
]
}

expect(input).toEqual(output)
})
})
})

0 comments on commit a6e0c49

Please sign in to comment.