Skip to content

Commit 65958be

Browse files
committed
Merge pull request #63 from contentful/fix-items-links
fix(links): Fix link resolution for links in same response
2 parents e3f21e9 + 9de1635 commit 65958be

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

lib/entities/entry.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {cloneDeep} from 'lodash/lang'
2+
import {uniq} from 'lodash/array'
23
import mixinToPlainObject from '../mixins/to-plain-object'
34
import mixinLinkGetters from '../mixins/link-getters'
45

@@ -39,8 +40,15 @@ export function wrapEntry (data) {
3940
*/
4041
export function wrapEntryCollection (data, resolveLinks) {
4142
const wrappedData = mixinToPlainObject(cloneDeep(data))
42-
if (wrappedData.includes && resolveLinks) {
43-
mixinLinkGetters(wrappedData.items, wrappedData.includes)
43+
if (resolveLinks) {
44+
const includes = prepareIncludes(wrappedData.includes, wrappedData.items)
45+
mixinLinkGetters(wrappedData.items, includes)
4446
}
4547
return Object.freeze(wrappedData)
4648
}
49+
50+
function prepareIncludes (includes = {}, items) {
51+
includes.Entry = includes.Entry || []
52+
includes.Entry = uniq(includes.Entry.concat(cloneDeep(items)))
53+
return includes
54+
}

test/integration/tests.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ test('Gets entries', t => {
6060
})
6161

6262
test('Gets entries with linked includes', t => {
63-
t.plan(3)
63+
t.plan(5)
6464
return client.getEntries({include: 2})
6565
.then(response => {
6666
t.ok(response.includes, 'includes')
6767
t.ok(response.includes.Asset, 'includes for Assets')
6868
t.ok(Object.keys(response.includes.Asset).length > 0, 'list of includes has asset items')
69+
t.equal(response.items[0].fields.bestFriend.sys.type, 'Entry', 'entry gets resolved from other entries in collection')
70+
t.ok(response.items[0].fields.bestFriend.fields, 'resolved entry has fields')
6971
})
7072
})
7173

test/unit/entities/entry-test.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'tape'
22

3-
import {entryMock} from '../mocks'
3+
import {entryMock, assetMock} from '../mocks'
44
import {cloneDeep} from 'lodash/lang'
55
import {wrapEntry, wrapEntryCollection} from '../../../lib/entities/entry'
66

@@ -30,12 +30,53 @@ test('Entry collection is wrapped', t => {
3030
limit: 100,
3131
items: [
3232
entryMock
33+
]
34+
}
35+
const wrappedEntry = wrapEntryCollection(entryCollection, true)
36+
t.looseEqual(wrappedEntry.toPlainObject(), entryCollection)
37+
t.end()
38+
})
39+
40+
test('Entry collection links are resolved', t => {
41+
const entryCollection = {
42+
total: 1,
43+
skip: 0,
44+
limit: 100,
45+
items: [
46+
cloneDeep(entryMock),
47+
cloneDeep(entryMock)
3348
],
3449
includes: {
35-
Entry: []
50+
Asset: [ cloneDeep(assetMock) ]
3651
}
3752
}
38-
const wrappedEntry = wrapEntryCollection(entryCollection, true)
39-
t.looseEqual(wrappedEntry.toPlainObject(), entryCollection)
53+
// setup first entry
54+
entryCollection.items[0].sys.id = 'entry1'
55+
entryCollection.items[0].fields.linked1 = {
56+
sys: {
57+
id: 'asset1',
58+
type: 'Link',
59+
linkType: 'Asset'
60+
}
61+
}
62+
entryCollection.items[0].fields.linked2 = {
63+
sys: {
64+
id: 'entry3',
65+
type: 'Link',
66+
linkType: 'Entry'
67+
}
68+
}
69+
// setup first linked entry
70+
entryCollection.includes.Asset[0].sys.id = 'asset1'
71+
// setup second linked entry
72+
entryCollection.items[1].sys.id = 'entry3'
73+
74+
const wrappedEntry = wrapEntryCollection(entryCollection, true).toPlainObject()
75+
// first linked entry resolved from includes
76+
t.equals(wrappedEntry.items[0].fields.linked1.sys.type, 'Asset', 'first linked entity is resolved')
77+
t.ok(wrappedEntry.items[0].fields.linked1.fields, 'first linked entity has fields')
78+
// second linked entry resolved from items list
79+
t.equals(wrappedEntry.items[0].fields.linked2.sys.type, 'Entry', 'second linked entity is resolved')
80+
t.ok(wrappedEntry.items[0].fields.linked2.fields, 'second linked entity has fields')
4081
t.end()
4182
})

0 commit comments

Comments
 (0)