From 6cf1e833874946a2f107e0fe42696a947603919d Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Wed, 7 Jun 2017 11:13:42 +0200 Subject: [PATCH] fix(link-getter): resolve links with locale:* (#157) --- lib/mixins/link-getters.js | 14 +------------- test/integration/tests.js | 19 ------------------- test/unit/mixins/link-getters-test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/lib/mixins/link-getters.js b/lib/mixins/link-getters.js index 93c6a8cdc..a1bf8a2a0 100644 --- a/lib/mixins/link-getters.js +++ b/lib/mixins/link-getters.js @@ -5,22 +5,15 @@ import get from 'lodash/get' import partial from 'lodash/partial' import memoize from 'lodash/memoize' -let resolveAllLocales = false /** * Sets getters on links for a given response * @private * @param {Array} items * @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset */ -export default function mixinLinkGetters (items, includes, resolveForAllLocales) { - resolveAllLocales = resolveForAllLocales +export default function mixinLinkGetters (items, includes) { const linkGetter = memoize(getLinksFromIncludes, memoizationResolver) each(items, (item) => { - // TODO: workaround the preview endpoint extra locale this should be removed when - // it is fixed on the backend - if (resolveForAllLocales && item.sys.locale) { - delete item.sys.locale - } setLocalizedFieldGetters(item.fields, !!item.sys.locale) }) @@ -100,11 +93,6 @@ export default function mixinLinkGetters (items, includes, resolveForAllLocales) function getLinksFromIncludes (field) { var link = find(includes[field.sys.linkType], ['sys.id', field.sys.id]) if (link && link.fields) { - // TODO: workaround the preview endpoint extra locale this should be removed when - // it is fixed on the backend - if (resolveAllLocales && link.sys.locale) { - delete link.sys.locale - } setLocalizedFieldGetters(link.fields, !!link.sys.locale) return link } diff --git a/test/integration/tests.js b/test/integration/tests.js index a7059326d..af584e87f 100644 --- a/test/integration/tests.js +++ b/test/integration/tests.js @@ -7,11 +7,6 @@ const params = { accessToken: 'b4c0n73n7fu1', space: 'cfexampleapi' } -const previewParams = { - host: 'preview.contentful.com', - accessToken: 'e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50', - space: 'cfexampleapi' -} const localeSpaceParams = { accessToken: 'da1dc0e316213fe11e6139d3cd02f853b12da3f3fd0b4f146a1613a9cca277cd', space: '7dh3w86is8ls' @@ -23,7 +18,6 @@ if (process.env.API_INTEGRATION_TESTS) { } const client = contentful.createClient(params) -const previewClient = contentful.createClient(previewParams) const localeClient = contentful.createClient(localeSpaceParams) test('Gets space', (t) => { @@ -153,19 +147,6 @@ test('Gets entries with linked includes', (t) => { }) }) -test('Gets entries with linked includes with preview', (t) => { - t.plan(5) - return previewClient.getEntries({locale: '*', include: 5, 'sys.id': 'nyancat'}) - .then((response) => { - t.ok(response.includes, 'includes') - t.ok(response.includes.Asset, 'includes for Assets from preview endpoint') - t.ok(Object.keys(response.includes.Asset).length > 0, 'list of includes has asset items from preview endpoint') - // testing the fiels needs to be done first because it will call the getters and fill in bestFriend['en-US'].sys.type - t.ok(response.items[0].fields.bestFriend['en-US'].fields, 'resolved entry has fields from preview endpoint') - t.equal(response.items[0].fields.bestFriend['en-US'].sys.type, 'Entry', 'entry gets resolved from other entries in collection from preview endpoint') - }) -}) - test('Gets entries with content type query param', (t) => { t.plan(2) return client.getEntries({content_type: 'cat'}) diff --git a/test/unit/mixins/link-getters-test.js b/test/unit/mixins/link-getters-test.js index 1c140f898..9b7e98bbe 100644 --- a/test/unit/mixins/link-getters-test.js +++ b/test/unit/mixins/link-getters-test.js @@ -114,3 +114,30 @@ test('links in response, with circular references', (t) => { t.equals(items[0].fields.animal.fields.friend.fields.friend.sys.id, 'oink', 'sub sub link id') t.end() }) + +test('links in response with locale: *', (t) => { + const items = [ + { + sys: {type: 'Entry'}, + fields: { + animal: { 'en': {sys: {type: 'Link', linkType: 'Entry', id: 'oink'}} }, + animals: { 'en': [{sys: {type: 'Link', linkType: 'Entry', id: 'oink'}}] } + } + } + ] + const includes = { + Entry: [ + { + sys: {type: 'Entry', id: 'oink'}, + fields: { + name: { + 'en': 'Pig' + } + } + } + ] + } + mixinLinkGetters(items, includes) + t.equals(items[0].fields.animal['en'].fields.name['en'], includes.Entry[0].fields.name['en']) + t.end() +})