Skip to content

Commit 8a96bdf

Browse files
committed
fix(sync): Fix issue with links in sync not being resolved
1 parent ed9b2fe commit 8a96bdf

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/sync.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default function pagedSync (http: Object, query: Object, resolveLinks: bo
6767
.then(response => {
6868
// clones response.items used in includes because we don't want these to be mutated
6969
if (resolveLinks) {
70-
mixinLinkGetters(response.items, cloneDeep(mapResponseItems(response.items)))
70+
mixinLinkGetters(response.items, mapIncludeItems(cloneDeep(response.items)))
7171
}
7272
// maps response items again after getters are attached
7373
const mappedResponseItems = mapResponseItems(response.items)
@@ -87,6 +87,18 @@ function mapResponseItems (items: ResponseItems): Object {
8787
}
8888
}
8989

90+
/**
91+
* @private
92+
* Creates an object similar to the one retrieved on `includes` from the `entries`
93+
* endpoint, for usage with the link getters mixin
94+
*/
95+
function mapIncludeItems (items: ResponseItems): Object {
96+
return {
97+
Entry: filter(items, ['sys.type', 'Entry']),
98+
Asset: filter(items, ['sys.type', 'Asset'])
99+
}
100+
}
101+
90102
/**
91103
* @private
92104
* If the response contains a nextPageUrl, extracts the sync token to get the

test/integration/tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,14 @@ test('Gets assets', t => {
282282
})
283283

284284
test('Sync space', t => {
285-
t.plan(5)
285+
t.plan(6)
286286
return client.sync({initial: true})
287287
.then(response => {
288288
t.ok(response.entries, 'entries')
289289
t.ok(response.assets, 'assets')
290290
t.ok(response.deletedEntries, 'deleted entries')
291291
t.ok(response.deletedAssets, 'deleted assets')
292292
t.ok(response.nextSyncToken, 'next sync token')
293+
t.equal(response.entries[4].fields.image['en-US'].sys.type, 'Asset', 'links are resolved')
293294
})
294295
})

test/unit/sync-test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ test('Throws with incompatible content_type and type parameter', t => {
4444
})
4545

4646
test('Initial sync with one page', t => {
47-
t.plan(6)
47+
t.plan(7)
4848
const http = {get: sinon.stub()}
49+
const entryWithLink = createEntry('1')
50+
entryWithLink.fields.linked = {
51+
sys: {
52+
id: '2',
53+
type: 'Link',
54+
linkType: 'Entry'
55+
}
56+
}
4957
http.get.withArgs('sync', {params: {initial: true}}).returns(Promise.resolve({
5058
data: {
5159
items: [
52-
createEntry('1'),
60+
entryWithLink,
5361
createEntry('2'),
5462
createEntry('3'),
5563
createEntry('3', true),
@@ -71,6 +79,7 @@ test('Initial sync with one page', t => {
7179
t.equal(response.assets.length, 3, 'entries length')
7280
t.equal(response.deletedAssets.length, 1, 'deleted assets length')
7381
t.equal(response.nextSyncToken, 'nextsynctoken', 'next sync token')
82+
t.equal(response.entries[0].fields.linked.sys.type, 'Entry', 'linked entry is resolved')
7483
})
7584
})
7685

0 commit comments

Comments
 (0)